3 * Copyright (c) 1991-2001, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "Sam sat on the ground and put his head in his hands. 'I wish I had never
12 * come here, and I don't want to see no more magic,' he said, and fell silent."
16 =head1 Magical Functions
23 #if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
32 /* if you only have signal() and it resets on each signal, FAKE_PERSISTENT_SIGNAL_HANDLERS fixes */
33 #if !defined(HAS_SIGACTION) && defined(VMS)
34 # define FAKE_PERSISTENT_SIGNAL_HANDLERS
36 /* if we're doing kill() with sys$sigprc on VMS, FAKE_DEFAULT_SIGNAL_HANDLERS */
37 #if defined(KILL_BY_SIGPRC)
38 # define FAKE_DEFAULT_SIGNAL_HANDLERS
41 static void restore_magic(pTHX_ void *p);
42 static void unwind_handler_stack(pTHX_ void *p);
45 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
53 /* MGS is typedef'ed to struct magic_state in perl.h */
56 S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
59 assert(SvMAGICAL(sv));
61 SAVEDESTRUCTOR_X(restore_magic, INT2PTR(void*, (IV)mgs_ix));
63 mgs = SSPTR(mgs_ix, MGS*);
65 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
66 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
70 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
74 =for apidoc mg_magical
76 Turns on the magical status of an SV. See C<sv_magic>.
82 Perl_mg_magical(pTHX_ SV *sv)
85 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
86 MGVTBL* vtbl = mg->mg_virtual;
88 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
92 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
101 Do magic after a value is retrieved from the SV. See C<sv_magic>.
107 Perl_mg_get(pTHX_ SV *sv)
110 MAGIC *newmg, *head, *cur, *mg;
111 I32 mgs_ix = SSNEW(sizeof(MGS));
113 save_magic(mgs_ix, sv);
115 /* We must call svt_get(sv, mg) for each valid entry in the linked
116 list of magic. svt_get() may delete the current entry, add new
117 magic to the head of the list, or upgrade the SV. AMS 20010810 */
119 newmg = cur = head = mg = SvMAGIC(sv);
121 MGVTBL *vtbl = mg->mg_virtual;
123 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
124 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
125 /* Don't restore the flags for this entry if it was deleted. */
126 if (mg->mg_flags & MGf_GSKIP)
127 (SSPTR(mgs_ix, MGS *))->mgs_flags = 0;
130 mg = mg->mg_moremagic;
133 /* Have we finished with the new entries we saw? Start again
134 where we left off (unless there are more new entries). */
142 /* Were any new entries added? */
143 if (!new && (newmg = SvMAGIC(sv)) != head) {
150 restore_magic(aTHX_ INT2PTR(void *, (IV)mgs_ix));
157 Do magic after a value is assigned to the SV. See C<sv_magic>.
163 Perl_mg_set(pTHX_ SV *sv)
169 mgs_ix = SSNEW(sizeof(MGS));
170 save_magic(mgs_ix, sv);
172 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
173 MGVTBL* vtbl = mg->mg_virtual;
174 nextmg = mg->mg_moremagic; /* it may delete itself */
175 if (mg->mg_flags & MGf_GSKIP) {
176 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
177 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
179 if (vtbl && vtbl->svt_set)
180 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
183 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
188 =for apidoc mg_length
190 Report on the SV's length. See C<sv_magic>.
196 Perl_mg_length(pTHX_ SV *sv)
201 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
202 MGVTBL* vtbl = mg->mg_virtual;
203 if (vtbl && vtbl->svt_len) {
206 mgs_ix = SSNEW(sizeof(MGS));
207 save_magic(mgs_ix, sv);
208 /* omit MGf_GSKIP -- not changed here */
209 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
210 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
217 U8 *s = (U8*)SvPV(sv, len);
218 len = Perl_utf8_length(aTHX_ s, s + len);
226 Perl_mg_size(pTHX_ SV *sv)
231 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
232 MGVTBL* vtbl = mg->mg_virtual;
233 if (vtbl && vtbl->svt_len) {
236 mgs_ix = SSNEW(sizeof(MGS));
237 save_magic(mgs_ix, sv);
238 /* omit MGf_GSKIP -- not changed here */
239 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
240 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
247 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
252 Perl_croak(aTHX_ "Size magic not implemented");
261 Clear something magical that the SV represents. See C<sv_magic>.
267 Perl_mg_clear(pTHX_ SV *sv)
272 mgs_ix = SSNEW(sizeof(MGS));
273 save_magic(mgs_ix, sv);
275 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
276 MGVTBL* vtbl = mg->mg_virtual;
277 /* omit GSKIP -- never set here */
279 if (vtbl && vtbl->svt_clear)
280 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
283 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
290 Finds the magic pointer for type matching the SV. See C<sv_magic>.
296 Perl_mg_find(pTHX_ SV *sv, int type)
301 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
302 if (mg->mg_type == type)
311 Copies the magic from one SV to another. See C<sv_magic>.
317 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
321 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
322 if (isUPPER(mg->mg_type)) {
324 mg->mg_type == PERL_MAGIC_tied ? SvTIED_obj(sv, mg) :
325 (mg->mg_type == PERL_MAGIC_regdata && mg->mg_obj)
327 toLOWER(mg->mg_type), key, klen);
337 Free any magic storage used by the SV. See C<sv_magic>.
343 Perl_mg_free(pTHX_ SV *sv)
347 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
348 MGVTBL* vtbl = mg->mg_virtual;
349 moremagic = mg->mg_moremagic;
350 if (vtbl && vtbl->svt_free)
351 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
352 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
354 Safefree(mg->mg_ptr);
355 else if (mg->mg_len == HEf_SVKEY)
356 SvREFCNT_dec((SV*)mg->mg_ptr);
358 if (mg->mg_flags & MGf_REFCOUNTED)
359 SvREFCNT_dec(mg->mg_obj);
367 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
372 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
376 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
377 if (mg->mg_obj) /* @+ */
380 return rx->lastparen;
387 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
395 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
399 if (paren <= rx->nparens &&
400 (s = rx->startp[paren]) != -1 &&
401 (t = rx->endp[paren]) != -1)
403 if (mg->mg_obj) /* @+ */
408 if (i > 0 && PL_reg_match_utf8) {
409 char *b = rx->subbeg;
411 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
421 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
423 Perl_croak(aTHX_ PL_no_modify);
429 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
436 switch (*mg->mg_ptr) {
437 case '1': case '2': case '3': case '4':
438 case '5': case '6': case '7': case '8': case '9': case '&':
439 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
441 paren = atoi(mg->mg_ptr); /* $& is in [0] */
443 if (paren <= rx->nparens &&
444 (s1 = rx->startp[paren]) != -1 &&
445 (t1 = rx->endp[paren]) != -1)
449 if (i > 0 && PL_reg_match_utf8) {
450 char *s = rx->subbeg + s1;
451 char *send = rx->subbeg + t1;
454 if (is_utf8_string((U8*)s, i))
455 i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send);
458 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
462 if (ckWARN(WARN_UNINITIALIZED))
467 if (ckWARN(WARN_UNINITIALIZED))
472 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
473 paren = rx->lastparen;
478 case '\016': /* ^N */
479 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
480 paren = rx->lastcloseparen;
486 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
487 if (rx->startp[0] != -1) {
498 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
499 if (rx->endp[0] != -1) {
500 i = rx->sublen - rx->endp[0];
511 if (!SvPOK(sv) && SvNIOK(sv)) {
521 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
528 switch (*mg->mg_ptr) {
529 case '\001': /* ^A */
530 sv_setsv(sv, PL_bodytarget);
532 case '\003': /* ^C */
533 sv_setiv(sv, (IV)PL_minus_c);
536 case '\004': /* ^D */
537 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
538 #if defined(YYDEBUG) && defined(DEBUGGING)
539 PL_yydebug = DEBUG_p_TEST;
542 case '\005': /* ^E */
543 if (*(mg->mg_ptr+1) == '\0') {
544 #ifdef MACOS_TRADITIONAL
548 sv_setnv(sv,(double)gMacPerl_OSErr);
549 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
554 # include <descrip.h>
555 # include <starlet.h>
557 $DESCRIPTOR(msgdsc,msg);
558 sv_setnv(sv,(NV) vaxc$errno);
559 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
560 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
566 if (!(_emx_env & 0x200)) { /* Under DOS */
567 sv_setnv(sv, (NV)errno);
568 sv_setpv(sv, errno ? Strerror(errno) : "");
570 if (errno != errno_isOS2) {
571 int tmp = _syserrno();
572 if (tmp) /* 2nd call to _syserrno() makes it 0 */
575 sv_setnv(sv, (NV)Perl_rc);
576 sv_setpv(sv, os2error(Perl_rc));
581 DWORD dwErr = GetLastError();
582 sv_setnv(sv, (NV)dwErr);
585 PerlProc_GetOSError(sv, dwErr);
592 sv_setnv(sv, (NV)errno);
593 sv_setpv(sv, errno ? Strerror(errno) : "");
598 SvNOK_on(sv); /* what a wonderful hack! */
600 else if (strEQ(mg->mg_ptr+1, "NCODING"))
601 sv_setsv(sv, PL_encoding);
603 case '\006': /* ^F */
604 sv_setiv(sv, (IV)PL_maxsysfd);
606 case '\010': /* ^H */
607 sv_setiv(sv, (IV)PL_hints);
609 case '\011': /* ^I */ /* NOT \t in EBCDIC */
611 sv_setpv(sv, PL_inplace);
613 sv_setsv(sv, &PL_sv_undef);
615 case '\017': /* ^O & ^OPEN */
616 if (*(mg->mg_ptr+1) == '\0')
617 sv_setpv(sv, PL_osname);
618 else if (strEQ(mg->mg_ptr, "\017PEN")) {
619 if (!PL_compiling.cop_io)
620 sv_setsv(sv, &PL_sv_undef);
622 sv_setsv(sv, PL_compiling.cop_io);
626 case '\020': /* ^P */
627 sv_setiv(sv, (IV)PL_perldb);
629 case '\023': /* ^S */
631 if (PL_lex_state != LEX_NOTPARSING)
634 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
639 case '\024': /* ^T */
640 if (*(mg->mg_ptr+1) == '\0') {
642 sv_setnv(sv, PL_basetime);
644 sv_setiv(sv, (IV)PL_basetime);
647 else if (strEQ(mg->mg_ptr, "\024AINT"))
648 sv_setiv(sv, PL_tainting);
650 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
651 if (*(mg->mg_ptr+1) == '\0')
652 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
653 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
654 if (PL_compiling.cop_warnings == pWARN_NONE ||
655 PL_compiling.cop_warnings == pWARN_STD)
657 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
659 else if (PL_compiling.cop_warnings == pWARN_ALL) {
660 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
663 sv_setsv(sv, PL_compiling.cop_warnings);
667 else if (strEQ(mg->mg_ptr+1, "IDE_SYSTEM_CALLS"))
668 sv_setiv(sv, (IV)PL_widesyscalls);
670 case '1': case '2': case '3': case '4':
671 case '5': case '6': case '7': case '8': case '9': case '&':
672 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
676 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
677 * XXX Does the new way break anything?
679 paren = atoi(mg->mg_ptr); /* $& is in [0] */
681 if (paren <= rx->nparens &&
682 (s1 = rx->startp[paren]) != -1 &&
683 (t1 = rx->endp[paren]) != -1)
692 bool was_tainted = FALSE;
694 was_tainted = PL_tainted;
698 if (PL_reg_match_utf8 && is_utf8_string((U8*)s, i))
703 PL_tainted = (was_tainted || RX_MATCH_TAINTED(rx));
708 sv_setsv(sv,&PL_sv_undef);
711 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
712 paren = rx->lastparen;
716 sv_setsv(sv,&PL_sv_undef);
718 case '\016': /* ^N */
719 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
720 paren = rx->lastcloseparen;
724 sv_setsv(sv,&PL_sv_undef);
727 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
728 if ((s = rx->subbeg) && rx->startp[0] != -1) {
733 sv_setsv(sv,&PL_sv_undef);
736 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
737 if (rx->subbeg && rx->endp[0] != -1) {
738 s = rx->subbeg + rx->endp[0];
739 i = rx->sublen - rx->endp[0];
743 sv_setsv(sv,&PL_sv_undef);
747 if (GvIO(PL_last_in_gv)) {
748 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
754 sv_setiv(sv, (IV)STATUS_CURRENT);
755 #ifdef COMPLEX_STATUS
756 LvTARGOFF(sv) = PL_statusvalue;
757 LvTARGLEN(sv) = PL_statusvalue_vms;
762 s = IoTOP_NAME(GvIOp(PL_defoutgv));
766 sv_setpv(sv,GvENAME(PL_defoutgv));
771 s = IoFMT_NAME(GvIOp(PL_defoutgv));
773 s = GvENAME(PL_defoutgv);
778 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
781 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
784 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
792 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
795 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
801 sv_setpv(sv,SvPVX(PL_ors_sv));
804 sv_setpv(sv,PL_ofmt);
808 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
809 sv_setpv(sv, errno ? Strerror(errno) : "");
812 int saveerrno = errno;
813 sv_setnv(sv, (NV)errno);
815 if (errno == errno_isOS2 || errno == errno_isOS2_set)
816 sv_setpv(sv, os2error(Perl_rc));
819 sv_setpv(sv, errno ? Strerror(errno) : "");
823 SvNOK_on(sv); /* what a wonderful hack! */
826 sv_setiv(sv, (IV)PL_uid);
829 sv_setiv(sv, (IV)PL_euid);
832 sv_setiv(sv, (IV)PL_gid);
834 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
838 sv_setiv(sv, (IV)PL_egid);
840 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
845 Groups_t gary[NGROUPS];
846 i = getgroups(NGROUPS,gary);
848 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
851 (void)SvIOK_on(sv); /* what a wonderful hack! */
855 #ifndef MACOS_TRADITIONAL
859 #ifdef USE_5005THREADS
861 sv_setsv(sv, thr->errsv);
863 #endif /* USE_5005THREADS */
869 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
871 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
873 if (uf && uf->uf_val)
874 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
879 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
889 #ifdef DYNAMIC_ENV_FETCH
890 /* We just undefd an environment var. Is a replacement */
891 /* waiting in the wings? */
894 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
895 s = SvPV(*valp, len);
899 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
900 /* And you'll never guess what the dog had */
901 /* in its mouth... */
903 MgTAINTEDDIR_off(mg);
905 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
906 char pathbuf[256], eltbuf[256], *cp, *elt = s;
910 do { /* DCL$PATH may be a search list */
911 while (1) { /* as may dev portion of any element */
912 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
913 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
914 cando_by_name(S_IWUSR,0,elt) ) {
919 if ((cp = strchr(elt, ':')) != Nullch)
921 if (my_trnlnm(elt, eltbuf, j++))
927 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
930 if (s && klen == 4 && strEQ(ptr,"PATH")) {
931 char *strend = s + len;
937 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
940 if (i >= sizeof tmpbuf /* too long -- assume the worst */
942 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
949 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
955 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
958 my_setenv(MgPV(mg,n_a),Nullch);
963 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
966 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
971 magic_clear_all_env(sv,mg);
972 hv_iterinit((HV*)sv);
973 while ((entry = hv_iternext((HV*)sv))) {
975 my_setenv(hv_iterkey(entry, &keylen),
976 SvPV(hv_iterval((HV*)sv, entry), n_a));
984 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
986 #if defined(VMS) || defined(EPOC)
987 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
989 # if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
992 # ifdef USE_ENVIRON_ARRAY
993 # ifndef PERL_USE_SAFE_PUTENV
996 if (environ == PL_origenviron)
997 environ = (char**)safesysmalloc(sizeof(char*));
999 for (i = 0; environ[i]; i++)
1000 safesysfree(environ[i]);
1001 # endif /* PERL_USE_SAFE_PUTENV */
1003 environ[0] = Nullch;
1005 # endif /* USE_ENVIRON_ARRAY */
1006 # endif /* PERL_IMPLICIT_SYS || WIN32 */
1007 #endif /* VMS || EPC */
1011 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS)||defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1012 static int sig_handlers_initted = 0;
1014 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1015 static int sig_ignoring[SIG_SIZE]; /* which signals we are ignoring */
1017 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1018 static int sig_defaulting[SIG_SIZE];
1023 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
1027 /* Are we fetching a signal entry? */
1028 i = whichsig(MgPV(mg,n_a));
1031 sv_setsv(sv,PL_psig_ptr[i]);
1033 Sighandler_t sigstate;
1034 sigstate = rsignal_state(i);
1035 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1036 if (sig_handlers_initted && sig_ignoring[i]) sigstate = SIG_IGN;
1038 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1039 if (sig_handlers_initted && sig_defaulting[i]) sigstate = SIG_DFL;
1041 /* cache state so we don't fetch it again */
1042 if(sigstate == SIG_IGN)
1043 sv_setpv(sv,"IGNORE");
1045 sv_setsv(sv,&PL_sv_undef);
1046 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1053 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
1057 /* Are we clearing a signal entry? */
1058 i = whichsig(MgPV(mg,n_a));
1060 if(PL_psig_ptr[i]) {
1061 SvREFCNT_dec(PL_psig_ptr[i]);
1064 if(PL_psig_name[i]) {
1065 SvREFCNT_dec(PL_psig_name[i]);
1073 Perl_raise_signal(pTHX_ int sig)
1075 /* Set a flag to say this signal is pending */
1076 PL_psig_pend[sig]++;
1077 /* And one to say _a_ signal is pending */
1082 Perl_csighandler(int sig)
1084 #ifdef PERL_GET_SIG_CONTEXT
1085 dTHXa(PERL_GET_SIG_CONTEXT);
1089 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1090 (void) rsignal(sig, &Perl_csighandler);
1091 if (sig_ignoring[sig]) return;
1093 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1094 if (sig_defaulting[sig])
1095 #ifdef KILL_BY_SIGPRC
1096 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1102 #ifdef PERL_OLD_SIGNALS
1103 /* Call the perl level handler now with risk we may be in malloc() etc. */
1104 (*PL_sighandlerp)(sig);
1106 Perl_raise_signal(aTHX_ sig);
1110 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1112 Perl_csighandler_init(void)
1115 if (sig_handlers_initted) return;
1117 for (sig = 1; sig < SIG_SIZE; sig++) {
1118 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1120 sig_defaulting[sig] = 1;
1121 (void) rsignal(sig, &Perl_csighandler);
1123 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1124 sig_ignoring[sig] = 0;
1127 sig_handlers_initted = 1;
1132 Perl_despatch_signals(pTHX)
1136 for (sig = 1; sig < SIG_SIZE; sig++) {
1137 if (PL_psig_pend[sig]) {
1138 PL_psig_pend[sig] = 0;
1139 (*PL_sighandlerp)(sig);
1145 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1154 if (strEQ(s,"__DIE__"))
1156 else if (strEQ(s,"__WARN__"))
1159 Perl_croak(aTHX_ "No such hook: %s", s);
1167 i = whichsig(s); /* ...no, a brick */
1169 if (ckWARN(WARN_SIGNAL))
1170 Perl_warner(aTHX_ WARN_SIGNAL, "No such signal: SIG%s", s);
1173 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1174 if (!sig_handlers_initted) Perl_csighandler_init();
1176 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1177 sig_ignoring[i] = 0;
1179 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1180 sig_defaulting[i] = 0;
1182 SvREFCNT_dec(PL_psig_name[i]);
1183 SvREFCNT_dec(PL_psig_ptr[i]);
1184 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1185 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1186 PL_psig_name[i] = newSVpvn(s, len);
1187 SvREADONLY_on(PL_psig_name[i]);
1189 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1191 (void)rsignal(i, &Perl_csighandler);
1193 *svp = SvREFCNT_inc(sv);
1196 s = SvPV_force(sv,len);
1197 if (strEQ(s,"IGNORE")) {
1199 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1200 sig_ignoring[i] = 1;
1201 (void)rsignal(i, &Perl_csighandler);
1203 (void)rsignal(i, SIG_IGN);
1208 else if (strEQ(s,"DEFAULT") || !*s) {
1210 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1212 sig_defaulting[i] = 1;
1213 (void)rsignal(i, &Perl_csighandler);
1216 (void)rsignal(i, SIG_DFL);
1223 * We should warn if HINT_STRICT_REFS, but without
1224 * access to a known hint bit in a known OP, we can't
1225 * tell whether HINT_STRICT_REFS is in force or not.
1227 if (!strchr(s,':') && !strchr(s,'\''))
1228 sv_insert(sv, 0, 0, "main::", 6);
1230 (void)rsignal(i, &Perl_csighandler);
1232 *svp = SvREFCNT_inc(sv);
1236 #endif /* !PERL_MICRO */
1239 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1241 PL_sub_generation++;
1246 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1248 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1249 PL_amagic_generation++;
1255 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1257 HV *hv = (HV*)LvTARG(sv);
1261 (void) hv_iterinit(hv);
1262 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1265 while (hv_iternext(hv))
1270 sv_setiv(sv, (IV)i);
1275 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1278 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1283 /* caller is responsible for stack switching/cleanup */
1285 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1291 PUSHs(SvTIED_obj(sv, mg));
1294 if (mg->mg_len >= 0)
1295 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1296 else if (mg->mg_len == HEf_SVKEY)
1297 PUSHs((SV*)mg->mg_ptr);
1299 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1300 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1308 return call_method(meth, flags);
1312 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1318 PUSHSTACKi(PERLSI_MAGIC);
1320 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1321 sv_setsv(sv, *PL_stack_sp--);
1331 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1333 magic_methpack(sv,mg,"FETCH");
1335 mg->mg_flags |= MGf_GSKIP;
1340 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1344 PUSHSTACKi(PERLSI_MAGIC);
1345 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1352 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1354 return magic_methpack(sv,mg,"DELETE");
1359 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1366 PUSHSTACKi(PERLSI_MAGIC);
1367 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1368 sv = *PL_stack_sp--;
1369 retval = (U32) SvIV(sv)-1;
1378 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1383 PUSHSTACKi(PERLSI_MAGIC);
1385 XPUSHs(SvTIED_obj(sv, mg));
1387 call_method("CLEAR", G_SCALAR|G_DISCARD);
1394 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1397 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1401 PUSHSTACKi(PERLSI_MAGIC);
1404 PUSHs(SvTIED_obj(sv, mg));
1409 if (call_method(meth, G_SCALAR))
1410 sv_setsv(key, *PL_stack_sp--);
1419 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1421 return magic_methpack(sv,mg,"EXISTS");
1425 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1435 svp = av_fetch(GvAV(gv),
1436 atoi(MgPV(mg,n_a)), FALSE);
1437 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
1443 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1445 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1450 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1452 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1457 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1459 SV* lsv = LvTARG(sv);
1461 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1462 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1463 if (mg && mg->mg_len >= 0) {
1466 sv_pos_b2u(lsv, &i);
1467 sv_setiv(sv, i + PL_curcop->cop_arybase);
1476 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1478 SV* lsv = LvTARG(sv);
1485 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1486 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1490 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1491 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1493 else if (!SvOK(sv)) {
1497 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1499 pos = SvIV(sv) - PL_curcop->cop_arybase;
1502 ulen = sv_len_utf8(lsv);
1517 sv_pos_u2b(lsv, &p, 0);
1522 mg->mg_flags &= ~MGf_MINMATCH;
1528 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1530 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1532 gv_efullname3(sv,((GV*)sv), "*");
1536 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1541 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1550 if (*s == '*' && s[1])
1552 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1557 GvGP(sv) = gp_ref(GvGP(gv));
1562 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1565 SV *lsv = LvTARG(sv);
1566 char *tmps = SvPV(lsv,len);
1567 I32 offs = LvTARGOFF(sv);
1568 I32 rem = LvTARGLEN(sv);
1571 sv_pos_u2b(lsv, &offs, &rem);
1574 if (rem + offs > len)
1576 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1583 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1586 char *tmps = SvPV(sv, len);
1587 SV *lsv = LvTARG(sv);
1588 I32 lvoff = LvTARGOFF(sv);
1589 I32 lvlen = LvTARGLEN(sv);
1592 sv_utf8_upgrade(lsv);
1593 sv_pos_u2b(lsv, &lvoff, &lvlen);
1594 sv_insert(lsv, lvoff, lvlen, tmps, len);
1597 else if (lsv && SvUTF8(lsv)) {
1598 sv_pos_u2b(lsv, &lvoff, &lvlen);
1599 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
1600 sv_insert(lsv, lvoff, lvlen, tmps, len);
1604 sv_insert(lsv, lvoff, lvlen, tmps, len);
1610 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1612 TAINT_IF((mg->mg_len & 1) ||
1613 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1618 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1620 if (PL_localizing) {
1621 if (PL_localizing == 1)
1626 else if (PL_tainted)
1634 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1636 SV *lsv = LvTARG(sv);
1643 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1648 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1650 do_vecset(sv); /* XXX slurp this routine */
1655 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1658 if (LvTARGLEN(sv)) {
1660 SV *ahv = LvTARG(sv);
1661 if (SvTYPE(ahv) == SVt_PVHV) {
1662 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1667 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1673 AV* av = (AV*)LvTARG(sv);
1674 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1675 targ = AvARRAY(av)[LvTARGOFF(sv)];
1677 if (targ && targ != &PL_sv_undef) {
1678 /* somebody else defined it for us */
1679 SvREFCNT_dec(LvTARG(sv));
1680 LvTARG(sv) = SvREFCNT_inc(targ);
1682 SvREFCNT_dec(mg->mg_obj);
1683 mg->mg_obj = Nullsv;
1684 mg->mg_flags &= ~MGf_REFCOUNTED;
1689 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1694 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1699 sv_setsv(LvTARG(sv), sv);
1700 SvSETMAGIC(LvTARG(sv));
1706 Perl_vivify_defelem(pTHX_ SV *sv)
1711 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
1714 SV *ahv = LvTARG(sv);
1716 if (SvTYPE(ahv) == SVt_PVHV) {
1717 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1722 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
1726 if (!value || value == &PL_sv_undef)
1727 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1730 AV* av = (AV*)LvTARG(sv);
1731 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1732 LvTARG(sv) = Nullsv; /* array can't be extended */
1734 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1735 if (!svp || (value = *svp) == &PL_sv_undef)
1736 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1739 (void)SvREFCNT_inc(value);
1740 SvREFCNT_dec(LvTARG(sv));
1743 SvREFCNT_dec(mg->mg_obj);
1744 mg->mg_obj = Nullsv;
1745 mg->mg_flags &= ~MGf_REFCOUNTED;
1749 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1751 AV *av = (AV*)mg->mg_obj;
1752 SV **svp = AvARRAY(av);
1753 I32 i = AvFILLp(av);
1755 if (svp[i] && svp[i] != &PL_sv_undef) {
1756 if (!SvWEAKREF(svp[i]))
1757 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1758 /* XXX Should we check that it hasn't changed? */
1760 (void)SvOK_off(svp[i]);
1761 SvWEAKREF_off(svp[i]);
1762 svp[i] = &PL_sv_undef;
1770 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1778 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1780 sv_unmagic(sv, PERL_MAGIC_bm);
1786 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1788 sv_unmagic(sv, PERL_MAGIC_fm);
1794 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1796 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1798 if (uf && uf->uf_set)
1799 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
1804 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1806 regexp *re = (regexp *)mg->mg_obj;
1811 #ifdef USE_LOCALE_COLLATE
1813 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1816 * RenE<eacute> Descartes said "I think not."
1817 * and vanished with a faint plop.
1820 Safefree(mg->mg_ptr);
1826 #endif /* USE_LOCALE_COLLATE */
1829 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1834 switch (*mg->mg_ptr) {
1835 case '\001': /* ^A */
1836 sv_setsv(PL_bodytarget, sv);
1838 case '\003': /* ^C */
1839 PL_minus_c = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1842 case '\004': /* ^D */
1843 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
1844 DEBUG_x(dump_all());
1846 case '\005': /* ^E */
1847 if (*(mg->mg_ptr+1) == '\0') {
1848 #ifdef MACOS_TRADITIONAL
1849 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1852 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1855 SetLastError( SvIV(sv) );
1858 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1860 /* will anyone ever use this? */
1861 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1867 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
1869 sv_setsv(PL_encoding, sv);
1871 PL_encoding = newSVsv(sv);
1873 case '\006': /* ^F */
1874 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1876 case '\010': /* ^H */
1877 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1879 case '\011': /* ^I */ /* NOT \t in EBCDIC */
1881 Safefree(PL_inplace);
1883 PL_inplace = savepv(SvPV(sv,len));
1885 PL_inplace = Nullch;
1887 case '\017': /* ^O */
1888 if (*(mg->mg_ptr+1) == '\0') {
1890 Safefree(PL_osname);
1892 PL_osname = savepv(SvPV(sv,len));
1896 else if (strEQ(mg->mg_ptr, "\017PEN")) {
1897 if (!PL_compiling.cop_io)
1898 PL_compiling.cop_io = newSVsv(sv);
1900 sv_setsv(PL_compiling.cop_io,sv);
1903 case '\020': /* ^P */
1904 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1905 if (PL_perldb && !PL_DBsingle)
1908 case '\024': /* ^T */
1910 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1912 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1915 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
1916 if (*(mg->mg_ptr+1) == '\0') {
1917 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1918 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1919 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1920 | (i ? G_WARN_ON : G_WARN_OFF) ;
1923 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
1924 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1925 if (!SvPOK(sv) && PL_localizing) {
1926 sv_setpvn(sv, WARN_NONEstring, WARNsize);
1927 PL_compiling.cop_warnings = pWARN_NONE;
1932 int accumulate = 0 ;
1933 int any_fatals = 0 ;
1934 char * ptr = (char*)SvPV(sv, len) ;
1935 for (i = 0 ; i < len ; ++i) {
1936 accumulate |= ptr[i] ;
1937 any_fatals |= (ptr[i] & 0xAA) ;
1940 PL_compiling.cop_warnings = pWARN_NONE;
1941 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
1942 PL_compiling.cop_warnings = pWARN_ALL;
1943 PL_dowarn |= G_WARN_ONCE ;
1946 if (specialWARN(PL_compiling.cop_warnings))
1947 PL_compiling.cop_warnings = newSVsv(sv) ;
1949 sv_setsv(PL_compiling.cop_warnings, sv);
1950 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1951 PL_dowarn |= G_WARN_ONCE ;
1957 else if (strEQ(mg->mg_ptr+1, "IDE_SYSTEM_CALLS"))
1958 PL_widesyscalls = SvTRUE(sv);
1961 if (PL_localizing) {
1962 if (PL_localizing == 1)
1963 SAVESPTR(PL_last_in_gv);
1965 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1966 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
1969 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
1970 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1971 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1974 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
1975 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1976 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1979 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1982 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1983 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1984 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
1987 IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1991 IO *io = GvIOp(PL_defoutgv);
1994 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1995 IoFLAGS(io) &= ~IOf_FLUSH;
1997 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1998 PerlIO *ofp = IoOFP(io);
2000 (void)PerlIO_flush(ofp);
2001 IoFLAGS(io) |= IOf_FLUSH;
2007 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2008 PL_multiline = (i != 0);
2011 SvREFCNT_dec(PL_rs);
2012 PL_rs = newSVsv(sv);
2016 SvREFCNT_dec(PL_ors_sv);
2017 if (SvOK(sv) || SvGMAGICAL(sv)) {
2018 PL_ors_sv = newSVsv(sv);
2026 SvREFCNT_dec(PL_ofs_sv);
2027 if (SvOK(sv) || SvGMAGICAL(sv)) {
2028 PL_ofs_sv = newSVsv(sv);
2037 PL_ofmt = savepv(SvPV(sv,len));
2040 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2043 #ifdef COMPLEX_STATUS
2044 if (PL_localizing == 2) {
2045 PL_statusvalue = LvTARGOFF(sv);
2046 PL_statusvalue_vms = LvTARGLEN(sv);
2050 #ifdef VMSISH_STATUS
2052 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
2055 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2058 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
2059 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
2062 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2063 if (PL_delaymagic) {
2064 PL_delaymagic |= DM_RUID;
2065 break; /* don't do magic till later */
2068 (void)setruid((Uid_t)PL_uid);
2071 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
2073 #ifdef HAS_SETRESUID
2074 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
2076 if (PL_uid == PL_euid) /* special case $< = $> */
2077 (void)PerlProc_setuid(PL_uid);
2079 PL_uid = PerlProc_getuid();
2080 Perl_croak(aTHX_ "setruid() not implemented");
2085 PL_uid = PerlProc_getuid();
2086 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2089 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2090 if (PL_delaymagic) {
2091 PL_delaymagic |= DM_EUID;
2092 break; /* don't do magic till later */
2095 (void)seteuid((Uid_t)PL_euid);
2098 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
2100 #ifdef HAS_SETRESUID
2101 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
2103 if (PL_euid == PL_uid) /* special case $> = $< */
2104 PerlProc_setuid(PL_euid);
2106 PL_euid = PerlProc_geteuid();
2107 Perl_croak(aTHX_ "seteuid() not implemented");
2112 PL_euid = PerlProc_geteuid();
2113 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2116 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2117 if (PL_delaymagic) {
2118 PL_delaymagic |= DM_RGID;
2119 break; /* don't do magic till later */
2122 (void)setrgid((Gid_t)PL_gid);
2125 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
2127 #ifdef HAS_SETRESGID
2128 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2130 if (PL_gid == PL_egid) /* special case $( = $) */
2131 (void)PerlProc_setgid(PL_gid);
2133 PL_gid = PerlProc_getgid();
2134 Perl_croak(aTHX_ "setrgid() not implemented");
2139 PL_gid = PerlProc_getgid();
2140 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2143 #ifdef HAS_SETGROUPS
2145 char *p = SvPV(sv, len);
2146 Groups_t gary[NGROUPS];
2151 for (i = 0; i < NGROUPS; ++i) {
2152 while (*p && !isSPACE(*p))
2161 (void)setgroups(i, gary);
2163 #else /* HAS_SETGROUPS */
2164 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2165 #endif /* HAS_SETGROUPS */
2166 if (PL_delaymagic) {
2167 PL_delaymagic |= DM_EGID;
2168 break; /* don't do magic till later */
2171 (void)setegid((Gid_t)PL_egid);
2174 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2176 #ifdef HAS_SETRESGID
2177 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2179 if (PL_egid == PL_gid) /* special case $) = $( */
2180 (void)PerlProc_setgid(PL_egid);
2182 PL_egid = PerlProc_getegid();
2183 Perl_croak(aTHX_ "setegid() not implemented");
2188 PL_egid = PerlProc_getegid();
2189 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2192 PL_chopset = SvPV_force(sv,len);
2194 #ifndef MACOS_TRADITIONAL
2196 #ifdef HAS_SETPROCTITLE
2197 /* The BSDs don't show the argv[] in ps(1) output, they
2198 * show a string from the process struct and provide
2199 * the setproctitle() routine to manipulate that. */
2202 # if __FreeBSD_version > 410001
2203 /* The leading "-" removes the "perl: " prefix,
2204 * but not the "(perl) suffix from the ps(1)
2205 * output, because that's what ps(1) shows if the
2206 * argv[] is modified. */
2207 setproctitle("-%s", s);
2208 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2209 /* This doesn't really work if you assume that
2210 * $0 = 'foobar'; will wipe out 'perl' from the $0
2211 * because in ps(1) output the result will be like
2212 * sprintf("perl: %s (perl)", s)
2213 * I guess this is a security feature:
2214 * one (a user process) cannot get rid of the original name.
2216 setproctitle("%s", s);
2223 /* See if all the arguments are contiguous in memory */
2224 for (i = 1; i < PL_origargc; i++) {
2225 if (PL_origargv[i] == s + 1
2227 || PL_origargv[i] == s + 2
2232 s += strlen(s); /* this one is ok too */
2237 /* can grab env area too? */
2238 if (PL_origenviron && (PL_origenviron[0] == s + 1)) {
2239 my_setenv("NoNe SuCh", Nullch);
2240 /* force copy of environment */
2241 for (i = 0; PL_origenviron[i]; i++)
2242 if (PL_origenviron[i] == s + 1) {
2249 PL_origalen = s - PL_origargv[0];
2251 s = SvPV_force(sv,len);
2253 if (i >= PL_origalen) {
2255 /* don't allow system to limit $0 seen by script */
2256 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2257 Copy(s, PL_origargv[0], i, char);
2258 s = PL_origargv[0]+i;
2262 Copy(s, PL_origargv[0], i, char);
2263 s = PL_origargv[0]+i;
2265 while (++i < PL_origalen)
2267 s = PL_origargv[0]+i;
2268 for (i = 1; i < PL_origargc; i++)
2269 PL_origargv[i] = Nullch;
2273 #ifdef USE_5005THREADS
2275 sv_setsv(thr->errsv, sv);
2277 #endif /* USE_5005THREADS */
2282 #ifdef USE_5005THREADS
2284 Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
2286 DEBUG_S(PerlIO_printf(Perl_debug_log,
2287 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2288 PTR2UV(thr), PTR2UV(sv)));
2290 Perl_croak(aTHX_ "panic: magic_mutexfree");
2291 MUTEX_DESTROY(MgMUTEXP(mg));
2292 COND_DESTROY(MgCONDP(mg));
2295 #endif /* USE_5005THREADS */
2298 Perl_whichsig(pTHX_ char *sig)
2300 register char **sigv;
2302 for (sigv = PL_sig_name+1; *sigv; sigv++)
2303 if (strEQ(sig,*sigv))
2304 return PL_sig_num[sigv - PL_sig_name];
2306 if (strEQ(sig,"CHLD"))
2310 if (strEQ(sig,"CLD"))
2316 #if !defined(PERL_IMPLICIT_CONTEXT)
2321 Perl_sighandler(int sig)
2323 #ifdef PERL_GET_SIG_CONTEXT
2324 dTHXa(PERL_GET_SIG_CONTEXT);
2331 SV *sv = Nullsv, *tSv = PL_Sv;
2337 if (PL_savestack_ix + 15 <= PL_savestack_max)
2339 if (PL_markstack_ptr < PL_markstack_max - 2)
2341 if (PL_retstack_ix < PL_retstack_max - 2)
2343 if (PL_scopestack_ix < PL_scopestack_max - 3)
2346 if (!PL_psig_ptr[sig])
2347 Perl_die(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
2350 /* Max number of items pushed there is 3*n or 4. We cannot fix
2351 infinity, so we fix 4 (in fact 5): */
2353 PL_savestack_ix += 5; /* Protect save in progress. */
2354 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2357 PL_markstack_ptr++; /* Protect mark. */
2360 PL_retstack[PL_retstack_ix] = NULL;
2363 PL_scopestack_ix += 1;
2364 /* sv_2cv is too complicated, try a simpler variant first: */
2365 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2366 || SvTYPE(cv) != SVt_PVCV)
2367 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2369 if (!cv || !CvROOT(cv)) {
2370 if (ckWARN(WARN_SIGNAL))
2371 Perl_warner(aTHX_ WARN_SIGNAL, "SIG%s handler \"%s\" not defined.\n",
2372 PL_sig_name[sig], (gv ? GvENAME(gv)
2379 if(PL_psig_name[sig]) {
2380 sv = SvREFCNT_inc(PL_psig_name[sig]);
2382 #if !defined(PERL_IMPLICIT_CONTEXT)
2386 sv = sv_newmortal();
2387 sv_setpv(sv,PL_sig_name[sig]);
2390 PUSHSTACKi(PERLSI_SIGNAL);
2395 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2398 if (SvTRUE(ERRSV)) {
2400 #ifdef HAS_SIGPROCMASK
2401 /* Handler "died", for example to get out of a restart-able read().
2402 * Before we re-do that on its behalf re-enable the signal which was
2403 * blocked by the system when we entered.
2407 sigaddset(&set,sig);
2408 sigprocmask(SIG_UNBLOCK, &set, NULL);
2410 /* Not clear if this will work */
2411 (void)rsignal(sig, SIG_IGN);
2412 (void)rsignal(sig, &Perl_csighandler);
2414 #endif /* !PERL_MICRO */
2415 Perl_die(aTHX_ Nullformat);
2419 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2425 PL_scopestack_ix -= 1;
2428 PL_op = myop; /* Apparently not needed... */
2430 PL_Sv = tSv; /* Restore global temporaries. */
2437 restore_magic(pTHX_ void *p)
2439 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2440 SV* sv = mgs->mgs_sv;
2445 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2448 SvFLAGS(sv) |= mgs->mgs_flags;
2452 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2455 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2457 /* If we're still on top of the stack, pop us off. (That condition
2458 * will be satisfied if restore_magic was called explicitly, but *not*
2459 * if it's being called via leave_scope.)
2460 * The reason for doing this is that otherwise, things like sv_2cv()
2461 * may leave alloc gunk on the savestack, and some code
2462 * (e.g. sighandler) doesn't expect that...
2464 if (PL_savestack_ix == mgs->mgs_ss_ix)
2466 I32 popval = SSPOPINT;
2467 assert(popval == SAVEt_DESTRUCTOR_X);
2468 PL_savestack_ix -= 2;
2470 assert(popval == SAVEt_ALLOC);
2472 PL_savestack_ix -= popval;
2478 unwind_handler_stack(pTHX_ void *p)
2480 U32 flags = *(U32*)p;
2483 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2484 /* cxstack_ix-- Not needed, die already unwound it. */
2485 #if !defined(PERL_IMPLICIT_CONTEXT)
2487 SvREFCNT_dec(sig_sv);