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)
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 MGVTBL* vtbl = mg->mg_virtual;
323 if ((mg->mg_flags & MGf_COPY) && vtbl->svt_copy){
324 count += CALL_FPTR(vtbl->svt_copy)(aTHX_ sv, mg, nsv, key, klen);
326 else if (isUPPER(mg->mg_type)) {
328 mg->mg_type == PERL_MAGIC_tied ? SvTIED_obj(sv, mg) :
329 (mg->mg_type == PERL_MAGIC_regdata && mg->mg_obj)
331 toLOWER(mg->mg_type), key, klen);
341 Free any magic storage used by the SV. See C<sv_magic>.
347 Perl_mg_free(pTHX_ SV *sv)
351 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
352 MGVTBL* vtbl = mg->mg_virtual;
353 moremagic = mg->mg_moremagic;
354 if (vtbl && vtbl->svt_free)
355 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
356 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
358 Safefree(mg->mg_ptr);
359 else if (mg->mg_len == HEf_SVKEY)
360 SvREFCNT_dec((SV*)mg->mg_ptr);
362 if (mg->mg_flags & MGf_REFCOUNTED)
363 SvREFCNT_dec(mg->mg_obj);
371 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
376 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
380 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
381 if (mg->mg_obj) /* @+ */
384 return rx->lastparen;
391 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
399 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
403 if (paren <= rx->nparens &&
404 (s = rx->startp[paren]) != -1 &&
405 (t = rx->endp[paren]) != -1)
407 if (mg->mg_obj) /* @+ */
412 if (i > 0 && PL_reg_match_utf8) {
413 char *b = rx->subbeg;
415 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
425 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
427 Perl_croak(aTHX_ PL_no_modify);
433 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
440 switch (*mg->mg_ptr) {
441 case '1': case '2': case '3': case '4':
442 case '5': case '6': case '7': case '8': case '9': case '&':
443 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
445 paren = atoi(mg->mg_ptr); /* $& is in [0] */
447 if (paren <= rx->nparens &&
448 (s1 = rx->startp[paren]) != -1 &&
449 (t1 = rx->endp[paren]) != -1)
453 if (i > 0 && PL_reg_match_utf8) {
454 char *s = rx->subbeg + s1;
455 char *send = rx->subbeg + t1;
458 if (is_utf8_string((U8*)s, i))
459 i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send);
462 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
466 if (ckWARN(WARN_UNINITIALIZED))
471 if (ckWARN(WARN_UNINITIALIZED))
476 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
477 paren = rx->lastparen;
482 case '\016': /* ^N */
483 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
484 paren = rx->lastcloseparen;
490 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
491 if (rx->startp[0] != -1) {
502 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
503 if (rx->endp[0] != -1) {
504 i = rx->sublen - rx->endp[0];
515 if (!SvPOK(sv) && SvNIOK(sv)) {
525 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
532 switch (*mg->mg_ptr) {
533 case '\001': /* ^A */
534 sv_setsv(sv, PL_bodytarget);
536 case '\003': /* ^C */
537 sv_setiv(sv, (IV)PL_minus_c);
540 case '\004': /* ^D */
541 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
542 #if defined(YYDEBUG) && defined(DEBUGGING)
543 PL_yydebug = DEBUG_p_TEST;
546 case '\005': /* ^E */
547 if (*(mg->mg_ptr+1) == '\0') {
548 #ifdef MACOS_TRADITIONAL
552 sv_setnv(sv,(double)gMacPerl_OSErr);
553 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
558 # include <descrip.h>
559 # include <starlet.h>
561 $DESCRIPTOR(msgdsc,msg);
562 sv_setnv(sv,(NV) vaxc$errno);
563 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
564 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
570 if (!(_emx_env & 0x200)) { /* Under DOS */
571 sv_setnv(sv, (NV)errno);
572 sv_setpv(sv, errno ? Strerror(errno) : "");
574 if (errno != errno_isOS2) {
575 int tmp = _syserrno();
576 if (tmp) /* 2nd call to _syserrno() makes it 0 */
579 sv_setnv(sv, (NV)Perl_rc);
580 sv_setpv(sv, os2error(Perl_rc));
585 DWORD dwErr = GetLastError();
586 sv_setnv(sv, (NV)dwErr);
589 PerlProc_GetOSError(sv, dwErr);
596 sv_setnv(sv, (NV)errno);
597 sv_setpv(sv, errno ? Strerror(errno) : "");
602 SvNOK_on(sv); /* what a wonderful hack! */
604 else if (strEQ(mg->mg_ptr+1, "NCODING"))
605 sv_setsv(sv, PL_encoding);
607 case '\006': /* ^F */
608 sv_setiv(sv, (IV)PL_maxsysfd);
610 case '\010': /* ^H */
611 sv_setiv(sv, (IV)PL_hints);
613 case '\011': /* ^I */ /* NOT \t in EBCDIC */
615 sv_setpv(sv, PL_inplace);
617 sv_setsv(sv, &PL_sv_undef);
619 case '\017': /* ^O & ^OPEN */
620 if (*(mg->mg_ptr+1) == '\0')
621 sv_setpv(sv, PL_osname);
622 else if (strEQ(mg->mg_ptr, "\017PEN")) {
623 if (!PL_compiling.cop_io)
624 sv_setsv(sv, &PL_sv_undef);
626 sv_setsv(sv, PL_compiling.cop_io);
630 case '\020': /* ^P */
631 sv_setiv(sv, (IV)PL_perldb);
633 case '\023': /* ^S */
635 if (PL_lex_state != LEX_NOTPARSING)
638 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
643 case '\024': /* ^T */
644 if (*(mg->mg_ptr+1) == '\0') {
646 sv_setnv(sv, PL_basetime);
648 sv_setiv(sv, (IV)PL_basetime);
651 else if (strEQ(mg->mg_ptr, "\024AINT"))
652 sv_setiv(sv, PL_tainting);
654 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
655 if (*(mg->mg_ptr+1) == '\0')
656 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
657 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
658 if (PL_compiling.cop_warnings == pWARN_NONE ||
659 PL_compiling.cop_warnings == pWARN_STD)
661 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
663 else if (PL_compiling.cop_warnings == pWARN_ALL) {
664 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
667 sv_setsv(sv, PL_compiling.cop_warnings);
671 else if (strEQ(mg->mg_ptr+1, "IDE_SYSTEM_CALLS"))
672 sv_setiv(sv, (IV)PL_widesyscalls);
674 case '1': case '2': case '3': case '4':
675 case '5': case '6': case '7': case '8': case '9': case '&':
676 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
680 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
681 * XXX Does the new way break anything?
683 paren = atoi(mg->mg_ptr); /* $& is in [0] */
685 if (paren <= rx->nparens &&
686 (s1 = rx->startp[paren]) != -1 &&
687 (t1 = rx->endp[paren]) != -1)
696 bool was_tainted = FALSE;
698 was_tainted = PL_tainted;
702 if (PL_reg_match_utf8 && is_utf8_string((U8*)s, i))
707 PL_tainted = (was_tainted || RX_MATCH_TAINTED(rx));
712 sv_setsv(sv,&PL_sv_undef);
715 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
716 paren = rx->lastparen;
720 sv_setsv(sv,&PL_sv_undef);
722 case '\016': /* ^N */
723 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
724 paren = rx->lastcloseparen;
728 sv_setsv(sv,&PL_sv_undef);
731 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
732 if ((s = rx->subbeg) && rx->startp[0] != -1) {
737 sv_setsv(sv,&PL_sv_undef);
740 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
741 if (rx->subbeg && rx->endp[0] != -1) {
742 s = rx->subbeg + rx->endp[0];
743 i = rx->sublen - rx->endp[0];
747 sv_setsv(sv,&PL_sv_undef);
751 if (GvIO(PL_last_in_gv)) {
752 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
758 sv_setiv(sv, (IV)STATUS_CURRENT);
759 #ifdef COMPLEX_STATUS
760 LvTARGOFF(sv) = PL_statusvalue;
761 LvTARGLEN(sv) = PL_statusvalue_vms;
766 s = IoTOP_NAME(GvIOp(PL_defoutgv));
770 sv_setpv(sv,GvENAME(PL_defoutgv));
775 s = IoFMT_NAME(GvIOp(PL_defoutgv));
777 s = GvENAME(PL_defoutgv);
782 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
785 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
788 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
796 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
799 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
805 sv_setpv(sv,SvPVX(PL_ors_sv));
808 sv_setpv(sv,PL_ofmt);
812 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
813 sv_setpv(sv, errno ? Strerror(errno) : "");
816 int saveerrno = errno;
817 sv_setnv(sv, (NV)errno);
819 if (errno == errno_isOS2 || errno == errno_isOS2_set)
820 sv_setpv(sv, os2error(Perl_rc));
823 sv_setpv(sv, errno ? Strerror(errno) : "");
827 SvNOK_on(sv); /* what a wonderful hack! */
830 sv_setiv(sv, (IV)PL_uid);
833 sv_setiv(sv, (IV)PL_euid);
836 sv_setiv(sv, (IV)PL_gid);
838 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
842 sv_setiv(sv, (IV)PL_egid);
844 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
849 Groups_t gary[NGROUPS];
850 i = getgroups(NGROUPS,gary);
852 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
855 (void)SvIOK_on(sv); /* what a wonderful hack! */
859 #ifndef MACOS_TRADITIONAL
863 #ifdef USE_5005THREADS
865 sv_setsv(sv, thr->errsv);
867 #endif /* USE_5005THREADS */
873 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
875 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
877 if (uf && uf->uf_val)
878 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
883 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
893 #ifdef DYNAMIC_ENV_FETCH
894 /* We just undefd an environment var. Is a replacement */
895 /* waiting in the wings? */
898 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
899 s = SvPV(*valp, len);
903 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
904 /* And you'll never guess what the dog had */
905 /* in its mouth... */
907 MgTAINTEDDIR_off(mg);
909 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
910 char pathbuf[256], eltbuf[256], *cp, *elt = s;
914 do { /* DCL$PATH may be a search list */
915 while (1) { /* as may dev portion of any element */
916 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
917 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
918 cando_by_name(S_IWUSR,0,elt) ) {
923 if ((cp = strchr(elt, ':')) != Nullch)
925 if (my_trnlnm(elt, eltbuf, j++))
931 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
934 if (s && klen == 4 && strEQ(ptr,"PATH")) {
935 char *strend = s + len;
941 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
944 if (i >= sizeof tmpbuf /* too long -- assume the worst */
946 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
953 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
959 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
962 my_setenv(MgPV(mg,n_a),Nullch);
967 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
970 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
975 magic_clear_all_env(sv,mg);
976 hv_iterinit((HV*)sv);
977 while ((entry = hv_iternext((HV*)sv))) {
979 my_setenv(hv_iterkey(entry, &keylen),
980 SvPV(hv_iterval((HV*)sv, entry), n_a));
988 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
990 #if defined(VMS) || defined(EPOC)
991 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
993 # if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
996 # ifdef USE_ENVIRON_ARRAY
997 # ifndef PERL_USE_SAFE_PUTENV
1000 if (environ == PL_origenviron)
1001 environ = (char**)safesysmalloc(sizeof(char*));
1003 for (i = 0; environ[i]; i++)
1004 safesysfree(environ[i]);
1005 # endif /* PERL_USE_SAFE_PUTENV */
1007 environ[0] = Nullch;
1009 # endif /* USE_ENVIRON_ARRAY */
1010 # endif /* PERL_IMPLICIT_SYS || WIN32 */
1011 #endif /* VMS || EPC */
1015 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS)||defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1016 static int sig_handlers_initted = 0;
1018 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1019 static int sig_ignoring[SIG_SIZE]; /* which signals we are ignoring */
1021 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1022 static int sig_defaulting[SIG_SIZE];
1027 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
1031 /* Are we fetching a signal entry? */
1032 i = whichsig(MgPV(mg,n_a));
1035 sv_setsv(sv,PL_psig_ptr[i]);
1037 Sighandler_t sigstate;
1038 sigstate = rsignal_state(i);
1039 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1040 if (sig_handlers_initted && sig_ignoring[i]) sigstate = SIG_IGN;
1042 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1043 if (sig_handlers_initted && sig_defaulting[i]) sigstate = SIG_DFL;
1045 /* cache state so we don't fetch it again */
1046 if(sigstate == SIG_IGN)
1047 sv_setpv(sv,"IGNORE");
1049 sv_setsv(sv,&PL_sv_undef);
1050 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1057 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
1061 /* Are we clearing a signal entry? */
1062 i = whichsig(MgPV(mg,n_a));
1064 if(PL_psig_ptr[i]) {
1065 SvREFCNT_dec(PL_psig_ptr[i]);
1068 if(PL_psig_name[i]) {
1069 SvREFCNT_dec(PL_psig_name[i]);
1077 Perl_raise_signal(pTHX_ int sig)
1079 /* Set a flag to say this signal is pending */
1080 PL_psig_pend[sig]++;
1081 /* And one to say _a_ signal is pending */
1086 Perl_csighandler(int sig)
1088 #ifdef PERL_GET_SIG_CONTEXT
1089 dTHXa(PERL_GET_SIG_CONTEXT);
1093 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1094 (void) rsignal(sig, &Perl_csighandler);
1095 if (sig_ignoring[sig]) return;
1097 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1098 if (sig_defaulting[sig])
1099 #ifdef KILL_BY_SIGPRC
1100 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1106 #ifdef PERL_OLD_SIGNALS
1107 /* Call the perl level handler now with risk we may be in malloc() etc. */
1108 (*PL_sighandlerp)(sig);
1110 Perl_raise_signal(aTHX_ sig);
1114 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1116 Perl_csighandler_init(void)
1119 if (sig_handlers_initted) return;
1121 for (sig = 1; sig < SIG_SIZE; sig++) {
1122 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1124 sig_defaulting[sig] = 1;
1125 (void) rsignal(sig, &Perl_csighandler);
1127 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1128 sig_ignoring[sig] = 0;
1131 sig_handlers_initted = 1;
1136 Perl_despatch_signals(pTHX)
1140 for (sig = 1; sig < SIG_SIZE; sig++) {
1141 if (PL_psig_pend[sig]) {
1142 PL_psig_pend[sig] = 0;
1143 (*PL_sighandlerp)(sig);
1149 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1158 if (strEQ(s,"__DIE__"))
1160 else if (strEQ(s,"__WARN__"))
1163 Perl_croak(aTHX_ "No such hook: %s", s);
1171 i = whichsig(s); /* ...no, a brick */
1173 if (ckWARN(WARN_SIGNAL))
1174 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
1177 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1178 if (!sig_handlers_initted) Perl_csighandler_init();
1180 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1181 sig_ignoring[i] = 0;
1183 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1184 sig_defaulting[i] = 0;
1186 SvREFCNT_dec(PL_psig_name[i]);
1187 SvREFCNT_dec(PL_psig_ptr[i]);
1188 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1189 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1190 PL_psig_name[i] = newSVpvn(s, len);
1191 SvREADONLY_on(PL_psig_name[i]);
1193 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1195 (void)rsignal(i, &Perl_csighandler);
1197 *svp = SvREFCNT_inc(sv);
1200 s = SvPV_force(sv,len);
1201 if (strEQ(s,"IGNORE")) {
1203 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1204 sig_ignoring[i] = 1;
1205 (void)rsignal(i, &Perl_csighandler);
1207 (void)rsignal(i, SIG_IGN);
1212 else if (strEQ(s,"DEFAULT") || !*s) {
1214 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1216 sig_defaulting[i] = 1;
1217 (void)rsignal(i, &Perl_csighandler);
1220 (void)rsignal(i, SIG_DFL);
1227 * We should warn if HINT_STRICT_REFS, but without
1228 * access to a known hint bit in a known OP, we can't
1229 * tell whether HINT_STRICT_REFS is in force or not.
1231 if (!strchr(s,':') && !strchr(s,'\''))
1232 sv_insert(sv, 0, 0, "main::", 6);
1234 (void)rsignal(i, &Perl_csighandler);
1236 *svp = SvREFCNT_inc(sv);
1240 #endif /* !PERL_MICRO */
1243 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1245 PL_sub_generation++;
1250 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1252 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1253 PL_amagic_generation++;
1259 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1261 HV *hv = (HV*)LvTARG(sv);
1265 (void) hv_iterinit(hv);
1266 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1269 while (hv_iternext(hv))
1274 sv_setiv(sv, (IV)i);
1279 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1282 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1287 /* caller is responsible for stack switching/cleanup */
1289 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1295 PUSHs(SvTIED_obj(sv, mg));
1298 if (mg->mg_len >= 0)
1299 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1300 else if (mg->mg_len == HEf_SVKEY)
1301 PUSHs((SV*)mg->mg_ptr);
1303 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1304 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1312 return call_method(meth, flags);
1316 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1322 PUSHSTACKi(PERLSI_MAGIC);
1324 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1325 sv_setsv(sv, *PL_stack_sp--);
1335 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1337 magic_methpack(sv,mg,"FETCH");
1339 mg->mg_flags |= MGf_GSKIP;
1344 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1348 PUSHSTACKi(PERLSI_MAGIC);
1349 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1356 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1358 return magic_methpack(sv,mg,"DELETE");
1363 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1370 PUSHSTACKi(PERLSI_MAGIC);
1371 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1372 sv = *PL_stack_sp--;
1373 retval = (U32) SvIV(sv)-1;
1382 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1387 PUSHSTACKi(PERLSI_MAGIC);
1389 XPUSHs(SvTIED_obj(sv, mg));
1391 call_method("CLEAR", G_SCALAR|G_DISCARD);
1398 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1401 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1405 PUSHSTACKi(PERLSI_MAGIC);
1408 PUSHs(SvTIED_obj(sv, mg));
1413 if (call_method(meth, G_SCALAR))
1414 sv_setsv(key, *PL_stack_sp--);
1423 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1425 return magic_methpack(sv,mg,"EXISTS");
1429 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1439 svp = av_fetch(GvAV(gv),
1440 atoi(MgPV(mg,n_a)), FALSE);
1441 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
1447 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1449 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1454 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1456 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1461 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1463 SV* lsv = LvTARG(sv);
1465 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1466 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1467 if (mg && mg->mg_len >= 0) {
1470 sv_pos_b2u(lsv, &i);
1471 sv_setiv(sv, i + PL_curcop->cop_arybase);
1480 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1482 SV* lsv = LvTARG(sv);
1489 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1490 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1494 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1495 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1497 else if (!SvOK(sv)) {
1501 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1503 pos = SvIV(sv) - PL_curcop->cop_arybase;
1506 ulen = sv_len_utf8(lsv);
1521 sv_pos_u2b(lsv, &p, 0);
1526 mg->mg_flags &= ~MGf_MINMATCH;
1532 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1534 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1536 gv_efullname3(sv,((GV*)sv), "*");
1540 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1545 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1554 if (*s == '*' && s[1])
1556 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1561 GvGP(sv) = gp_ref(GvGP(gv));
1566 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1569 SV *lsv = LvTARG(sv);
1570 char *tmps = SvPV(lsv,len);
1571 I32 offs = LvTARGOFF(sv);
1572 I32 rem = LvTARGLEN(sv);
1575 sv_pos_u2b(lsv, &offs, &rem);
1578 if (rem + offs > len)
1580 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1587 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1590 char *tmps = SvPV(sv, len);
1591 SV *lsv = LvTARG(sv);
1592 I32 lvoff = LvTARGOFF(sv);
1593 I32 lvlen = LvTARGLEN(sv);
1596 sv_utf8_upgrade(lsv);
1597 sv_pos_u2b(lsv, &lvoff, &lvlen);
1598 sv_insert(lsv, lvoff, lvlen, tmps, len);
1601 else if (lsv && SvUTF8(lsv)) {
1602 sv_pos_u2b(lsv, &lvoff, &lvlen);
1603 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
1604 sv_insert(lsv, lvoff, lvlen, tmps, len);
1608 sv_insert(lsv, lvoff, lvlen, tmps, len);
1614 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1616 TAINT_IF((mg->mg_len & 1) ||
1617 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1622 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1624 if (PL_localizing) {
1625 if (PL_localizing == 1)
1630 else if (PL_tainted)
1638 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1640 SV *lsv = LvTARG(sv);
1647 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1652 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1654 do_vecset(sv); /* XXX slurp this routine */
1659 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1662 if (LvTARGLEN(sv)) {
1664 SV *ahv = LvTARG(sv);
1665 if (SvTYPE(ahv) == SVt_PVHV) {
1666 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1671 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1677 AV* av = (AV*)LvTARG(sv);
1678 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1679 targ = AvARRAY(av)[LvTARGOFF(sv)];
1681 if (targ && targ != &PL_sv_undef) {
1682 /* somebody else defined it for us */
1683 SvREFCNT_dec(LvTARG(sv));
1684 LvTARG(sv) = SvREFCNT_inc(targ);
1686 SvREFCNT_dec(mg->mg_obj);
1687 mg->mg_obj = Nullsv;
1688 mg->mg_flags &= ~MGf_REFCOUNTED;
1693 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1698 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1703 sv_setsv(LvTARG(sv), sv);
1704 SvSETMAGIC(LvTARG(sv));
1710 Perl_vivify_defelem(pTHX_ SV *sv)
1715 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
1718 SV *ahv = LvTARG(sv);
1720 if (SvTYPE(ahv) == SVt_PVHV) {
1721 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1726 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
1730 if (!value || value == &PL_sv_undef)
1731 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1734 AV* av = (AV*)LvTARG(sv);
1735 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1736 LvTARG(sv) = Nullsv; /* array can't be extended */
1738 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1739 if (!svp || (value = *svp) == &PL_sv_undef)
1740 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1743 (void)SvREFCNT_inc(value);
1744 SvREFCNT_dec(LvTARG(sv));
1747 SvREFCNT_dec(mg->mg_obj);
1748 mg->mg_obj = Nullsv;
1749 mg->mg_flags &= ~MGf_REFCOUNTED;
1753 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1755 AV *av = (AV*)mg->mg_obj;
1756 SV **svp = AvARRAY(av);
1757 I32 i = AvFILLp(av);
1759 if (svp[i] && svp[i] != &PL_sv_undef) {
1760 if (!SvWEAKREF(svp[i]))
1761 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1762 /* XXX Should we check that it hasn't changed? */
1764 (void)SvOK_off(svp[i]);
1765 SvWEAKREF_off(svp[i]);
1766 svp[i] = &PL_sv_undef;
1774 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1782 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1784 sv_unmagic(sv, PERL_MAGIC_bm);
1790 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1792 sv_unmagic(sv, PERL_MAGIC_fm);
1798 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1800 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1802 if (uf && uf->uf_set)
1803 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
1808 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1810 regexp *re = (regexp *)mg->mg_obj;
1815 #ifdef USE_LOCALE_COLLATE
1817 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1820 * RenE<eacute> Descartes said "I think not."
1821 * and vanished with a faint plop.
1824 Safefree(mg->mg_ptr);
1830 #endif /* USE_LOCALE_COLLATE */
1833 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1838 switch (*mg->mg_ptr) {
1839 case '\001': /* ^A */
1840 sv_setsv(PL_bodytarget, sv);
1842 case '\003': /* ^C */
1843 PL_minus_c = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1846 case '\004': /* ^D */
1847 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
1848 DEBUG_x(dump_all());
1850 case '\005': /* ^E */
1851 if (*(mg->mg_ptr+1) == '\0') {
1852 #ifdef MACOS_TRADITIONAL
1853 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1856 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1859 SetLastError( SvIV(sv) );
1862 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1864 /* will anyone ever use this? */
1865 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1871 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
1873 sv_setsv(PL_encoding, sv);
1875 PL_encoding = newSVsv(sv);
1877 case '\006': /* ^F */
1878 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1880 case '\010': /* ^H */
1881 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1883 case '\011': /* ^I */ /* NOT \t in EBCDIC */
1885 Safefree(PL_inplace);
1887 PL_inplace = savepv(SvPV(sv,len));
1889 PL_inplace = Nullch;
1891 case '\017': /* ^O */
1892 if (*(mg->mg_ptr+1) == '\0') {
1894 Safefree(PL_osname);
1896 PL_osname = savepv(SvPV(sv,len));
1900 else if (strEQ(mg->mg_ptr, "\017PEN")) {
1901 if (!PL_compiling.cop_io)
1902 PL_compiling.cop_io = newSVsv(sv);
1904 sv_setsv(PL_compiling.cop_io,sv);
1907 case '\020': /* ^P */
1908 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1909 if (PL_perldb && !PL_DBsingle)
1912 case '\024': /* ^T */
1914 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1916 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1919 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
1920 if (*(mg->mg_ptr+1) == '\0') {
1921 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1922 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1923 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1924 | (i ? G_WARN_ON : G_WARN_OFF) ;
1927 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
1928 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1929 if (!SvPOK(sv) && PL_localizing) {
1930 sv_setpvn(sv, WARN_NONEstring, WARNsize);
1931 PL_compiling.cop_warnings = pWARN_NONE;
1936 int accumulate = 0 ;
1937 int any_fatals = 0 ;
1938 char * ptr = (char*)SvPV(sv, len) ;
1939 for (i = 0 ; i < len ; ++i) {
1940 accumulate |= ptr[i] ;
1941 any_fatals |= (ptr[i] & 0xAA) ;
1944 PL_compiling.cop_warnings = pWARN_NONE;
1945 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
1946 PL_compiling.cop_warnings = pWARN_ALL;
1947 PL_dowarn |= G_WARN_ONCE ;
1950 if (specialWARN(PL_compiling.cop_warnings))
1951 PL_compiling.cop_warnings = newSVsv(sv) ;
1953 sv_setsv(PL_compiling.cop_warnings, sv);
1954 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1955 PL_dowarn |= G_WARN_ONCE ;
1961 else if (strEQ(mg->mg_ptr+1, "IDE_SYSTEM_CALLS"))
1962 PL_widesyscalls = SvTRUE(sv);
1965 if (PL_localizing) {
1966 if (PL_localizing == 1)
1967 SAVESPTR(PL_last_in_gv);
1969 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1970 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
1973 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
1974 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1975 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1978 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
1979 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1980 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1983 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1986 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1987 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1988 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
1991 IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1995 IO *io = GvIOp(PL_defoutgv);
1998 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1999 IoFLAGS(io) &= ~IOf_FLUSH;
2001 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2002 PerlIO *ofp = IoOFP(io);
2004 (void)PerlIO_flush(ofp);
2005 IoFLAGS(io) |= IOf_FLUSH;
2011 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2012 PL_multiline = (i != 0);
2015 SvREFCNT_dec(PL_rs);
2016 PL_rs = newSVsv(sv);
2020 SvREFCNT_dec(PL_ors_sv);
2021 if (SvOK(sv) || SvGMAGICAL(sv)) {
2022 PL_ors_sv = newSVsv(sv);
2030 SvREFCNT_dec(PL_ofs_sv);
2031 if (SvOK(sv) || SvGMAGICAL(sv)) {
2032 PL_ofs_sv = newSVsv(sv);
2041 PL_ofmt = savepv(SvPV(sv,len));
2044 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2047 #ifdef COMPLEX_STATUS
2048 if (PL_localizing == 2) {
2049 PL_statusvalue = LvTARGOFF(sv);
2050 PL_statusvalue_vms = LvTARGLEN(sv);
2054 #ifdef VMSISH_STATUS
2056 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
2059 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2062 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
2063 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
2066 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2067 if (PL_delaymagic) {
2068 PL_delaymagic |= DM_RUID;
2069 break; /* don't do magic till later */
2072 (void)setruid((Uid_t)PL_uid);
2075 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
2077 #ifdef HAS_SETRESUID
2078 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
2080 if (PL_uid == PL_euid) /* special case $< = $> */
2081 (void)PerlProc_setuid(PL_uid);
2083 PL_uid = PerlProc_getuid();
2084 Perl_croak(aTHX_ "setruid() not implemented");
2089 PL_uid = PerlProc_getuid();
2090 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2093 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2094 if (PL_delaymagic) {
2095 PL_delaymagic |= DM_EUID;
2096 break; /* don't do magic till later */
2099 (void)seteuid((Uid_t)PL_euid);
2102 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
2104 #ifdef HAS_SETRESUID
2105 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
2107 if (PL_euid == PL_uid) /* special case $> = $< */
2108 PerlProc_setuid(PL_euid);
2110 PL_euid = PerlProc_geteuid();
2111 Perl_croak(aTHX_ "seteuid() not implemented");
2116 PL_euid = PerlProc_geteuid();
2117 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2120 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2121 if (PL_delaymagic) {
2122 PL_delaymagic |= DM_RGID;
2123 break; /* don't do magic till later */
2126 (void)setrgid((Gid_t)PL_gid);
2129 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
2131 #ifdef HAS_SETRESGID
2132 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2134 if (PL_gid == PL_egid) /* special case $( = $) */
2135 (void)PerlProc_setgid(PL_gid);
2137 PL_gid = PerlProc_getgid();
2138 Perl_croak(aTHX_ "setrgid() not implemented");
2143 PL_gid = PerlProc_getgid();
2144 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2147 #ifdef HAS_SETGROUPS
2149 char *p = SvPV(sv, len);
2150 Groups_t gary[NGROUPS];
2155 for (i = 0; i < NGROUPS; ++i) {
2156 while (*p && !isSPACE(*p))
2165 (void)setgroups(i, gary);
2167 #else /* HAS_SETGROUPS */
2168 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2169 #endif /* HAS_SETGROUPS */
2170 if (PL_delaymagic) {
2171 PL_delaymagic |= DM_EGID;
2172 break; /* don't do magic till later */
2175 (void)setegid((Gid_t)PL_egid);
2178 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2180 #ifdef HAS_SETRESGID
2181 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2183 if (PL_egid == PL_gid) /* special case $) = $( */
2184 (void)PerlProc_setgid(PL_egid);
2186 PL_egid = PerlProc_getegid();
2187 Perl_croak(aTHX_ "setegid() not implemented");
2192 PL_egid = PerlProc_getegid();
2193 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2196 PL_chopset = SvPV_force(sv,len);
2198 #ifndef MACOS_TRADITIONAL
2200 #ifdef HAS_SETPROCTITLE
2201 /* The BSDs don't show the argv[] in ps(1) output, they
2202 * show a string from the process struct and provide
2203 * the setproctitle() routine to manipulate that. */
2206 # if __FreeBSD_version > 410001
2207 /* The leading "-" removes the "perl: " prefix,
2208 * but not the "(perl) suffix from the ps(1)
2209 * output, because that's what ps(1) shows if the
2210 * argv[] is modified. */
2211 setproctitle("-%s", s);
2212 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2213 /* This doesn't really work if you assume that
2214 * $0 = 'foobar'; will wipe out 'perl' from the $0
2215 * because in ps(1) output the result will be like
2216 * sprintf("perl: %s (perl)", s)
2217 * I guess this is a security feature:
2218 * one (a user process) cannot get rid of the original name.
2220 setproctitle("%s", s);
2227 /* See if all the arguments are contiguous in memory */
2228 for (i = 1; i < PL_origargc; i++) {
2229 if (PL_origargv[i] == s + 1
2231 || PL_origargv[i] == s + 2
2236 s += strlen(s); /* this one is ok too */
2241 /* can grab env area too? */
2242 if (PL_origenviron && (PL_origenviron[0] == s + 1)) {
2243 my_setenv("NoNe SuCh", Nullch);
2244 /* force copy of environment */
2245 for (i = 0; PL_origenviron[i]; i++)
2246 if (PL_origenviron[i] == s + 1) {
2253 PL_origalen = s - PL_origargv[0];
2255 s = SvPV_force(sv,len);
2257 if (i >= PL_origalen) {
2259 /* don't allow system to limit $0 seen by script */
2260 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2261 Copy(s, PL_origargv[0], i, char);
2262 s = PL_origargv[0]+i;
2266 Copy(s, PL_origargv[0], i, char);
2267 s = PL_origargv[0]+i;
2269 while (++i < PL_origalen)
2271 s = PL_origargv[0]+i;
2272 for (i = 1; i < PL_origargc; i++)
2273 PL_origargv[i] = Nullch;
2277 #ifdef USE_5005THREADS
2279 sv_setsv(thr->errsv, sv);
2281 #endif /* USE_5005THREADS */
2286 #ifdef USE_5005THREADS
2288 Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
2290 DEBUG_S(PerlIO_printf(Perl_debug_log,
2291 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2292 PTR2UV(thr), PTR2UV(sv)));
2294 Perl_croak(aTHX_ "panic: magic_mutexfree");
2295 MUTEX_DESTROY(MgMUTEXP(mg));
2296 COND_DESTROY(MgCONDP(mg));
2299 #endif /* USE_5005THREADS */
2302 Perl_whichsig(pTHX_ char *sig)
2304 register char **sigv;
2306 for (sigv = PL_sig_name+1; *sigv; sigv++)
2307 if (strEQ(sig,*sigv))
2308 return PL_sig_num[sigv - PL_sig_name];
2310 if (strEQ(sig,"CHLD"))
2314 if (strEQ(sig,"CLD"))
2320 #if !defined(PERL_IMPLICIT_CONTEXT)
2325 Perl_sighandler(int sig)
2327 #ifdef PERL_GET_SIG_CONTEXT
2328 dTHXa(PERL_GET_SIG_CONTEXT);
2335 SV *sv = Nullsv, *tSv = PL_Sv;
2341 if (PL_savestack_ix + 15 <= PL_savestack_max)
2343 if (PL_markstack_ptr < PL_markstack_max - 2)
2345 if (PL_retstack_ix < PL_retstack_max - 2)
2347 if (PL_scopestack_ix < PL_scopestack_max - 3)
2350 if (!PL_psig_ptr[sig]) {
2351 Perl_warn(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
2356 /* Max number of items pushed there is 3*n or 4. We cannot fix
2357 infinity, so we fix 4 (in fact 5): */
2359 PL_savestack_ix += 5; /* Protect save in progress. */
2360 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2363 PL_markstack_ptr++; /* Protect mark. */
2366 PL_retstack[PL_retstack_ix] = NULL;
2369 PL_scopestack_ix += 1;
2370 /* sv_2cv is too complicated, try a simpler variant first: */
2371 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2372 || SvTYPE(cv) != SVt_PVCV)
2373 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2375 if (!cv || !CvROOT(cv)) {
2376 if (ckWARN(WARN_SIGNAL))
2377 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
2378 PL_sig_name[sig], (gv ? GvENAME(gv)
2385 if(PL_psig_name[sig]) {
2386 sv = SvREFCNT_inc(PL_psig_name[sig]);
2388 #if !defined(PERL_IMPLICIT_CONTEXT)
2392 sv = sv_newmortal();
2393 sv_setpv(sv,PL_sig_name[sig]);
2396 PUSHSTACKi(PERLSI_SIGNAL);
2401 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2404 if (SvTRUE(ERRSV)) {
2406 #ifdef HAS_SIGPROCMASK
2407 /* Handler "died", for example to get out of a restart-able read().
2408 * Before we re-do that on its behalf re-enable the signal which was
2409 * blocked by the system when we entered.
2413 sigaddset(&set,sig);
2414 sigprocmask(SIG_UNBLOCK, &set, NULL);
2416 /* Not clear if this will work */
2417 (void)rsignal(sig, SIG_IGN);
2418 (void)rsignal(sig, &Perl_csighandler);
2420 #endif /* !PERL_MICRO */
2421 Perl_die(aTHX_ Nullformat);
2425 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2431 PL_scopestack_ix -= 1;
2434 PL_op = myop; /* Apparently not needed... */
2436 PL_Sv = tSv; /* Restore global temporaries. */
2443 restore_magic(pTHX_ void *p)
2445 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2446 SV* sv = mgs->mgs_sv;
2451 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2454 SvFLAGS(sv) |= mgs->mgs_flags;
2458 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2461 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2463 /* If we're still on top of the stack, pop us off. (That condition
2464 * will be satisfied if restore_magic was called explicitly, but *not*
2465 * if it's being called via leave_scope.)
2466 * The reason for doing this is that otherwise, things like sv_2cv()
2467 * may leave alloc gunk on the savestack, and some code
2468 * (e.g. sighandler) doesn't expect that...
2470 if (PL_savestack_ix == mgs->mgs_ss_ix)
2472 I32 popval = SSPOPINT;
2473 assert(popval == SAVEt_DESTRUCTOR_X);
2474 PL_savestack_ix -= 2;
2476 assert(popval == SAVEt_ALLOC);
2478 PL_savestack_ix -= popval;
2484 unwind_handler_stack(pTHX_ void *p)
2486 U32 flags = *(U32*)p;
2489 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2490 /* cxstack_ix-- Not needed, die already unwound it. */
2491 #if !defined(PERL_IMPLICIT_CONTEXT)
2493 SvREFCNT_dec(sig_sv);