3 * Copyright (c) 1991-2002, 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)
33 # include <sys/pstat.h>
36 /* if you only have signal() and it resets on each signal, FAKE_PERSISTENT_SIGNAL_HANDLERS fixes */
37 #if !defined(HAS_SIGACTION) && defined(VMS)
38 # define FAKE_PERSISTENT_SIGNAL_HANDLERS
40 /* if we're doing kill() with sys$sigprc on VMS, FAKE_DEFAULT_SIGNAL_HANDLERS */
41 #if defined(KILL_BY_SIGPRC)
42 # define FAKE_DEFAULT_SIGNAL_HANDLERS
45 static void restore_magic(pTHX_ void *p);
46 static void unwind_handler_stack(pTHX_ void *p);
49 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
57 /* MGS is typedef'ed to struct magic_state in perl.h */
60 S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
63 assert(SvMAGICAL(sv));
64 #ifdef PERL_COPY_ON_WRITE
65 /* Turning READONLY off for a copy-on-write scalar is a bad idea. */
70 SAVEDESTRUCTOR_X(restore_magic, INT2PTR(void*, (IV)mgs_ix));
72 mgs = SSPTR(mgs_ix, MGS*);
74 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
75 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
79 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
83 =for apidoc mg_magical
85 Turns on the magical status of an SV. See C<sv_magic>.
91 Perl_mg_magical(pTHX_ SV *sv)
94 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
95 MGVTBL* vtbl = mg->mg_virtual;
97 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
101 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
110 Do magic after a value is retrieved from the SV. See C<sv_magic>.
116 Perl_mg_get(pTHX_ SV *sv)
119 MAGIC *newmg, *head, *cur, *mg;
120 I32 mgs_ix = SSNEW(sizeof(MGS));
122 save_magic(mgs_ix, sv);
124 /* We must call svt_get(sv, mg) for each valid entry in the linked
125 list of magic. svt_get() may delete the current entry, add new
126 magic to the head of the list, or upgrade the SV. AMS 20010810 */
128 newmg = cur = head = mg = SvMAGIC(sv);
130 MGVTBL *vtbl = mg->mg_virtual;
132 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
133 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
134 /* Don't restore the flags for this entry if it was deleted. */
135 if (mg->mg_flags & MGf_GSKIP)
136 (SSPTR(mgs_ix, MGS *))->mgs_flags = 0;
139 mg = mg->mg_moremagic;
142 /* Have we finished with the new entries we saw? Start again
143 where we left off (unless there are more new entries). */
151 /* Were any new entries added? */
152 if (!new && (newmg = SvMAGIC(sv)) != head) {
159 restore_magic(aTHX_ INT2PTR(void *, (IV)mgs_ix));
166 Do magic after a value is assigned to the SV. See C<sv_magic>.
172 Perl_mg_set(pTHX_ SV *sv)
178 mgs_ix = SSNEW(sizeof(MGS));
179 save_magic(mgs_ix, sv);
181 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
182 MGVTBL* vtbl = mg->mg_virtual;
183 nextmg = mg->mg_moremagic; /* it may delete itself */
184 if (mg->mg_flags & MGf_GSKIP) {
185 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
186 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
188 if (vtbl && vtbl->svt_set)
189 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
192 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
197 =for apidoc mg_length
199 Report on the SV's length. See C<sv_magic>.
205 Perl_mg_length(pTHX_ SV *sv)
210 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
211 MGVTBL* vtbl = mg->mg_virtual;
212 if (vtbl && vtbl->svt_len) {
215 mgs_ix = SSNEW(sizeof(MGS));
216 save_magic(mgs_ix, sv);
217 /* omit MGf_GSKIP -- not changed here */
218 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
219 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
226 U8 *s = (U8*)SvPV(sv, len);
227 len = Perl_utf8_length(aTHX_ s, s + len);
235 Perl_mg_size(pTHX_ SV *sv)
240 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
241 MGVTBL* vtbl = mg->mg_virtual;
242 if (vtbl && vtbl->svt_len) {
245 mgs_ix = SSNEW(sizeof(MGS));
246 save_magic(mgs_ix, sv);
247 /* omit MGf_GSKIP -- not changed here */
248 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
249 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
256 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
261 Perl_croak(aTHX_ "Size magic not implemented");
270 Clear something magical that the SV represents. See C<sv_magic>.
276 Perl_mg_clear(pTHX_ SV *sv)
281 mgs_ix = SSNEW(sizeof(MGS));
282 save_magic(mgs_ix, sv);
284 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
285 MGVTBL* vtbl = mg->mg_virtual;
286 /* omit GSKIP -- never set here */
288 if (vtbl && vtbl->svt_clear)
289 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
292 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
299 Finds the magic pointer for type matching the SV. See C<sv_magic>.
305 Perl_mg_find(pTHX_ SV *sv, int type)
310 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
311 if (mg->mg_type == type)
320 Copies the magic from one SV to another. See C<sv_magic>.
326 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
330 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
331 MGVTBL* vtbl = mg->mg_virtual;
332 if ((mg->mg_flags & MGf_COPY) && vtbl->svt_copy){
333 count += CALL_FPTR(vtbl->svt_copy)(aTHX_ sv, mg, nsv, key, klen);
335 else if (isUPPER(mg->mg_type)) {
337 mg->mg_type == PERL_MAGIC_tied ? SvTIED_obj(sv, mg) :
338 (mg->mg_type == PERL_MAGIC_regdata && mg->mg_obj)
340 toLOWER(mg->mg_type), key, klen);
350 Free any magic storage used by the SV. See C<sv_magic>.
356 Perl_mg_free(pTHX_ SV *sv)
360 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
361 MGVTBL* vtbl = mg->mg_virtual;
362 moremagic = mg->mg_moremagic;
363 if (vtbl && vtbl->svt_free)
364 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
365 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
367 Safefree(mg->mg_ptr);
368 else if (mg->mg_len == HEf_SVKEY)
369 SvREFCNT_dec((SV*)mg->mg_ptr);
371 if (mg->mg_flags & MGf_REFCOUNTED)
372 SvREFCNT_dec(mg->mg_obj);
380 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
385 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
389 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
390 if (mg->mg_obj) /* @+ */
393 return rx->lastparen;
400 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
408 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
412 if (paren <= (I32)rx->nparens &&
413 (s = rx->startp[paren]) != -1 &&
414 (t = rx->endp[paren]) != -1)
416 if (mg->mg_obj) /* @+ */
421 if (i > 0 && RX_MATCH_UTF8(rx)) {
422 char *b = rx->subbeg;
424 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
434 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
436 Perl_croak(aTHX_ PL_no_modify);
442 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
449 switch (*mg->mg_ptr) {
450 case '1': case '2': case '3': case '4':
451 case '5': case '6': case '7': case '8': case '9': case '&':
452 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
454 paren = atoi(mg->mg_ptr); /* $& is in [0] */
456 if (paren <= (I32)rx->nparens &&
457 (s1 = rx->startp[paren]) != -1 &&
458 (t1 = rx->endp[paren]) != -1)
462 if (i > 0 && RX_MATCH_UTF8(rx)) {
463 char *s = rx->subbeg + s1;
464 char *send = rx->subbeg + t1;
467 if (is_utf8_string((U8*)s, i))
468 i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send);
471 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
475 if (ckWARN(WARN_UNINITIALIZED))
480 if (ckWARN(WARN_UNINITIALIZED))
485 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
486 paren = rx->lastparen;
491 case '\016': /* ^N */
492 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
493 paren = rx->lastcloseparen;
499 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
500 if (rx->startp[0] != -1) {
511 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
512 if (rx->endp[0] != -1) {
513 i = rx->sublen - rx->endp[0];
524 if (!SvPOK(sv) && SvNIOK(sv)) {
534 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
541 switch (*mg->mg_ptr) {
542 case '\001': /* ^A */
543 sv_setsv(sv, PL_bodytarget);
545 case '\003': /* ^C */
546 sv_setiv(sv, (IV)PL_minus_c);
549 case '\004': /* ^D */
550 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
551 #if defined(YYDEBUG) && defined(DEBUGGING)
552 PL_yydebug = DEBUG_p_TEST;
555 case '\005': /* ^E */
556 if (*(mg->mg_ptr+1) == '\0') {
557 #ifdef MACOS_TRADITIONAL
561 sv_setnv(sv,(double)gMacPerl_OSErr);
562 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
567 # include <descrip.h>
568 # include <starlet.h>
570 $DESCRIPTOR(msgdsc,msg);
571 sv_setnv(sv,(NV) vaxc$errno);
572 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
573 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
579 if (!(_emx_env & 0x200)) { /* Under DOS */
580 sv_setnv(sv, (NV)errno);
581 sv_setpv(sv, errno ? Strerror(errno) : "");
583 if (errno != errno_isOS2) {
584 int tmp = _syserrno();
585 if (tmp) /* 2nd call to _syserrno() makes it 0 */
588 sv_setnv(sv, (NV)Perl_rc);
589 sv_setpv(sv, os2error(Perl_rc));
594 DWORD dwErr = GetLastError();
595 sv_setnv(sv, (NV)dwErr);
598 PerlProc_GetOSError(sv, dwErr);
605 sv_setnv(sv, (NV)errno);
606 sv_setpv(sv, errno ? Strerror(errno) : "");
611 SvNOK_on(sv); /* what a wonderful hack! */
613 else if (strEQ(mg->mg_ptr+1, "NCODING"))
614 sv_setsv(sv, PL_encoding);
616 case '\006': /* ^F */
617 sv_setiv(sv, (IV)PL_maxsysfd);
619 case '\010': /* ^H */
620 sv_setiv(sv, (IV)PL_hints);
622 case '\011': /* ^I */ /* NOT \t in EBCDIC */
624 sv_setpv(sv, PL_inplace);
626 sv_setsv(sv, &PL_sv_undef);
628 case '\017': /* ^O & ^OPEN */
629 if (*(mg->mg_ptr+1) == '\0')
630 sv_setpv(sv, PL_osname);
631 else if (strEQ(mg->mg_ptr, "\017PEN")) {
632 if (!PL_compiling.cop_io)
633 sv_setsv(sv, &PL_sv_undef);
635 sv_setsv(sv, PL_compiling.cop_io);
639 case '\020': /* ^P */
640 sv_setiv(sv, (IV)PL_perldb);
642 case '\023': /* ^S */
644 if (PL_lex_state != LEX_NOTPARSING)
647 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
652 case '\024': /* ^T */
653 if (*(mg->mg_ptr+1) == '\0') {
655 sv_setnv(sv, PL_basetime);
657 sv_setiv(sv, (IV)PL_basetime);
660 else if (strEQ(mg->mg_ptr, "\024AINT"))
661 sv_setiv(sv, PL_tainting
662 ? (PL_taint_warn || PL_unsafe ? -1 : 1)
665 case '\025': /* $^UNICODE */
666 if (strEQ(mg->mg_ptr, "\025NICODE"))
667 sv_setuv(sv, (UV) PL_unicode);
669 case '\027': /* ^W & $^WARNING_BITS */
670 if (*(mg->mg_ptr+1) == '\0')
671 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
672 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
673 if (PL_compiling.cop_warnings == pWARN_NONE ||
674 PL_compiling.cop_warnings == pWARN_STD)
676 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
678 else if (PL_compiling.cop_warnings == pWARN_ALL) {
679 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
682 sv_setsv(sv, PL_compiling.cop_warnings);
687 case '1': case '2': case '3': case '4':
688 case '5': case '6': case '7': case '8': case '9': case '&':
689 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
693 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
694 * XXX Does the new way break anything?
696 paren = atoi(mg->mg_ptr); /* $& is in [0] */
698 if (paren <= (I32)rx->nparens &&
699 (s1 = rx->startp[paren]) != -1 &&
700 (t1 = rx->endp[paren]) != -1)
710 if (RX_MATCH_UTF8(rx) && is_utf8_string((U8*)s, i))
715 if (RX_MATCH_TAINTED(rx)) {
716 MAGIC* mg = SvMAGIC(sv);
719 SvMAGIC(sv) = mg->mg_moremagic;
721 if ((mgt = SvMAGIC(sv))) {
722 mg->mg_moremagic = mgt;
732 sv_setsv(sv,&PL_sv_undef);
735 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
736 paren = rx->lastparen;
740 sv_setsv(sv,&PL_sv_undef);
742 case '\016': /* ^N */
743 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
744 paren = rx->lastcloseparen;
748 sv_setsv(sv,&PL_sv_undef);
751 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
752 if ((s = rx->subbeg) && rx->startp[0] != -1) {
757 sv_setsv(sv,&PL_sv_undef);
760 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
761 if (rx->subbeg && rx->endp[0] != -1) {
762 s = rx->subbeg + rx->endp[0];
763 i = rx->sublen - rx->endp[0];
767 sv_setsv(sv,&PL_sv_undef);
771 if (GvIO(PL_last_in_gv)) {
772 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
778 sv_setiv(sv, (IV)STATUS_CURRENT);
779 #ifdef COMPLEX_STATUS
780 LvTARGOFF(sv) = PL_statusvalue;
781 LvTARGLEN(sv) = PL_statusvalue_vms;
786 s = IoTOP_NAME(GvIOp(PL_defoutgv));
790 sv_setpv(sv,GvENAME(PL_defoutgv));
795 s = IoFMT_NAME(GvIOp(PL_defoutgv));
797 s = GvENAME(PL_defoutgv);
802 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
805 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
808 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
816 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
819 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
825 sv_copypv(sv, PL_ors_sv);
828 sv_setpv(sv,PL_ofmt);
832 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
833 sv_setpv(sv, errno ? Strerror(errno) : "");
836 int saveerrno = errno;
837 sv_setnv(sv, (NV)errno);
839 if (errno == errno_isOS2 || errno == errno_isOS2_set)
840 sv_setpv(sv, os2error(Perl_rc));
843 sv_setpv(sv, errno ? Strerror(errno) : "");
847 SvNOK_on(sv); /* what a wonderful hack! */
850 sv_setiv(sv, (IV)PL_uid);
853 sv_setiv(sv, (IV)PL_euid);
856 sv_setiv(sv, (IV)PL_gid);
858 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
862 sv_setiv(sv, (IV)PL_egid);
864 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
869 Groups_t gary[NGROUPS];
870 i = getgroups(NGROUPS,gary);
872 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
875 (void)SvIOK_on(sv); /* what a wonderful hack! */
879 #ifndef MACOS_TRADITIONAL
888 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
890 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
892 if (uf && uf->uf_val)
893 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
898 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
908 #ifdef DYNAMIC_ENV_FETCH
909 /* We just undefd an environment var. Is a replacement */
910 /* waiting in the wings? */
913 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
914 s = SvPV(*valp, len);
918 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
919 /* And you'll never guess what the dog had */
920 /* in its mouth... */
922 MgTAINTEDDIR_off(mg);
924 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
925 char pathbuf[256], eltbuf[256], *cp, *elt = s;
929 do { /* DCL$PATH may be a search list */
930 while (1) { /* as may dev portion of any element */
931 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
932 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
933 cando_by_name(S_IWUSR,0,elt) ) {
938 if ((cp = strchr(elt, ':')) != Nullch)
940 if (my_trnlnm(elt, eltbuf, j++))
946 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
949 if (s && klen == 4 && strEQ(ptr,"PATH")) {
950 char *strend = s + len;
956 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
959 if (i >= sizeof tmpbuf /* too long -- assume the worst */
961 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
968 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
974 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
977 my_setenv(MgPV(mg,n_a),Nullch);
982 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
985 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
990 magic_clear_all_env(sv,mg);
991 hv_iterinit((HV*)sv);
992 while ((entry = hv_iternext((HV*)sv))) {
994 my_setenv(hv_iterkey(entry, &keylen),
995 SvPV(hv_iterval((HV*)sv, entry), n_a));
1003 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
1005 #if defined(VMS) || defined(EPOC)
1006 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1008 # if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
1011 # ifdef USE_ENVIRON_ARRAY
1012 # if defined(USE_ITHREADS)
1013 /* only the parent thread can clobber the process environment */
1014 if (PL_curinterp == aTHX)
1017 # ifndef PERL_USE_SAFE_PUTENV
1020 if (environ == PL_origenviron)
1021 environ = (char**)safesysmalloc(sizeof(char*));
1023 for (i = 0; environ[i]; i++)
1024 safesysfree(environ[i]);
1025 # endif /* PERL_USE_SAFE_PUTENV */
1027 environ[0] = Nullch;
1029 # endif /* USE_ENVIRON_ARRAY */
1030 # endif /* PERL_IMPLICIT_SYS || WIN32 */
1031 #endif /* VMS || EPC */
1035 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS)||defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1036 static int sig_handlers_initted = 0;
1038 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1039 static int sig_ignoring[SIG_SIZE]; /* which signals we are ignoring */
1041 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1042 static int sig_defaulting[SIG_SIZE];
1047 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
1051 /* Are we fetching a signal entry? */
1052 i = whichsig(MgPV(mg,n_a));
1055 sv_setsv(sv,PL_psig_ptr[i]);
1057 Sighandler_t sigstate;
1058 sigstate = rsignal_state(i);
1059 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1060 if (sig_handlers_initted && sig_ignoring[i]) sigstate = SIG_IGN;
1062 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1063 if (sig_handlers_initted && sig_defaulting[i]) sigstate = SIG_DFL;
1065 /* cache state so we don't fetch it again */
1066 if(sigstate == SIG_IGN)
1067 sv_setpv(sv,"IGNORE");
1069 sv_setsv(sv,&PL_sv_undef);
1070 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1077 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
1081 /* Are we clearing a signal entry? */
1082 i = whichsig(MgPV(mg,n_a));
1084 if(PL_psig_ptr[i]) {
1085 SvREFCNT_dec(PL_psig_ptr[i]);
1088 if(PL_psig_name[i]) {
1089 SvREFCNT_dec(PL_psig_name[i]);
1097 Perl_raise_signal(pTHX_ int sig)
1099 /* Set a flag to say this signal is pending */
1100 PL_psig_pend[sig]++;
1101 /* And one to say _a_ signal is pending */
1106 Perl_csighandler(int sig)
1108 #ifdef PERL_GET_SIG_CONTEXT
1109 dTHXa(PERL_GET_SIG_CONTEXT);
1113 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1114 (void) rsignal(sig, &Perl_csighandler);
1115 if (sig_ignoring[sig]) return;
1117 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1118 if (sig_defaulting[sig])
1119 #ifdef KILL_BY_SIGPRC
1120 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1126 #ifdef PERL_OLD_SIGNALS
1127 /* Call the perl level handler now with risk we may be in malloc() etc. */
1128 (*PL_sighandlerp)(sig);
1130 Perl_raise_signal(aTHX_ sig);
1134 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1136 Perl_csighandler_init(void)
1139 if (sig_handlers_initted) return;
1141 for (sig = 1; sig < SIG_SIZE; sig++) {
1142 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1144 sig_defaulting[sig] = 1;
1145 (void) rsignal(sig, &Perl_csighandler);
1147 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1148 sig_ignoring[sig] = 0;
1151 sig_handlers_initted = 1;
1156 Perl_despatch_signals(pTHX)
1160 for (sig = 1; sig < SIG_SIZE; sig++) {
1161 if (PL_psig_pend[sig]) {
1162 PL_psig_pend[sig] = 0;
1163 (*PL_sighandlerp)(sig);
1169 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1178 if (strEQ(s,"__DIE__"))
1180 else if (strEQ(s,"__WARN__"))
1183 Perl_croak(aTHX_ "No such hook: %s", s);
1191 i = whichsig(s); /* ...no, a brick */
1193 if (ckWARN(WARN_SIGNAL))
1194 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
1197 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1198 if (!sig_handlers_initted) Perl_csighandler_init();
1200 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1201 sig_ignoring[i] = 0;
1203 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1204 sig_defaulting[i] = 0;
1206 SvREFCNT_dec(PL_psig_name[i]);
1207 SvREFCNT_dec(PL_psig_ptr[i]);
1208 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1209 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1210 PL_psig_name[i] = newSVpvn(s, len);
1211 SvREADONLY_on(PL_psig_name[i]);
1213 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1215 (void)rsignal(i, &Perl_csighandler);
1217 *svp = SvREFCNT_inc(sv);
1220 s = SvPV_force(sv,len);
1221 if (strEQ(s,"IGNORE")) {
1223 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1224 sig_ignoring[i] = 1;
1225 (void)rsignal(i, &Perl_csighandler);
1227 (void)rsignal(i, SIG_IGN);
1232 else if (strEQ(s,"DEFAULT") || !*s) {
1234 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1236 sig_defaulting[i] = 1;
1237 (void)rsignal(i, &Perl_csighandler);
1240 (void)rsignal(i, SIG_DFL);
1247 * We should warn if HINT_STRICT_REFS, but without
1248 * access to a known hint bit in a known OP, we can't
1249 * tell whether HINT_STRICT_REFS is in force or not.
1251 if (!strchr(s,':') && !strchr(s,'\''))
1252 sv_insert(sv, 0, 0, "main::", 6);
1254 (void)rsignal(i, &Perl_csighandler);
1256 *svp = SvREFCNT_inc(sv);
1260 #endif /* !PERL_MICRO */
1263 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1265 PL_sub_generation++;
1270 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1272 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1273 PL_amagic_generation++;
1279 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1281 HV *hv = (HV*)LvTARG(sv);
1285 (void) hv_iterinit(hv);
1286 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1289 while (hv_iternext(hv))
1294 sv_setiv(sv, (IV)i);
1299 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1302 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1307 /* caller is responsible for stack switching/cleanup */
1309 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1315 PUSHs(SvTIED_obj(sv, mg));
1318 if (mg->mg_len >= 0)
1319 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1320 else if (mg->mg_len == HEf_SVKEY)
1321 PUSHs((SV*)mg->mg_ptr);
1323 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1324 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1332 return call_method(meth, flags);
1336 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1342 PUSHSTACKi(PERLSI_MAGIC);
1344 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1345 sv_setsv(sv, *PL_stack_sp--);
1355 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1357 magic_methpack(sv,mg,"FETCH");
1359 mg->mg_flags |= MGf_GSKIP;
1364 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1368 PUSHSTACKi(PERLSI_MAGIC);
1369 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1376 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1378 return magic_methpack(sv,mg,"DELETE");
1383 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1390 PUSHSTACKi(PERLSI_MAGIC);
1391 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1392 sv = *PL_stack_sp--;
1393 retval = (U32) SvIV(sv)-1;
1402 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1407 PUSHSTACKi(PERLSI_MAGIC);
1409 XPUSHs(SvTIED_obj(sv, mg));
1411 call_method("CLEAR", G_SCALAR|G_DISCARD);
1418 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1421 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1425 PUSHSTACKi(PERLSI_MAGIC);
1428 PUSHs(SvTIED_obj(sv, mg));
1433 if (call_method(meth, G_SCALAR))
1434 sv_setsv(key, *PL_stack_sp--);
1443 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1445 return magic_methpack(sv,mg,"EXISTS");
1449 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1459 svp = av_fetch(GvAV(gv),
1460 atoi(MgPV(mg,n_a)), FALSE);
1461 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp)))) {
1462 /* set or clear breakpoint in the relevant control op */
1464 o->op_flags |= OPf_SPECIAL;
1466 o->op_flags &= ~OPf_SPECIAL;
1472 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1474 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1479 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1481 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1486 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1488 SV* lsv = LvTARG(sv);
1490 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1491 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1492 if (mg && mg->mg_len >= 0) {
1495 sv_pos_b2u(lsv, &i);
1496 sv_setiv(sv, i + PL_curcop->cop_arybase);
1505 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1507 SV* lsv = LvTARG(sv);
1514 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1515 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1519 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1520 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1522 else if (!SvOK(sv)) {
1526 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1528 pos = SvIV(sv) - PL_curcop->cop_arybase;
1531 ulen = sv_len_utf8(lsv);
1541 else if (pos > (SSize_t)len)
1546 sv_pos_u2b(lsv, &p, 0);
1551 mg->mg_flags &= ~MGf_MINMATCH;
1557 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1559 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1561 gv_efullname3(sv,((GV*)sv), "*");
1565 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1570 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1579 if (*s == '*' && s[1])
1581 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1586 GvGP(sv) = gp_ref(GvGP(gv));
1591 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1594 SV *lsv = LvTARG(sv);
1595 char *tmps = SvPV(lsv,len);
1596 I32 offs = LvTARGOFF(sv);
1597 I32 rem = LvTARGLEN(sv);
1600 sv_pos_u2b(lsv, &offs, &rem);
1601 if (offs > (I32)len)
1603 if (rem + offs > (I32)len)
1605 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1612 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1615 char *tmps = SvPV(sv, len);
1616 SV *lsv = LvTARG(sv);
1617 I32 lvoff = LvTARGOFF(sv);
1618 I32 lvlen = LvTARGLEN(sv);
1621 sv_utf8_upgrade(lsv);
1622 sv_pos_u2b(lsv, &lvoff, &lvlen);
1623 sv_insert(lsv, lvoff, lvlen, tmps, len);
1626 else if (lsv && SvUTF8(lsv)) {
1627 sv_pos_u2b(lsv, &lvoff, &lvlen);
1628 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
1629 sv_insert(lsv, lvoff, lvlen, tmps, len);
1633 sv_insert(lsv, lvoff, lvlen, tmps, len);
1639 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1641 TAINT_IF((mg->mg_len & 1) ||
1642 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1647 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1649 if (PL_localizing) {
1650 if (PL_localizing == 1)
1655 else if (PL_tainted)
1663 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1665 SV *lsv = LvTARG(sv);
1672 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1677 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1679 do_vecset(sv); /* XXX slurp this routine */
1684 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1687 if (LvTARGLEN(sv)) {
1689 SV *ahv = LvTARG(sv);
1690 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1695 AV* av = (AV*)LvTARG(sv);
1696 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1697 targ = AvARRAY(av)[LvTARGOFF(sv)];
1699 if (targ && targ != &PL_sv_undef) {
1700 /* somebody else defined it for us */
1701 SvREFCNT_dec(LvTARG(sv));
1702 LvTARG(sv) = SvREFCNT_inc(targ);
1704 SvREFCNT_dec(mg->mg_obj);
1705 mg->mg_obj = Nullsv;
1706 mg->mg_flags &= ~MGf_REFCOUNTED;
1711 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1716 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1721 sv_setsv(LvTARG(sv), sv);
1722 SvSETMAGIC(LvTARG(sv));
1728 Perl_vivify_defelem(pTHX_ SV *sv)
1733 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
1736 SV *ahv = LvTARG(sv);
1738 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1741 if (!value || value == &PL_sv_undef)
1742 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1745 AV* av = (AV*)LvTARG(sv);
1746 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1747 LvTARG(sv) = Nullsv; /* array can't be extended */
1749 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1750 if (!svp || (value = *svp) == &PL_sv_undef)
1751 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1754 (void)SvREFCNT_inc(value);
1755 SvREFCNT_dec(LvTARG(sv));
1758 SvREFCNT_dec(mg->mg_obj);
1759 mg->mg_obj = Nullsv;
1760 mg->mg_flags &= ~MGf_REFCOUNTED;
1764 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1766 AV *av = (AV*)mg->mg_obj;
1767 SV **svp = AvARRAY(av);
1768 I32 i = AvFILLp(av);
1770 if (svp[i] && svp[i] != &PL_sv_undef) {
1771 if (!SvWEAKREF(svp[i]))
1772 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1773 /* XXX Should we check that it hasn't changed? */
1775 (void)SvOK_off(svp[i]);
1776 SvWEAKREF_off(svp[i]);
1777 svp[i] = &PL_sv_undef;
1785 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1793 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1795 sv_unmagic(sv, PERL_MAGIC_bm);
1801 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1803 sv_unmagic(sv, PERL_MAGIC_fm);
1809 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1811 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1813 if (uf && uf->uf_set)
1814 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
1819 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1821 regexp *re = (regexp *)mg->mg_obj;
1826 #ifdef USE_LOCALE_COLLATE
1828 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1831 * RenE<eacute> Descartes said "I think not."
1832 * and vanished with a faint plop.
1835 Safefree(mg->mg_ptr);
1841 #endif /* USE_LOCALE_COLLATE */
1843 /* Just clear the UTF-8 cache data. */
1845 Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
1847 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
1849 mg->mg_len = -1; /* The mg_len holds the len cache. */
1854 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1859 switch (*mg->mg_ptr) {
1860 case '\001': /* ^A */
1861 sv_setsv(PL_bodytarget, sv);
1863 case '\003': /* ^C */
1864 PL_minus_c = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1867 case '\004': /* ^D */
1868 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
1869 DEBUG_x(dump_all());
1871 case '\005': /* ^E */
1872 if (*(mg->mg_ptr+1) == '\0') {
1873 #ifdef MACOS_TRADITIONAL
1874 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1877 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1880 SetLastError( SvIV(sv) );
1883 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1885 /* will anyone ever use this? */
1886 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1892 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
1894 SvREFCNT_dec(PL_encoding);
1895 if (SvOK(sv) || SvGMAGICAL(sv)) {
1896 PL_encoding = newSVsv(sv);
1899 PL_encoding = Nullsv;
1903 case '\006': /* ^F */
1904 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1906 case '\010': /* ^H */
1907 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1909 case '\011': /* ^I */ /* NOT \t in EBCDIC */
1911 Safefree(PL_inplace);
1913 PL_inplace = savepv(SvPV(sv,len));
1915 PL_inplace = Nullch;
1917 case '\017': /* ^O */
1918 if (*(mg->mg_ptr+1) == '\0') {
1920 Safefree(PL_osname);
1922 PL_osname = savepv(SvPV(sv,len));
1926 else if (strEQ(mg->mg_ptr, "\017PEN")) {
1927 if (!PL_compiling.cop_io)
1928 PL_compiling.cop_io = newSVsv(sv);
1930 sv_setsv(PL_compiling.cop_io,sv);
1933 case '\020': /* ^P */
1934 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1935 if (PL_perldb && !PL_DBsingle)
1938 case '\024': /* ^T */
1940 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1942 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1945 case '\027': /* ^W & $^WARNING_BITS */
1946 if (*(mg->mg_ptr+1) == '\0') {
1947 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1948 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1949 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1950 | (i ? G_WARN_ON : G_WARN_OFF) ;
1953 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
1954 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1955 if (!SvPOK(sv) && PL_localizing) {
1956 sv_setpvn(sv, WARN_NONEstring, WARNsize);
1957 PL_compiling.cop_warnings = pWARN_NONE;
1962 int accumulate = 0 ;
1963 int any_fatals = 0 ;
1964 char * ptr = (char*)SvPV(sv, len) ;
1965 for (i = 0 ; i < len ; ++i) {
1966 accumulate |= ptr[i] ;
1967 any_fatals |= (ptr[i] & 0xAA) ;
1970 PL_compiling.cop_warnings = pWARN_NONE;
1971 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
1972 PL_compiling.cop_warnings = pWARN_ALL;
1973 PL_dowarn |= G_WARN_ONCE ;
1976 if (specialWARN(PL_compiling.cop_warnings))
1977 PL_compiling.cop_warnings = newSVsv(sv) ;
1979 sv_setsv(PL_compiling.cop_warnings, sv);
1980 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1981 PL_dowarn |= G_WARN_ONCE ;
1989 if (PL_localizing) {
1990 if (PL_localizing == 1)
1991 SAVESPTR(PL_last_in_gv);
1993 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1994 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
1997 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
1998 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1999 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
2002 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2003 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
2004 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
2007 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2010 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2011 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2012 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
2015 IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2019 IO *io = GvIOp(PL_defoutgv);
2022 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
2023 IoFLAGS(io) &= ~IOf_FLUSH;
2025 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2026 PerlIO *ofp = IoOFP(io);
2028 (void)PerlIO_flush(ofp);
2029 IoFLAGS(io) |= IOf_FLUSH;
2035 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2036 PL_multiline = (i != 0);
2039 SvREFCNT_dec(PL_rs);
2040 PL_rs = newSVsv(sv);
2044 SvREFCNT_dec(PL_ors_sv);
2045 if (SvOK(sv) || SvGMAGICAL(sv)) {
2046 PL_ors_sv = newSVsv(sv);
2054 SvREFCNT_dec(PL_ofs_sv);
2055 if (SvOK(sv) || SvGMAGICAL(sv)) {
2056 PL_ofs_sv = newSVsv(sv);
2065 PL_ofmt = savepv(SvPV(sv,len));
2068 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2071 #ifdef COMPLEX_STATUS
2072 if (PL_localizing == 2) {
2073 PL_statusvalue = LvTARGOFF(sv);
2074 PL_statusvalue_vms = LvTARGLEN(sv);
2078 #ifdef VMSISH_STATUS
2080 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
2083 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2088 # define PERL_VMS_BANG vaxc$errno
2090 # define PERL_VMS_BANG 0
2092 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
2093 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
2097 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2098 if (PL_delaymagic) {
2099 PL_delaymagic |= DM_RUID;
2100 break; /* don't do magic till later */
2103 (void)setruid((Uid_t)PL_uid);
2106 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
2108 #ifdef HAS_SETRESUID
2109 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
2111 if (PL_uid == PL_euid) /* special case $< = $> */
2112 (void)PerlProc_setuid(PL_uid);
2114 PL_uid = PerlProc_getuid();
2115 Perl_croak(aTHX_ "setruid() not implemented");
2120 PL_uid = PerlProc_getuid();
2121 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2124 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2125 if (PL_delaymagic) {
2126 PL_delaymagic |= DM_EUID;
2127 break; /* don't do magic till later */
2130 (void)seteuid((Uid_t)PL_euid);
2133 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
2135 #ifdef HAS_SETRESUID
2136 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
2138 if (PL_euid == PL_uid) /* special case $> = $< */
2139 PerlProc_setuid(PL_euid);
2141 PL_euid = PerlProc_geteuid();
2142 Perl_croak(aTHX_ "seteuid() not implemented");
2147 PL_euid = PerlProc_geteuid();
2148 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2151 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2152 if (PL_delaymagic) {
2153 PL_delaymagic |= DM_RGID;
2154 break; /* don't do magic till later */
2157 (void)setrgid((Gid_t)PL_gid);
2160 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
2162 #ifdef HAS_SETRESGID
2163 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2165 if (PL_gid == PL_egid) /* special case $( = $) */
2166 (void)PerlProc_setgid(PL_gid);
2168 PL_gid = PerlProc_getgid();
2169 Perl_croak(aTHX_ "setrgid() not implemented");
2174 PL_gid = PerlProc_getgid();
2175 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2178 #ifdef HAS_SETGROUPS
2180 char *p = SvPV(sv, len);
2181 Groups_t gary[NGROUPS];
2186 for (i = 0; i < NGROUPS; ++i) {
2187 while (*p && !isSPACE(*p))
2196 (void)setgroups(i, gary);
2198 #else /* HAS_SETGROUPS */
2199 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2200 #endif /* HAS_SETGROUPS */
2201 if (PL_delaymagic) {
2202 PL_delaymagic |= DM_EGID;
2203 break; /* don't do magic till later */
2206 (void)setegid((Gid_t)PL_egid);
2209 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2211 #ifdef HAS_SETRESGID
2212 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2214 if (PL_egid == PL_gid) /* special case $) = $( */
2215 (void)PerlProc_setgid(PL_egid);
2217 PL_egid = PerlProc_getegid();
2218 Perl_croak(aTHX_ "setegid() not implemented");
2223 PL_egid = PerlProc_getegid();
2224 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2227 PL_chopset = SvPV_force(sv,len);
2229 #ifndef MACOS_TRADITIONAL
2231 LOCK_DOLLARZERO_MUTEX;
2232 #ifdef HAS_SETPROCTITLE
2233 /* The BSDs don't show the argv[] in ps(1) output, they
2234 * show a string from the process struct and provide
2235 * the setproctitle() routine to manipulate that. */
2238 # if __FreeBSD_version > 410001
2239 /* The leading "-" removes the "perl: " prefix,
2240 * but not the "(perl) suffix from the ps(1)
2241 * output, because that's what ps(1) shows if the
2242 * argv[] is modified. */
2243 setproctitle("-%s", s);
2244 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2245 /* This doesn't really work if you assume that
2246 * $0 = 'foobar'; will wipe out 'perl' from the $0
2247 * because in ps(1) output the result will be like
2248 * sprintf("perl: %s (perl)", s)
2249 * I guess this is a security feature:
2250 * one (a user process) cannot get rid of the original name.
2252 setproctitle("%s", s);
2256 #if defined(__hpux) && defined(PSTAT_SETCMD)
2261 pstat(PSTAT_SETCMD, un, len, 0, 0);
2267 /* See if all the arguments are contiguous in memory */
2268 for (i = 1; i < PL_origargc; i++) {
2269 if (PL_origargv[i] == s + 1
2271 || PL_origargv[i] == s + 2
2276 s += strlen(s); /* this one is ok too */
2281 /* can grab env area too? */
2284 && PL_curinterp == aTHX
2286 && (PL_origenviron[0] == s + 1))
2288 my_setenv("NoNe SuCh", Nullch);
2289 /* force copy of environment */
2290 for (i = 0; PL_origenviron[i]; i++)
2291 if (PL_origenviron[i] == s + 1) {
2298 PL_origalen = s - PL_origargv[0];
2300 s = SvPV_force(sv,len);
2302 if (i >= (I32)PL_origalen) {
2304 /* don't allow system to limit $0 seen by script */
2305 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2306 Copy(s, PL_origargv[0], i, char);
2307 s = PL_origargv[0]+i;
2311 Copy(s, PL_origargv[0], i, char);
2312 s = PL_origargv[0]+i;
2314 while (++i < (I32)PL_origalen)
2316 for (i = 1; i < PL_origargc; i++)
2317 PL_origargv[i] = Nullch;
2319 UNLOCK_DOLLARZERO_MUTEX;
2327 Perl_whichsig(pTHX_ char *sig)
2329 register char **sigv;
2331 for (sigv = PL_sig_name+1; *sigv; sigv++)
2332 if (strEQ(sig,*sigv))
2333 return PL_sig_num[sigv - PL_sig_name];
2335 if (strEQ(sig,"CHLD"))
2339 if (strEQ(sig,"CLD"))
2345 #if !defined(PERL_IMPLICIT_CONTEXT)
2350 Perl_sighandler(int sig)
2352 #ifdef PERL_GET_SIG_CONTEXT
2353 dTHXa(PERL_GET_SIG_CONTEXT);
2360 SV *sv = Nullsv, *tSv = PL_Sv;
2366 if (PL_savestack_ix + 15 <= PL_savestack_max)
2368 if (PL_markstack_ptr < PL_markstack_max - 2)
2370 if (PL_retstack_ix < PL_retstack_max - 2)
2372 if (PL_scopestack_ix < PL_scopestack_max - 3)
2375 if (!PL_psig_ptr[sig]) {
2376 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
2381 /* Max number of items pushed there is 3*n or 4. We cannot fix
2382 infinity, so we fix 4 (in fact 5): */
2384 PL_savestack_ix += 5; /* Protect save in progress. */
2385 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2388 PL_markstack_ptr++; /* Protect mark. */
2391 PL_retstack[PL_retstack_ix] = NULL;
2394 PL_scopestack_ix += 1;
2395 /* sv_2cv is too complicated, try a simpler variant first: */
2396 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2397 || SvTYPE(cv) != SVt_PVCV)
2398 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2400 if (!cv || !CvROOT(cv)) {
2401 if (ckWARN(WARN_SIGNAL))
2402 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
2403 PL_sig_name[sig], (gv ? GvENAME(gv)
2410 if(PL_psig_name[sig]) {
2411 sv = SvREFCNT_inc(PL_psig_name[sig]);
2413 #if !defined(PERL_IMPLICIT_CONTEXT)
2417 sv = sv_newmortal();
2418 sv_setpv(sv,PL_sig_name[sig]);
2421 PUSHSTACKi(PERLSI_SIGNAL);
2426 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2429 if (SvTRUE(ERRSV)) {
2431 #ifdef HAS_SIGPROCMASK
2432 /* Handler "died", for example to get out of a restart-able read().
2433 * Before we re-do that on its behalf re-enable the signal which was
2434 * blocked by the system when we entered.
2438 sigaddset(&set,sig);
2439 sigprocmask(SIG_UNBLOCK, &set, NULL);
2441 /* Not clear if this will work */
2442 (void)rsignal(sig, SIG_IGN);
2443 (void)rsignal(sig, &Perl_csighandler);
2445 #endif /* !PERL_MICRO */
2446 Perl_die(aTHX_ Nullformat);
2450 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2456 PL_scopestack_ix -= 1;
2459 PL_op = myop; /* Apparently not needed... */
2461 PL_Sv = tSv; /* Restore global temporaries. */
2468 restore_magic(pTHX_ void *p)
2470 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2471 SV* sv = mgs->mgs_sv;
2476 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2479 SvFLAGS(sv) |= mgs->mgs_flags;
2483 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2486 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2488 /* If we're still on top of the stack, pop us off. (That condition
2489 * will be satisfied if restore_magic was called explicitly, but *not*
2490 * if it's being called via leave_scope.)
2491 * The reason for doing this is that otherwise, things like sv_2cv()
2492 * may leave alloc gunk on the savestack, and some code
2493 * (e.g. sighandler) doesn't expect that...
2495 if (PL_savestack_ix == mgs->mgs_ss_ix)
2497 I32 popval = SSPOPINT;
2498 assert(popval == SAVEt_DESTRUCTOR_X);
2499 PL_savestack_ix -= 2;
2501 assert(popval == SAVEt_ALLOC);
2503 PL_savestack_ix -= popval;
2509 unwind_handler_stack(pTHX_ void *p)
2511 U32 flags = *(U32*)p;
2514 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2515 /* cxstack_ix-- Not needed, die already unwound it. */
2516 #if !defined(PERL_IMPLICIT_CONTEXT)
2518 SvREFCNT_dec(sig_sv);