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, ...);
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)
88 assert(SvMAGICAL(sv));
89 /* Turning READONLY off for a copy-on-write scalar (including shared
90 hash keys) is a bad idea. */
92 sv_force_normal_flags(sv, 0);
94 SAVEDESTRUCTOR_X(S_restore_magic, INT2PTR(void*, (IV)mgs_ix));
96 mgs = SSPTR(mgs_ix, MGS*);
98 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
99 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
103 if (!(SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK))) {
104 /* No public flags are set, so promote any private flags to public. */
105 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
110 =for apidoc mg_magical
112 Turns on the magical status of an SV. See C<sv_magic>.
118 Perl_mg_magical(pTHX_ SV *sv)
122 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
123 const MGVTBL* const vtbl = mg->mg_virtual;
125 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
129 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
138 Do magic after a value is retrieved from the SV. See C<sv_magic>.
144 Perl_mg_get(pTHX_ SV *sv)
147 const I32 mgs_ix = SSNEW(sizeof(MGS));
148 const bool was_temp = (bool)SvTEMP(sv);
150 MAGIC *newmg, *head, *cur, *mg;
151 /* guard against sv having being freed midway by holding a private
154 /* sv_2mortal has this side effect of turning on the TEMP flag, which can
155 cause the SV's buffer to get stolen (and maybe other stuff).
158 sv_2mortal(SvREFCNT_inc_simple_NN(sv));
163 save_magic(mgs_ix, sv);
165 /* We must call svt_get(sv, mg) for each valid entry in the linked
166 list of magic. svt_get() may delete the current entry, add new
167 magic to the head of the list, or upgrade the SV. AMS 20010810 */
169 newmg = cur = head = mg = SvMAGIC(sv);
171 const MGVTBL * const vtbl = mg->mg_virtual;
173 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
174 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
176 /* guard against magic having been deleted - eg FETCH calling
181 /* Don't restore the flags for this entry if it was deleted. */
182 if (mg->mg_flags & MGf_GSKIP)
183 (SSPTR(mgs_ix, MGS *))->mgs_flags = 0;
186 mg = mg->mg_moremagic;
189 /* Have we finished with the new entries we saw? Start again
190 where we left off (unless there are more new entries). */
198 /* Were any new entries added? */
199 if (!have_new && (newmg = SvMAGIC(sv)) != head) {
206 restore_magic(INT2PTR(void *, (IV)mgs_ix));
208 if (SvREFCNT(sv) == 1) {
209 /* We hold the last reference to this SV, which implies that the
210 SV was deleted as a side effect of the routines we called. */
219 Do magic after a value is assigned to the SV. See C<sv_magic>.
225 Perl_mg_set(pTHX_ SV *sv)
228 const I32 mgs_ix = SSNEW(sizeof(MGS));
232 save_magic(mgs_ix, sv);
234 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
235 const MGVTBL* vtbl = mg->mg_virtual;
236 nextmg = mg->mg_moremagic; /* it may delete itself */
237 if (mg->mg_flags & MGf_GSKIP) {
238 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
239 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
241 if (vtbl && vtbl->svt_set)
242 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
245 restore_magic(INT2PTR(void*, (IV)mgs_ix));
250 =for apidoc mg_length
252 Report on the SV's length. See C<sv_magic>.
258 Perl_mg_length(pTHX_ SV *sv)
264 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
265 const MGVTBL * const vtbl = mg->mg_virtual;
266 if (vtbl && vtbl->svt_len) {
267 const I32 mgs_ix = SSNEW(sizeof(MGS));
268 save_magic(mgs_ix, sv);
269 /* omit MGf_GSKIP -- not changed here */
270 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
271 restore_magic(INT2PTR(void*, (IV)mgs_ix));
277 const U8 *s = (U8*)SvPV_const(sv, len);
278 len = utf8_length(s, s + len);
281 (void)SvPV_const(sv, len);
286 Perl_mg_size(pTHX_ SV *sv)
290 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
291 const MGVTBL* const vtbl = mg->mg_virtual;
292 if (vtbl && vtbl->svt_len) {
293 const I32 mgs_ix = SSNEW(sizeof(MGS));
295 save_magic(mgs_ix, sv);
296 /* omit MGf_GSKIP -- not changed here */
297 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
298 restore_magic(INT2PTR(void*, (IV)mgs_ix));
305 return AvFILLp((AV *) sv); /* Fallback to non-tied array */
309 Perl_croak(aTHX_ "Size magic not implemented");
318 Clear something magical that the SV represents. See C<sv_magic>.
324 Perl_mg_clear(pTHX_ SV *sv)
326 const I32 mgs_ix = SSNEW(sizeof(MGS));
329 save_magic(mgs_ix, sv);
331 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
332 const MGVTBL* const vtbl = mg->mg_virtual;
333 /* omit GSKIP -- never set here */
335 if (vtbl && vtbl->svt_clear)
336 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
339 restore_magic(INT2PTR(void*, (IV)mgs_ix));
346 Finds the magic pointer for type matching the SV. See C<sv_magic>.
352 Perl_mg_find(pTHX_ const SV *sv, int type)
357 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
358 if (mg->mg_type == type)
368 Copies the magic from one SV to another. See C<sv_magic>.
374 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
378 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
379 const MGVTBL* const vtbl = mg->mg_virtual;
380 if ((mg->mg_flags & MGf_COPY) && vtbl->svt_copy){
381 count += CALL_FPTR(vtbl->svt_copy)(aTHX_ sv, mg, nsv, key, klen);
384 const char type = mg->mg_type;
385 if (isUPPER(type) && type != PERL_MAGIC_uvar) {
387 (type == PERL_MAGIC_tied)
389 : (type == PERL_MAGIC_regdata && mg->mg_obj)
392 toLOWER(type), key, klen);
401 =for apidoc mg_localize
403 Copy some of the magic from an existing SV to new localized version of
404 that SV. Container magic (eg %ENV, $1, tie) gets copied, value magic
405 doesn't (eg taint, pos).
411 Perl_mg_localize(pTHX_ SV *sv, SV *nsv)
415 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
416 const MGVTBL* const vtbl = mg->mg_virtual;
417 switch (mg->mg_type) {
418 /* value magic types: don't copy */
421 case PERL_MAGIC_regex_global:
422 case PERL_MAGIC_nkeys:
423 #ifdef USE_LOCALE_COLLATE
424 case PERL_MAGIC_collxfrm:
427 case PERL_MAGIC_taint:
429 case PERL_MAGIC_vstring:
430 case PERL_MAGIC_utf8:
431 case PERL_MAGIC_substr:
432 case PERL_MAGIC_defelem:
433 case PERL_MAGIC_arylen:
435 case PERL_MAGIC_backref:
436 case PERL_MAGIC_arylen_p:
437 case PERL_MAGIC_rhash:
438 case PERL_MAGIC_symtab:
442 if ((mg->mg_flags & MGf_LOCAL) && vtbl->svt_local)
443 (void)CALL_FPTR(vtbl->svt_local)(aTHX_ nsv, mg);
445 sv_magicext(nsv, mg->mg_obj, mg->mg_type, vtbl,
446 mg->mg_ptr, mg->mg_len);
448 /* container types should remain read-only across localization */
449 SvFLAGS(nsv) |= SvREADONLY(sv);
452 if (SvTYPE(nsv) >= SVt_PVMG && SvMAGIC(nsv)) {
453 SvFLAGS(nsv) |= SvMAGICAL(sv);
463 Free any magic storage used by the SV. See C<sv_magic>.
469 Perl_mg_free(pTHX_ SV *sv)
473 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
474 const MGVTBL* const vtbl = mg->mg_virtual;
475 moremagic = mg->mg_moremagic;
476 if (vtbl && vtbl->svt_free)
477 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
478 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
479 if (mg->mg_len > 0 || mg->mg_type == PERL_MAGIC_utf8)
480 Safefree(mg->mg_ptr);
481 else if (mg->mg_len == HEf_SVKEY)
482 SvREFCNT_dec((SV*)mg->mg_ptr);
484 if (mg->mg_flags & MGf_REFCOUNTED)
485 SvREFCNT_dec(mg->mg_obj);
488 SvMAGIC_set(sv, NULL);
495 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
501 register const REGEXP * const rx = PM_GETRE(PL_curpm);
503 if (mg->mg_obj) { /* @+ */
504 /* return the number possible */
507 I32 paren = rx->lastparen;
509 /* return the last filled */
511 && (rx->startp[paren] == -1 || rx->endp[paren] == -1) )
522 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
526 register const REGEXP * const rx = PM_GETRE(PL_curpm);
528 register const I32 paren = mg->mg_len;
533 if (paren <= (I32)rx->nparens &&
534 (s = rx->startp[paren]) != -1 &&
535 (t = rx->endp[paren]) != -1)
538 if (mg->mg_obj) /* @+ */
543 if (i > 0 && RX_MATCH_UTF8(rx)) {
544 const char * const b = rx->subbeg;
546 i = utf8_length((U8*)b, (U8*)(b+i));
557 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
561 Perl_croak(aTHX_ PL_no_modify);
562 NORETURN_FUNCTION_END;
566 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
571 register const REGEXP *rx;
574 switch (*mg->mg_ptr) {
575 case '1': case '2': case '3': case '4':
576 case '5': case '6': case '7': case '8': case '9': case '&':
577 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
579 paren = atoi(mg->mg_ptr); /* $& is in [0] */
581 if (paren <= (I32)rx->nparens &&
582 (s1 = rx->startp[paren]) != -1 &&
583 (t1 = rx->endp[paren]) != -1)
587 if (i > 0 && RX_MATCH_UTF8(rx)) {
588 const char * const s = rx->subbeg + s1;
593 if (is_utf8_string_loclen((U8*)s, i, &ep, &el))
597 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
601 if (ckWARN(WARN_UNINITIALIZED))
606 if (ckWARN(WARN_UNINITIALIZED))
611 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
612 paren = rx->lastparen;
617 case '\016': /* ^N */
618 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
619 paren = rx->lastcloseparen;
625 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
626 if (rx->startp[0] != -1) {
637 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
638 if (rx->endp[0] != -1) {
639 i = rx->sublen - rx->endp[0];
650 if (!SvPOK(sv) && SvNIOK(sv)) {
658 #define SvRTRIM(sv) STMT_START { \
660 STRLEN len = SvCUR(sv); \
661 char * const p = SvPVX(sv); \
662 while (len > 0 && isSPACE(p[len-1])) \
664 SvCUR_set(sv, len); \
670 Perl_emulate_cop_io(pTHX_ const COP *const c, SV *const sv)
672 if (!(CopHINTS_get(c) & (HINT_LEXICAL_IO_IN|HINT_LEXICAL_IO_OUT)))
673 sv_setsv(sv, &PL_sv_undef);
677 if ((CopHINTS_get(c) & HINT_LEXICAL_IO_IN)) {
678 SV *const value = Perl_refcounted_he_fetch(aTHX_
680 0, "open<", 5, 0, 0);
685 if ((CopHINTS_get(c) & HINT_LEXICAL_IO_OUT)) {
686 SV *const value = Perl_refcounted_he_fetch(aTHX_
688 0, "open>", 5, 0, 0);
696 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
700 register char *s = NULL;
702 const char * const remaining = mg->mg_ptr + 1;
703 const char nextchar = *remaining;
705 switch (*mg->mg_ptr) {
706 case '\001': /* ^A */
707 sv_setsv(sv, PL_bodytarget);
709 case '\003': /* ^C, ^CHILD_ERROR_NATIVE */
710 if (nextchar == '\0') {
711 sv_setiv(sv, (IV)PL_minus_c);
713 else if (strEQ(remaining, "HILD_ERROR_NATIVE")) {
714 sv_setiv(sv, (IV)STATUS_NATIVE);
718 case '\004': /* ^D */
719 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
721 case '\005': /* ^E */
722 if (nextchar == '\0') {
723 #if defined(MACOS_TRADITIONAL)
727 sv_setnv(sv,(double)gMacPerl_OSErr);
728 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
732 # include <descrip.h>
733 # include <starlet.h>
735 $DESCRIPTOR(msgdsc,msg);
736 sv_setnv(sv,(NV) vaxc$errno);
737 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
738 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
743 if (!(_emx_env & 0x200)) { /* Under DOS */
744 sv_setnv(sv, (NV)errno);
745 sv_setpv(sv, errno ? Strerror(errno) : "");
747 if (errno != errno_isOS2) {
748 const int tmp = _syserrno();
749 if (tmp) /* 2nd call to _syserrno() makes it 0 */
752 sv_setnv(sv, (NV)Perl_rc);
753 sv_setpv(sv, os2error(Perl_rc));
757 const DWORD dwErr = GetLastError();
758 sv_setnv(sv, (NV)dwErr);
760 PerlProc_GetOSError(sv, dwErr);
763 sv_setpvn(sv, "", 0);
768 const int saveerrno = errno;
769 sv_setnv(sv, (NV)errno);
770 sv_setpv(sv, errno ? Strerror(errno) : "");
775 SvNOK_on(sv); /* what a wonderful hack! */
777 else if (strEQ(remaining, "NCODING"))
778 sv_setsv(sv, PL_encoding);
780 case '\006': /* ^F */
781 sv_setiv(sv, (IV)PL_maxsysfd);
783 case '\010': /* ^H */
784 sv_setiv(sv, (IV)PL_hints);
786 case '\011': /* ^I */ /* NOT \t in EBCDIC */
788 sv_setpv(sv, PL_inplace);
790 sv_setsv(sv, &PL_sv_undef);
792 case '\017': /* ^O & ^OPEN */
793 if (nextchar == '\0') {
794 sv_setpv(sv, PL_osname);
797 else if (strEQ(remaining, "PEN")) {
798 Perl_emulate_cop_io(aTHX_ &PL_compiling, sv);
802 if (nextchar == '\0') { /* ^P */
803 sv_setiv(sv, (IV)PL_perldb);
804 } else if (strEQ(remaining, "REMATCH")) { /* $^PREMATCH */
805 goto do_prematch_fetch;
806 } else if (strEQ(remaining, "OSTMATCH")) { /* $^POSTMATCH */
807 goto do_postmatch_fetch;
810 case '\023': /* ^S */
811 if (nextchar == '\0') {
812 if (PL_lex_state != LEX_NOTPARSING)
815 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
820 case '\024': /* ^T */
821 if (nextchar == '\0') {
823 sv_setnv(sv, PL_basetime);
825 sv_setiv(sv, (IV)PL_basetime);
828 else if (strEQ(remaining, "AINT"))
829 sv_setiv(sv, PL_tainting
830 ? (PL_taint_warn || PL_unsafe ? -1 : 1)
833 case '\025': /* $^UNICODE, $^UTF8LOCALE, $^UTF8CACHE */
834 if (strEQ(remaining, "NICODE"))
835 sv_setuv(sv, (UV) PL_unicode);
836 else if (strEQ(remaining, "TF8LOCALE"))
837 sv_setuv(sv, (UV) PL_utf8locale);
838 else if (strEQ(remaining, "TF8CACHE"))
839 sv_setiv(sv, (IV) PL_utf8cache);
841 case '\027': /* ^W & $^WARNING_BITS */
842 if (nextchar == '\0')
843 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
844 else if (strEQ(remaining, "ARNING_BITS")) {
845 if (PL_compiling.cop_warnings == pWARN_NONE) {
846 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
848 else if (PL_compiling.cop_warnings == pWARN_STD) {
851 (PL_dowarn & G_WARN_ON) ? WARN_ALLstring : WARN_NONEstring,
855 else if (PL_compiling.cop_warnings == pWARN_ALL) {
856 /* Get the bit mask for $warnings::Bits{all}, because
857 * it could have been extended by warnings::register */
858 HV * const bits=get_hv("warnings::Bits", FALSE);
860 SV ** const bits_all = hv_fetchs(bits, "all", FALSE);
862 sv_setsv(sv, *bits_all);
865 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
869 sv_setpvn(sv, (char *) (PL_compiling.cop_warnings + 1),
870 *PL_compiling.cop_warnings);
875 case '\015': /* $^MATCH */
876 if (strEQ(remaining, "ATCH")) {
877 case '1': case '2': case '3': case '4':
878 case '5': case '6': case '7': case '8': case '9': case '&':
879 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
881 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
882 * XXX Does the new way break anything?
884 paren = atoi(mg->mg_ptr); /* $& is in [0] */
885 CALLREG_NUMBUF(rx,paren,sv);
888 sv_setsv(sv,&PL_sv_undef);
892 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
894 CALLREG_NUMBUF(rx,rx->lastparen,sv);
898 sv_setsv(sv,&PL_sv_undef);
900 case '\016': /* ^N */
901 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
902 if (rx->lastcloseparen) {
903 CALLREG_NUMBUF(rx,rx->lastcloseparen,sv);
908 sv_setsv(sv,&PL_sv_undef);
912 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
913 CALLREG_NUMBUF(rx,-2,sv);
916 sv_setsv(sv,&PL_sv_undef);
920 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
921 CALLREG_NUMBUF(rx,-1,sv);
924 sv_setsv(sv,&PL_sv_undef);
927 if (GvIO(PL_last_in_gv)) {
928 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
933 sv_setiv(sv, (IV)STATUS_CURRENT);
934 #ifdef COMPLEX_STATUS
935 LvTARGOFF(sv) = PL_statusvalue;
936 LvTARGLEN(sv) = PL_statusvalue_vms;
941 if (GvIOp(PL_defoutgv))
942 s = IoTOP_NAME(GvIOp(PL_defoutgv));
946 sv_setpv(sv,GvENAME(PL_defoutgv));
951 if (GvIOp(PL_defoutgv))
952 s = IoFMT_NAME(GvIOp(PL_defoutgv));
954 s = GvENAME(PL_defoutgv);
958 if (GvIOp(PL_defoutgv))
959 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
962 if (GvIOp(PL_defoutgv))
963 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
966 if (GvIOp(PL_defoutgv))
967 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
974 sv_setiv(sv, (IV)CopARYBASE_get(PL_curcop));
977 if (GvIOp(PL_defoutgv))
978 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
984 sv_copypv(sv, PL_ors_sv);
988 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
989 sv_setpv(sv, errno ? Strerror(errno) : "");
992 const int saveerrno = errno;
993 sv_setnv(sv, (NV)errno);
995 if (errno == errno_isOS2 || errno == errno_isOS2_set)
996 sv_setpv(sv, os2error(Perl_rc));
999 sv_setpv(sv, errno ? Strerror(errno) : "");
1004 SvNOK_on(sv); /* what a wonderful hack! */
1007 sv_setiv(sv, (IV)PL_uid);
1010 sv_setiv(sv, (IV)PL_euid);
1013 sv_setiv(sv, (IV)PL_gid);
1016 sv_setiv(sv, (IV)PL_egid);
1018 #ifdef HAS_GETGROUPS
1020 Groups_t *gary = NULL;
1021 I32 i, num_groups = getgroups(0, gary);
1022 Newx(gary, num_groups, Groups_t);
1023 num_groups = getgroups(num_groups, gary);
1024 for (i = 0; i < num_groups; i++)
1025 Perl_sv_catpvf(aTHX_ sv, " %"IVdf, (IV)gary[i]);
1028 (void)SvIOK_on(sv); /* what a wonderful hack! */
1031 #ifndef MACOS_TRADITIONAL
1040 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
1042 struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
1044 if (uf && uf->uf_val)
1045 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
1050 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
1053 STRLEN len = 0, klen;
1054 const char *s = SvOK(sv) ? SvPV_const(sv,len) : "";
1055 const char * const ptr = MgPV_const(mg,klen);
1058 #ifdef DYNAMIC_ENV_FETCH
1059 /* We just undefd an environment var. Is a replacement */
1060 /* waiting in the wings? */
1062 SV ** const valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE);
1064 s = SvOK(*valp) ? SvPV_const(*valp, len) : "";
1068 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
1069 /* And you'll never guess what the dog had */
1070 /* in its mouth... */
1072 MgTAINTEDDIR_off(mg);
1074 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
1075 char pathbuf[256], eltbuf[256], *cp, *elt;
1079 my_strlcpy(eltbuf, s, sizeof(eltbuf));
1081 do { /* DCL$PATH may be a search list */
1082 while (1) { /* as may dev portion of any element */
1083 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
1084 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
1085 cando_by_name(S_IWUSR,0,elt) ) {
1086 MgTAINTEDDIR_on(mg);
1090 if ((cp = strchr(elt, ':')) != NULL)
1092 if (my_trnlnm(elt, eltbuf, j++))
1098 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
1101 if (s && klen == 4 && strEQ(ptr,"PATH")) {
1102 const char * const strend = s + len;
1104 while (s < strend) {
1108 #ifdef VMS /* Hmm. How do we get $Config{path_sep} from C? */
1109 const char path_sep = '|';
1111 const char path_sep = ':';
1113 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
1114 s, strend, path_sep, &i);
1116 if (i >= (I32)sizeof tmpbuf /* too long -- assume the worst */
1118 || !strchr(tmpbuf, ':') /* no colon thus no device name -- assume relative path */
1120 || *tmpbuf != '/' /* no starting slash -- assume relative path */
1122 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
1123 MgTAINTEDDIR_on(mg);
1129 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
1135 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
1137 PERL_UNUSED_ARG(sv);
1138 my_setenv(MgPV_nolen_const(mg),NULL);
1143 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
1146 PERL_UNUSED_ARG(mg);
1148 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1150 if (PL_localizing) {
1153 hv_iterinit((HV*)sv);
1154 while ((entry = hv_iternext((HV*)sv))) {
1156 my_setenv(hv_iterkey(entry, &keylen),
1157 SvPV_nolen_const(hv_iterval((HV*)sv, entry)));
1165 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
1168 PERL_UNUSED_ARG(sv);
1169 PERL_UNUSED_ARG(mg);
1171 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1179 #ifdef HAS_SIGPROCMASK
1181 restore_sigmask(pTHX_ SV *save_sv)
1183 const sigset_t * const ossetp = (const sigset_t *) SvPV_nolen_const( save_sv );
1184 (void)sigprocmask(SIG_SETMASK, ossetp, NULL);
1188 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
1191 /* Are we fetching a signal entry? */
1192 const I32 i = whichsig(MgPV_nolen_const(mg));
1195 sv_setsv(sv,PL_psig_ptr[i]);
1197 Sighandler_t sigstate = rsignal_state(i);
1198 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1199 if (PL_sig_handlers_initted && PL_sig_ignoring[i])
1202 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1203 if (PL_sig_handlers_initted && PL_sig_defaulting[i])
1206 /* cache state so we don't fetch it again */
1207 if(sigstate == (Sighandler_t) SIG_IGN)
1208 sv_setpvs(sv,"IGNORE");
1210 sv_setsv(sv,&PL_sv_undef);
1211 PL_psig_ptr[i] = SvREFCNT_inc_simple_NN(sv);
1218 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
1220 /* XXX Some of this code was copied from Perl_magic_setsig. A little
1221 * refactoring might be in order.
1224 register const char * const s = MgPV_nolen_const(mg);
1225 PERL_UNUSED_ARG(sv);
1228 if (strEQ(s,"__DIE__"))
1230 else if (strEQ(s,"__WARN__") && PL_warnhook != PERL_WARNHOOK_FATAL)
1233 SV *const to_dec = *svp;
1235 SvREFCNT_dec(to_dec);
1239 /* Are we clearing a signal entry? */
1240 const I32 i = whichsig(s);
1242 #ifdef HAS_SIGPROCMASK
1245 /* Avoid having the signal arrive at a bad time, if possible. */
1248 sigprocmask(SIG_BLOCK, &set, &save);
1250 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1251 SAVEFREESV(save_sv);
1252 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1255 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1256 if (!PL_sig_handlers_initted) Perl_csighandler_init();
1258 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1259 PL_sig_defaulting[i] = 1;
1260 (void)rsignal(i, PL_csighandlerp);
1262 (void)rsignal(i, (Sighandler_t) SIG_DFL);
1264 if(PL_psig_name[i]) {
1265 SvREFCNT_dec(PL_psig_name[i]);
1268 if(PL_psig_ptr[i]) {
1269 SV * const to_dec=PL_psig_ptr[i];
1272 SvREFCNT_dec(to_dec);
1282 * The signal handling nomenclature has gotten a bit confusing since the advent of
1283 * safe signals. S_raise_signal only raises signals by analogy with what the
1284 * underlying system's signal mechanism does. It might be more proper to say that
1285 * it defers signals that have already been raised and caught.
1287 * PL_sig_pending and PL_psig_pend likewise do not track signals that are pending
1288 * in the sense of being on the system's signal queue in between raising and delivery.
1289 * They are only pending on Perl's deferral list, i.e., they track deferred signals
1290 * awaiting delivery after the current Perl opcode completes and say nothing about
1291 * signals raised but not yet caught in the underlying signal implementation.
1294 #ifndef SIG_PENDING_DIE_COUNT
1295 # define SIG_PENDING_DIE_COUNT 120
1299 S_raise_signal(pTHX_ int sig)
1302 /* Set a flag to say this signal is pending */
1303 PL_psig_pend[sig]++;
1304 /* And one to say _a_ signal is pending */
1305 if (++PL_sig_pending >= SIG_PENDING_DIE_COUNT)
1306 Perl_croak(aTHX_ "Maximal count of pending signals (%lu) exceeded",
1307 (unsigned long)SIG_PENDING_DIE_COUNT);
1311 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
1312 Perl_csighandler(int sig, ...)
1314 Perl_csighandler(int sig)
1317 #ifdef PERL_GET_SIG_CONTEXT
1318 dTHXa(PERL_GET_SIG_CONTEXT);
1322 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1323 (void) rsignal(sig, PL_csighandlerp);
1324 if (PL_sig_ignoring[sig]) return;
1326 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1327 if (PL_sig_defaulting[sig])
1328 #ifdef KILL_BY_SIGPRC
1329 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1344 (PL_signals & PERL_SIGNALS_UNSAFE_FLAG))
1345 /* Call the perl level handler now--
1346 * with risk we may be in malloc() etc. */
1347 (*PL_sighandlerp)(sig);
1349 S_raise_signal(aTHX_ sig);
1352 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1354 Perl_csighandler_init(void)
1357 if (PL_sig_handlers_initted) return;
1359 for (sig = 1; sig < SIG_SIZE; sig++) {
1360 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1362 PL_sig_defaulting[sig] = 1;
1363 (void) rsignal(sig, PL_csighandlerp);
1365 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1366 PL_sig_ignoring[sig] = 0;
1369 PL_sig_handlers_initted = 1;
1374 Perl_despatch_signals(pTHX)
1379 for (sig = 1; sig < SIG_SIZE; sig++) {
1380 if (PL_psig_pend[sig]) {
1381 PERL_BLOCKSIG_ADD(set, sig);
1382 PL_psig_pend[sig] = 0;
1383 PERL_BLOCKSIG_BLOCK(set);
1384 (*PL_sighandlerp)(sig);
1385 PERL_BLOCKSIG_UNBLOCK(set);
1391 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1396 /* Need to be careful with SvREFCNT_dec(), because that can have side
1397 * effects (due to closures). We must make sure that the new disposition
1398 * is in place before it is called.
1402 #ifdef HAS_SIGPROCMASK
1407 register const char *s = MgPV_const(mg,len);
1409 if (strEQ(s,"__DIE__"))
1411 else if (strEQ(s,"__WARN__"))
1414 Perl_croak(aTHX_ "No such hook: %s", s);
1417 if (*svp != PERL_WARNHOOK_FATAL)
1423 i = whichsig(s); /* ...no, a brick */
1425 if (ckWARN(WARN_SIGNAL))
1426 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
1429 #ifdef HAS_SIGPROCMASK
1430 /* Avoid having the signal arrive at a bad time, if possible. */
1433 sigprocmask(SIG_BLOCK, &set, &save);
1435 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1436 SAVEFREESV(save_sv);
1437 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1440 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1441 if (!PL_sig_handlers_initted) Perl_csighandler_init();
1443 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1444 PL_sig_ignoring[i] = 0;
1446 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1447 PL_sig_defaulting[i] = 0;
1449 SvREFCNT_dec(PL_psig_name[i]);
1450 to_dec = PL_psig_ptr[i];
1451 PL_psig_ptr[i] = SvREFCNT_inc_simple_NN(sv);
1452 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1453 PL_psig_name[i] = newSVpvn(s, len);
1454 SvREADONLY_on(PL_psig_name[i]);
1456 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1458 (void)rsignal(i, PL_csighandlerp);
1459 #ifdef HAS_SIGPROCMASK
1464 *svp = SvREFCNT_inc_simple_NN(sv);
1466 SvREFCNT_dec(to_dec);
1469 s = SvOK(sv) ? SvPV_force(sv,len) : "DEFAULT";
1470 if (strEQ(s,"IGNORE")) {
1472 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1473 PL_sig_ignoring[i] = 1;
1474 (void)rsignal(i, PL_csighandlerp);
1476 (void)rsignal(i, (Sighandler_t) SIG_IGN);
1480 else if (strEQ(s,"DEFAULT") || !*s) {
1482 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1484 PL_sig_defaulting[i] = 1;
1485 (void)rsignal(i, PL_csighandlerp);
1488 (void)rsignal(i, (Sighandler_t) SIG_DFL);
1493 * We should warn if HINT_STRICT_REFS, but without
1494 * access to a known hint bit in a known OP, we can't
1495 * tell whether HINT_STRICT_REFS is in force or not.
1497 if (!strchr(s,':') && !strchr(s,'\''))
1498 Perl_sv_insert(aTHX_ sv, 0, 0, STR_WITH_LEN("main::"));
1500 (void)rsignal(i, PL_csighandlerp);
1502 *svp = SvREFCNT_inc_simple_NN(sv);
1504 #ifdef HAS_SIGPROCMASK
1509 SvREFCNT_dec(to_dec);
1512 #endif /* !PERL_MICRO */
1515 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1518 PERL_UNUSED_ARG(sv);
1519 PERL_UNUSED_ARG(mg);
1520 PL_sub_generation++;
1525 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1528 PERL_UNUSED_ARG(sv);
1529 PERL_UNUSED_ARG(mg);
1530 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1531 PL_amagic_generation++;
1537 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1539 HV * const hv = (HV*)LvTARG(sv);
1541 PERL_UNUSED_ARG(mg);
1544 (void) hv_iterinit(hv);
1545 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1548 while (hv_iternext(hv))
1553 sv_setiv(sv, (IV)i);
1558 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1560 PERL_UNUSED_ARG(mg);
1562 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1567 /* caller is responsible for stack switching/cleanup */
1569 S_magic_methcall(pTHX_ SV *sv, const MAGIC *mg, const char *meth, I32 flags, int n, SV *val)
1576 PUSHs(SvTIED_obj(sv, mg));
1579 if (mg->mg_len >= 0)
1580 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1581 else if (mg->mg_len == HEf_SVKEY)
1582 PUSHs((SV*)mg->mg_ptr);
1584 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1585 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1593 return call_method(meth, flags);
1597 S_magic_methpack(pTHX_ SV *sv, const MAGIC *mg, const char *meth)
1603 PUSHSTACKi(PERLSI_MAGIC);
1605 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1606 sv_setsv(sv, *PL_stack_sp--);
1616 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1619 mg->mg_flags |= MGf_GSKIP;
1620 magic_methpack(sv,mg,"FETCH");
1625 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1629 PUSHSTACKi(PERLSI_MAGIC);
1630 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1637 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1639 return magic_methpack(sv,mg,"DELETE");
1644 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1651 PUSHSTACKi(PERLSI_MAGIC);
1652 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1653 sv = *PL_stack_sp--;
1654 retval = (U32) SvIV(sv)-1;
1663 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1668 PUSHSTACKi(PERLSI_MAGIC);
1670 XPUSHs(SvTIED_obj(sv, mg));
1672 call_method("CLEAR", G_SCALAR|G_DISCARD);
1680 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1683 const char * const meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1687 PUSHSTACKi(PERLSI_MAGIC);
1690 PUSHs(SvTIED_obj(sv, mg));
1695 if (call_method(meth, G_SCALAR))
1696 sv_setsv(key, *PL_stack_sp--);
1705 Perl_magic_existspack(pTHX_ SV *sv, const MAGIC *mg)
1707 return magic_methpack(sv,mg,"EXISTS");
1711 Perl_magic_scalarpack(pTHX_ HV *hv, MAGIC *mg)
1715 SV * const tied = SvTIED_obj((SV*)hv, mg);
1716 HV * const pkg = SvSTASH((SV*)SvRV(tied));
1718 if (!gv_fetchmethod_autoload(pkg, "SCALAR", FALSE)) {
1720 if (HvEITER_get(hv))
1721 /* we are in an iteration so the hash cannot be empty */
1723 /* no xhv_eiter so now use FIRSTKEY */
1724 key = sv_newmortal();
1725 magic_nextpack((SV*)hv, mg, key);
1726 HvEITER_set(hv, NULL); /* need to reset iterator */
1727 return SvOK(key) ? &PL_sv_yes : &PL_sv_no;
1730 /* there is a SCALAR method that we can call */
1732 PUSHSTACKi(PERLSI_MAGIC);
1738 if (call_method("SCALAR", G_SCALAR))
1739 retval = *PL_stack_sp--;
1741 retval = &PL_sv_undef;
1748 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1751 GV * const gv = PL_DBline;
1752 const I32 i = SvTRUE(sv);
1753 SV ** const svp = av_fetch(GvAV(gv),
1754 atoi(MgPV_nolen_const(mg)), FALSE);
1755 if (svp && SvIOKp(*svp)) {
1756 OP * const o = INT2PTR(OP*,SvIVX(*svp));
1758 /* set or clear breakpoint in the relevant control op */
1760 o->op_flags |= OPf_SPECIAL;
1762 o->op_flags &= ~OPf_SPECIAL;
1769 Perl_magic_getarylen(pTHX_ SV *sv, const MAGIC *mg)
1772 const AV * const obj = (AV*)mg->mg_obj;
1774 sv_setiv(sv, AvFILL(obj) + CopARYBASE_get(PL_curcop));
1782 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1785 AV * const obj = (AV*)mg->mg_obj;
1787 av_fill(obj, SvIV(sv) - CopARYBASE_get(PL_curcop));
1789 if (ckWARN(WARN_MISC))
1790 Perl_warner(aTHX_ packWARN(WARN_MISC),
1791 "Attempt to set length of freed array");
1797 Perl_magic_freearylen_p(pTHX_ SV *sv, MAGIC *mg)
1800 PERL_UNUSED_ARG(sv);
1801 /* during global destruction, mg_obj may already have been freed */
1802 if (PL_in_clean_all)
1805 mg = mg_find (mg->mg_obj, PERL_MAGIC_arylen);
1808 /* arylen scalar holds a pointer back to the array, but doesn't own a
1809 reference. Hence the we (the array) are about to go away with it
1810 still pointing at us. Clear its pointer, else it would be pointing
1811 at free memory. See the comment in sv_magic about reference loops,
1812 and why it can't own a reference to us. */
1819 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1822 SV* const lsv = LvTARG(sv);
1823 PERL_UNUSED_ARG(mg);
1825 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1826 MAGIC * const found = mg_find(lsv, PERL_MAGIC_regex_global);
1827 if (found && found->mg_len >= 0) {
1828 I32 i = found->mg_len;
1830 sv_pos_b2u(lsv, &i);
1831 sv_setiv(sv, i + CopARYBASE_get(PL_curcop));
1840 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1843 SV* const lsv = LvTARG(sv);
1849 PERL_UNUSED_ARG(mg);
1851 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1852 found = mg_find(lsv, PERL_MAGIC_regex_global);
1858 #ifdef PERL_OLD_COPY_ON_WRITE
1860 sv_force_normal_flags(lsv, 0);
1862 found = sv_magicext(lsv, NULL, PERL_MAGIC_regex_global, &PL_vtbl_mglob,
1865 else if (!SvOK(sv)) {
1869 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1871 pos = SvIV(sv) - CopARYBASE_get(PL_curcop);
1874 ulen = sv_len_utf8(lsv);
1884 else if (pos > (SSize_t)len)
1889 sv_pos_u2b(lsv, &p, 0);
1893 found->mg_len = pos;
1894 found->mg_flags &= ~MGf_MINMATCH;
1900 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1903 PERL_UNUSED_ARG(mg);
1907 if (isGV_with_GP(sv)) {
1908 /* We're actually already a typeglob, so don't need the stuff below.
1912 gv = gv_fetchsv(sv, GV_ADD, SVt_PVGV);
1917 GvGP(sv) = gp_ref(GvGP(gv));
1922 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1925 SV * const lsv = LvTARG(sv);
1926 const char * const tmps = SvPV_const(lsv,len);
1927 I32 offs = LvTARGOFF(sv);
1928 I32 rem = LvTARGLEN(sv);
1929 PERL_UNUSED_ARG(mg);
1932 sv_pos_u2b(lsv, &offs, &rem);
1933 if (offs > (I32)len)
1935 if (rem + offs > (I32)len)
1937 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1944 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1948 const char * const tmps = SvPV_const(sv, len);
1949 SV * const lsv = LvTARG(sv);
1950 I32 lvoff = LvTARGOFF(sv);
1951 I32 lvlen = LvTARGLEN(sv);
1952 PERL_UNUSED_ARG(mg);
1955 sv_utf8_upgrade(lsv);
1956 sv_pos_u2b(lsv, &lvoff, &lvlen);
1957 sv_insert(lsv, lvoff, lvlen, tmps, len);
1958 LvTARGLEN(sv) = sv_len_utf8(sv);
1961 else if (lsv && SvUTF8(lsv)) {
1963 sv_pos_u2b(lsv, &lvoff, &lvlen);
1964 LvTARGLEN(sv) = len;
1965 utf8 = (char*)bytes_to_utf8((U8*)tmps, &len);
1966 sv_insert(lsv, lvoff, lvlen, utf8, len);
1970 sv_insert(lsv, lvoff, lvlen, tmps, len);
1971 LvTARGLEN(sv) = len;
1979 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1982 PERL_UNUSED_ARG(sv);
1983 TAINT_IF((PL_localizing != 1) && (mg->mg_len & 1));
1988 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1991 PERL_UNUSED_ARG(sv);
1992 /* update taint status unless we're restoring at scope exit */
1993 if (PL_localizing != 2) {
2003 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
2005 SV * const lsv = LvTARG(sv);
2006 PERL_UNUSED_ARG(mg);
2009 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
2017 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
2019 PERL_UNUSED_ARG(mg);
2020 do_vecset(sv); /* XXX slurp this routine */
2025 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
2029 if (LvTARGLEN(sv)) {
2031 SV * const ahv = LvTARG(sv);
2032 HE * const he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
2037 AV* const av = (AV*)LvTARG(sv);
2038 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
2039 targ = AvARRAY(av)[LvTARGOFF(sv)];
2041 if (targ && (targ != &PL_sv_undef)) {
2042 /* somebody else defined it for us */
2043 SvREFCNT_dec(LvTARG(sv));
2044 LvTARG(sv) = SvREFCNT_inc_simple_NN(targ);
2046 SvREFCNT_dec(mg->mg_obj);
2048 mg->mg_flags &= ~MGf_REFCOUNTED;
2053 sv_setsv(sv, targ ? targ : &PL_sv_undef);
2058 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
2060 PERL_UNUSED_ARG(mg);
2064 sv_setsv(LvTARG(sv), sv);
2065 SvSETMAGIC(LvTARG(sv));
2071 Perl_vivify_defelem(pTHX_ SV *sv)
2077 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
2080 SV * const ahv = LvTARG(sv);
2081 HE * const he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
2084 if (!value || value == &PL_sv_undef)
2085 Perl_croak(aTHX_ PL_no_helem_sv, SVfARG(mg->mg_obj));
2088 AV* const av = (AV*)LvTARG(sv);
2089 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
2090 LvTARG(sv) = NULL; /* array can't be extended */
2092 SV* const * const svp = av_fetch(av, LvTARGOFF(sv), TRUE);
2093 if (!svp || (value = *svp) == &PL_sv_undef)
2094 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
2097 SvREFCNT_inc_simple_void(value);
2098 SvREFCNT_dec(LvTARG(sv));
2101 SvREFCNT_dec(mg->mg_obj);
2103 mg->mg_flags &= ~MGf_REFCOUNTED;
2107 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
2109 return Perl_sv_kill_backrefs(aTHX_ sv, (AV*)mg->mg_obj);
2113 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
2115 PERL_UNUSED_CONTEXT;
2122 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
2124 PERL_UNUSED_ARG(mg);
2125 sv_unmagic(sv, PERL_MAGIC_bm);
2132 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
2134 PERL_UNUSED_ARG(mg);
2135 sv_unmagic(sv, PERL_MAGIC_fm);
2141 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
2143 const struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
2145 if (uf && uf->uf_set)
2146 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
2151 Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
2153 PERL_UNUSED_ARG(mg);
2154 sv_unmagic(sv, PERL_MAGIC_qr);
2159 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
2162 regexp * const re = (regexp *)mg->mg_obj;
2163 PERL_UNUSED_ARG(sv);
2169 #ifdef USE_LOCALE_COLLATE
2171 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
2174 * RenE<eacute> Descartes said "I think not."
2175 * and vanished with a faint plop.
2177 PERL_UNUSED_CONTEXT;
2178 PERL_UNUSED_ARG(sv);
2180 Safefree(mg->mg_ptr);
2186 #endif /* USE_LOCALE_COLLATE */
2188 /* Just clear the UTF-8 cache data. */
2190 Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
2192 PERL_UNUSED_CONTEXT;
2193 PERL_UNUSED_ARG(sv);
2194 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
2196 mg->mg_len = -1; /* The mg_len holds the len cache. */
2201 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
2204 register const char *s;
2207 switch (*mg->mg_ptr) {
2208 case '\001': /* ^A */
2209 sv_setsv(PL_bodytarget, sv);
2211 case '\003': /* ^C */
2212 PL_minus_c = (bool)SvIV(sv);
2215 case '\004': /* ^D */
2217 s = SvPV_nolen_const(sv);
2218 PL_debug = get_debug_opts(&s, 0) | DEBUG_TOP_FLAG;
2219 DEBUG_x(dump_all());
2221 PL_debug = (SvIV(sv)) | DEBUG_TOP_FLAG;
2224 case '\005': /* ^E */
2225 if (*(mg->mg_ptr+1) == '\0') {
2226 #ifdef MACOS_TRADITIONAL
2227 gMacPerl_OSErr = SvIV(sv);
2230 set_vaxc_errno(SvIV(sv));
2233 SetLastError( SvIV(sv) );
2236 os2_setsyserrno(SvIV(sv));
2238 /* will anyone ever use this? */
2239 SETERRNO(SvIV(sv), 4);
2245 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
2247 SvREFCNT_dec(PL_encoding);
2248 if (SvOK(sv) || SvGMAGICAL(sv)) {
2249 PL_encoding = newSVsv(sv);
2256 case '\006': /* ^F */
2257 PL_maxsysfd = SvIV(sv);
2259 case '\010': /* ^H */
2260 PL_hints = SvIV(sv);
2262 case '\011': /* ^I */ /* NOT \t in EBCDIC */
2263 Safefree(PL_inplace);
2264 PL_inplace = SvOK(sv) ? savesvpv(sv) : NULL;
2266 case '\017': /* ^O */
2267 if (*(mg->mg_ptr+1) == '\0') {
2268 Safefree(PL_osname);
2271 TAINT_PROPER("assigning to $^O");
2272 PL_osname = savesvpv(sv);
2275 else if (strEQ(mg->mg_ptr, "\017PEN")) {
2277 const char *const start = SvPV(sv, len);
2278 const char *out = (const char*)memchr(start, '\0', len);
2280 struct refcounted_he *tmp_he;
2283 PL_compiling.cop_hints |= HINT_LEXICAL_IO_IN | HINT_LEXICAL_IO_OUT;
2285 |= HINT_LOCALIZE_HH | HINT_LEXICAL_IO_IN | HINT_LEXICAL_IO_OUT;
2287 /* Opening for input is more common than opening for output, so
2288 ensure that hints for input are sooner on linked list. */
2289 tmp = sv_2mortal(out ? newSVpvn(out + 1, start + len - out - 1)
2291 SvFLAGS(tmp) |= SvUTF8(sv);
2294 = Perl_refcounted_he_new(aTHX_ PL_compiling.cop_hints_hash,
2295 sv_2mortal(newSVpvs("open>")), tmp);
2297 /* The UTF-8 setting is carried over */
2298 sv_setpvn(tmp, start, out ? (STRLEN)(out - start) : len);
2300 PL_compiling.cop_hints_hash
2301 = Perl_refcounted_he_new(aTHX_ tmp_he,
2302 sv_2mortal(newSVpvs("open<")), tmp);
2305 case '\020': /* ^P */
2306 PL_perldb = SvIV(sv);
2307 if (PL_perldb && !PL_DBsingle)
2310 case '\024': /* ^T */
2312 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
2314 PL_basetime = (Time_t)SvIV(sv);
2317 case '\025': /* ^UTF8CACHE */
2318 if (strEQ(mg->mg_ptr+1, "TF8CACHE")) {
2319 PL_utf8cache = (signed char) sv_2iv(sv);
2322 case '\027': /* ^W & $^WARNING_BITS */
2323 if (*(mg->mg_ptr+1) == '\0') {
2324 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2326 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
2327 | (i ? G_WARN_ON : G_WARN_OFF) ;
2330 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
2331 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2332 if (!SvPOK(sv) && PL_localizing) {
2333 sv_setpvn(sv, WARN_NONEstring, WARNsize);
2334 PL_compiling.cop_warnings = pWARN_NONE;
2339 int accumulate = 0 ;
2340 int any_fatals = 0 ;
2341 const char * const ptr = SvPV_const(sv, len) ;
2342 for (i = 0 ; i < len ; ++i) {
2343 accumulate |= ptr[i] ;
2344 any_fatals |= (ptr[i] & 0xAA) ;
2347 if (!specialWARN(PL_compiling.cop_warnings))
2348 PerlMemShared_free(PL_compiling.cop_warnings);
2349 PL_compiling.cop_warnings = pWARN_NONE;
2351 /* Yuck. I can't see how to abstract this: */
2352 else if (isWARN_on(((STRLEN *)SvPV_nolen_const(sv)) - 1,
2353 WARN_ALL) && !any_fatals) {
2354 if (!specialWARN(PL_compiling.cop_warnings))
2355 PerlMemShared_free(PL_compiling.cop_warnings);
2356 PL_compiling.cop_warnings = pWARN_ALL;
2357 PL_dowarn |= G_WARN_ONCE ;
2361 const char *const p = SvPV_const(sv, len);
2363 PL_compiling.cop_warnings
2364 = Perl_new_warnings_bitfield(aTHX_ PL_compiling.cop_warnings,
2367 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2368 PL_dowarn |= G_WARN_ONCE ;
2376 if (PL_localizing) {
2377 if (PL_localizing == 1)
2378 SAVESPTR(PL_last_in_gv);
2380 else if (SvOK(sv) && GvIO(PL_last_in_gv))
2381 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
2384 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2385 s = IoTOP_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
2386 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
2389 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2390 s = IoFMT_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
2391 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
2394 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIV(sv));
2397 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIV(sv));
2398 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2399 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
2402 IoPAGE(GvIOp(PL_defoutgv)) = (SvIV(sv));
2406 IO * const io = GvIOp(PL_defoutgv);
2409 if ((SvIV(sv)) == 0)
2410 IoFLAGS(io) &= ~IOf_FLUSH;
2412 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2413 PerlIO *ofp = IoOFP(io);
2415 (void)PerlIO_flush(ofp);
2416 IoFLAGS(io) |= IOf_FLUSH;
2422 SvREFCNT_dec(PL_rs);
2423 PL_rs = newSVsv(sv);
2427 SvREFCNT_dec(PL_ors_sv);
2428 if (SvOK(sv) || SvGMAGICAL(sv)) {
2429 PL_ors_sv = newSVsv(sv);
2437 SvREFCNT_dec(PL_ofs_sv);
2438 if (SvOK(sv) || SvGMAGICAL(sv)) {
2439 PL_ofs_sv = newSVsv(sv);
2446 CopARYBASE_set(&PL_compiling, SvIV(sv));
2449 #ifdef COMPLEX_STATUS
2450 if (PL_localizing == 2) {
2451 PL_statusvalue = LvTARGOFF(sv);
2452 PL_statusvalue_vms = LvTARGLEN(sv);
2456 #ifdef VMSISH_STATUS
2458 STATUS_NATIVE_CHILD_SET((U32)SvIV(sv));
2461 STATUS_UNIX_EXIT_SET(SvIV(sv));
2466 # define PERL_VMS_BANG vaxc$errno
2468 # define PERL_VMS_BANG 0
2470 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
2471 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
2476 if (PL_delaymagic) {
2477 PL_delaymagic |= DM_RUID;
2478 break; /* don't do magic till later */
2481 (void)setruid((Uid_t)PL_uid);
2484 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
2486 #ifdef HAS_SETRESUID
2487 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
2489 if (PL_uid == PL_euid) { /* special case $< = $> */
2491 /* workaround for Darwin's setuid peculiarity, cf [perl #24122] */
2492 if (PL_uid != 0 && PerlProc_getuid() == 0)
2493 (void)PerlProc_setuid(0);
2495 (void)PerlProc_setuid(PL_uid);
2497 PL_uid = PerlProc_getuid();
2498 Perl_croak(aTHX_ "setruid() not implemented");
2503 PL_uid = PerlProc_getuid();
2504 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2508 if (PL_delaymagic) {
2509 PL_delaymagic |= DM_EUID;
2510 break; /* don't do magic till later */
2513 (void)seteuid((Uid_t)PL_euid);
2516 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
2518 #ifdef HAS_SETRESUID
2519 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
2521 if (PL_euid == PL_uid) /* special case $> = $< */
2522 PerlProc_setuid(PL_euid);
2524 PL_euid = PerlProc_geteuid();
2525 Perl_croak(aTHX_ "seteuid() not implemented");
2530 PL_euid = PerlProc_geteuid();
2531 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2535 if (PL_delaymagic) {
2536 PL_delaymagic |= DM_RGID;
2537 break; /* don't do magic till later */
2540 (void)setrgid((Gid_t)PL_gid);
2543 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
2545 #ifdef HAS_SETRESGID
2546 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2548 if (PL_gid == PL_egid) /* special case $( = $) */
2549 (void)PerlProc_setgid(PL_gid);
2551 PL_gid = PerlProc_getgid();
2552 Perl_croak(aTHX_ "setrgid() not implemented");
2557 PL_gid = PerlProc_getgid();
2558 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2561 #ifdef HAS_SETGROUPS
2563 const char *p = SvPV_const(sv, len);
2564 Groups_t *gary = NULL;
2569 for (i = 0; i < NGROUPS; ++i) {
2570 while (*p && !isSPACE(*p))
2577 Newx(gary, i + 1, Groups_t);
2579 Renew(gary, i + 1, Groups_t);
2583 (void)setgroups(i, gary);
2586 #else /* HAS_SETGROUPS */
2588 #endif /* HAS_SETGROUPS */
2589 if (PL_delaymagic) {
2590 PL_delaymagic |= DM_EGID;
2591 break; /* don't do magic till later */
2594 (void)setegid((Gid_t)PL_egid);
2597 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2599 #ifdef HAS_SETRESGID
2600 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2602 if (PL_egid == PL_gid) /* special case $) = $( */
2603 (void)PerlProc_setgid(PL_egid);
2605 PL_egid = PerlProc_getegid();
2606 Perl_croak(aTHX_ "setegid() not implemented");
2611 PL_egid = PerlProc_getegid();
2612 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2615 PL_chopset = SvPV_force(sv,len);
2617 #ifndef MACOS_TRADITIONAL
2619 LOCK_DOLLARZERO_MUTEX;
2620 #ifdef HAS_SETPROCTITLE
2621 /* The BSDs don't show the argv[] in ps(1) output, they
2622 * show a string from the process struct and provide
2623 * the setproctitle() routine to manipulate that. */
2624 if (PL_origalen != 1) {
2625 s = SvPV_const(sv, len);
2626 # if __FreeBSD_version > 410001
2627 /* The leading "-" removes the "perl: " prefix,
2628 * but not the "(perl) suffix from the ps(1)
2629 * output, because that's what ps(1) shows if the
2630 * argv[] is modified. */
2631 setproctitle("-%s", s);
2632 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2633 /* This doesn't really work if you assume that
2634 * $0 = 'foobar'; will wipe out 'perl' from the $0
2635 * because in ps(1) output the result will be like
2636 * sprintf("perl: %s (perl)", s)
2637 * I guess this is a security feature:
2638 * one (a user process) cannot get rid of the original name.
2640 setproctitle("%s", s);
2643 #elif defined(__hpux) && defined(PSTAT_SETCMD)
2644 if (PL_origalen != 1) {
2646 s = SvPV_const(sv, len);
2647 un.pst_command = (char *)s;
2648 pstat(PSTAT_SETCMD, un, len, 0, 0);
2651 if (PL_origalen > 1) {
2652 /* PL_origalen is set in perl_parse(). */
2653 s = SvPV_force(sv,len);
2654 if (len >= (STRLEN)PL_origalen-1) {
2655 /* Longer than original, will be truncated. We assume that
2656 * PL_origalen bytes are available. */
2657 Copy(s, PL_origargv[0], PL_origalen-1, char);
2660 /* Shorter than original, will be padded. */
2662 /* Special case for Mac OS X: see [perl #38868] */
2665 /* Is the space counterintuitive? Yes.
2666 * (You were expecting \0?)
2667 * Does it work? Seems to. (In Linux 2.4.20 at least.)
2669 const int pad = ' ';
2671 Copy(s, PL_origargv[0], len, char);
2672 PL_origargv[0][len] = 0;
2673 memset(PL_origargv[0] + len + 1,
2674 pad, PL_origalen - len - 1);
2676 PL_origargv[0][PL_origalen-1] = 0;
2677 for (i = 1; i < PL_origargc; i++)
2681 UNLOCK_DOLLARZERO_MUTEX;
2689 Perl_whichsig(pTHX_ const char *sig)
2691 register char* const* sigv;
2692 PERL_UNUSED_CONTEXT;
2694 for (sigv = (char* const*)PL_sig_name; *sigv; sigv++)
2695 if (strEQ(sig,*sigv))
2696 return PL_sig_num[sigv - (char* const*)PL_sig_name];
2698 if (strEQ(sig,"CHLD"))
2702 if (strEQ(sig,"CLD"))
2709 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
2710 Perl_sighandler(int sig, ...)
2712 Perl_sighandler(int sig)
2715 #ifdef PERL_GET_SIG_CONTEXT
2716 dTHXa(PERL_GET_SIG_CONTEXT);
2723 SV * const tSv = PL_Sv;
2727 XPV * const tXpv = PL_Xpv;
2729 if (PL_savestack_ix + 15 <= PL_savestack_max)
2731 if (PL_markstack_ptr < PL_markstack_max - 2)
2733 if (PL_scopestack_ix < PL_scopestack_max - 3)
2736 if (!PL_psig_ptr[sig]) {
2737 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
2742 /* Max number of items pushed there is 3*n or 4. We cannot fix
2743 infinity, so we fix 4 (in fact 5): */
2745 PL_savestack_ix += 5; /* Protect save in progress. */
2746 SAVEDESTRUCTOR_X(S_unwind_handler_stack, (void*)&flags);
2749 PL_markstack_ptr++; /* Protect mark. */
2751 PL_scopestack_ix += 1;
2752 /* sv_2cv is too complicated, try a simpler variant first: */
2753 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2754 || SvTYPE(cv) != SVt_PVCV) {
2756 cv = sv_2cv(PL_psig_ptr[sig], &st, &gv, GV_ADD);
2759 if (!cv || !CvROOT(cv)) {
2760 if (ckWARN(WARN_SIGNAL))
2761 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
2762 PL_sig_name[sig], (gv ? GvENAME(gv)
2769 if(PL_psig_name[sig]) {
2770 sv = SvREFCNT_inc_NN(PL_psig_name[sig]);
2772 #if !defined(PERL_IMPLICIT_CONTEXT)
2776 sv = sv_newmortal();
2777 sv_setpv(sv,PL_sig_name[sig]);
2780 PUSHSTACKi(PERLSI_SIGNAL);
2783 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
2785 struct sigaction oact;
2787 if (sigaction(sig, 0, &oact) == 0 && oact.sa_flags & SA_SIGINFO) {
2791 va_start(args, sig);
2792 sip = (siginfo_t*)va_arg(args, siginfo_t*);
2795 SV *rv = newRV_noinc((SV*)sih);
2796 /* The siginfo fields signo, code, errno, pid, uid,
2797 * addr, status, and band are defined by POSIX/SUSv3. */
2798 hv_store(sih, "signo", 5, newSViv(sip->si_signo), 0);
2799 hv_store(sih, "code", 4, newSViv(sip->si_code), 0);
2800 #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. */
2801 hv_store(sih, "errno", 5, newSViv(sip->si_errno), 0);
2802 hv_store(sih, "status", 6, newSViv(sip->si_status), 0);
2803 hv_store(sih, "uid", 3, newSViv(sip->si_uid), 0);
2804 hv_store(sih, "pid", 3, newSViv(sip->si_pid), 0);
2805 hv_store(sih, "addr", 4, newSVuv(PTR2UV(sip->si_addr)), 0);
2806 hv_store(sih, "band", 4, newSViv(sip->si_band), 0);
2810 PUSHs(newSVpv((char *)sip, sizeof(*sip)));
2819 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2822 if (SvTRUE(ERRSV)) {
2824 #ifdef HAS_SIGPROCMASK
2825 /* Handler "died", for example to get out of a restart-able read().
2826 * Before we re-do that on its behalf re-enable the signal which was
2827 * blocked by the system when we entered.
2831 sigaddset(&set,sig);
2832 sigprocmask(SIG_UNBLOCK, &set, NULL);
2834 /* Not clear if this will work */
2835 (void)rsignal(sig, SIG_IGN);
2836 (void)rsignal(sig, PL_csighandlerp);
2838 #endif /* !PERL_MICRO */
2839 Perl_die(aTHX_ NULL);
2843 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2847 PL_scopestack_ix -= 1;
2850 PL_op = myop; /* Apparently not needed... */
2852 PL_Sv = tSv; /* Restore global temporaries. */
2859 S_restore_magic(pTHX_ const void *p)
2862 MGS* const mgs = SSPTR(PTR2IV(p), MGS*);
2863 SV* const sv = mgs->mgs_sv;
2868 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2870 #ifdef PERL_OLD_COPY_ON_WRITE
2871 /* While magic was saved (and off) sv_setsv may well have seen
2872 this SV as a prime candidate for COW. */
2874 sv_force_normal_flags(sv, 0);
2878 SvFLAGS(sv) |= mgs->mgs_flags;
2881 if (SvGMAGICAL(sv)) {
2882 /* downgrade public flags to private,
2883 and discard any other private flags */
2885 const U32 pubflags = SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK);
2887 SvFLAGS(sv) &= ~( pubflags | (SVp_IOK|SVp_NOK|SVp_POK) );
2888 SvFLAGS(sv) |= ( pubflags << PRIVSHIFT );
2893 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2895 /* If we're still on top of the stack, pop us off. (That condition
2896 * will be satisfied if restore_magic was called explicitly, but *not*
2897 * if it's being called via leave_scope.)
2898 * The reason for doing this is that otherwise, things like sv_2cv()
2899 * may leave alloc gunk on the savestack, and some code
2900 * (e.g. sighandler) doesn't expect that...
2902 if (PL_savestack_ix == mgs->mgs_ss_ix)
2904 I32 popval = SSPOPINT;
2905 assert(popval == SAVEt_DESTRUCTOR_X);
2906 PL_savestack_ix -= 2;
2908 assert(popval == SAVEt_ALLOC);
2910 PL_savestack_ix -= popval;
2916 S_unwind_handler_stack(pTHX_ const void *p)
2919 const U32 flags = *(const U32*)p;
2922 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2923 #if !defined(PERL_IMPLICIT_CONTEXT)
2925 SvREFCNT_dec(PL_sig_sv);
2930 =for apidoc magic_sethint
2932 Triggered by a store to %^H, records the key/value pair to
2933 C<PL_compiling.cop_hints_hash>. It is assumed that hints aren't storing
2934 anything that would need a deep copy. Maybe we should warn if we find a
2940 Perl_magic_sethint(pTHX_ SV *sv, MAGIC *mg)
2943 assert(mg->mg_len == HEf_SVKEY);
2945 /* mg->mg_obj isn't being used. If needed, it would be possible to store
2946 an alternative leaf in there, with PL_compiling.cop_hints being used if
2947 it's NULL. If needed for threads, the alternative could lock a mutex,
2948 or take other more complex action. */
2950 /* Something changed in %^H, so it will need to be restored on scope exit.
2951 Doing this here saves a lot of doing it manually in perl code (and
2952 forgetting to do it, and consequent subtle errors. */
2953 PL_hints |= HINT_LOCALIZE_HH;
2954 PL_compiling.cop_hints_hash
2955 = Perl_refcounted_he_new(aTHX_ PL_compiling.cop_hints_hash,
2956 (SV *)mg->mg_ptr, sv);
2961 =for apidoc magic_sethint
2963 Triggered by a delete from %^H, records the key to
2964 C<PL_compiling.cop_hints_hash>.
2969 Perl_magic_clearhint(pTHX_ SV *sv, MAGIC *mg)
2972 PERL_UNUSED_ARG(sv);
2974 assert(mg->mg_len == HEf_SVKEY);
2976 PERL_UNUSED_ARG(sv);
2978 PL_hints |= HINT_LOCALIZE_HH;
2979 PL_compiling.cop_hints_hash
2980 = Perl_refcounted_he_new(aTHX_ PL_compiling.cop_hints_hash,
2981 (SV *)mg->mg_ptr, &PL_sv_placeholder);
2987 * c-indentation-style: bsd
2989 * indent-tabs-mode: t
2992 * ex: set ts=8 sts=4 sw=4 noet: