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."
19 #if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
28 /* if you only have signal() and it resets on each signal, FAKE_PERSISTENT_SIGNAL_HANDLERS fixes */
29 #if !defined(HAS_SIGACTION) && defined(VMS)
30 # define FAKE_PERSISTENT_SIGNAL_HANDLERS
32 /* if we're doing kill() with sys$sigprc on VMS, FAKE_DEFAULT_SIGNAL_HANDLERS */
33 #if defined(KILL_BY_SIGPRC)
34 # define FAKE_DEFAULT_SIGNAL_HANDLERS
37 static void restore_magic(pTHX_ void *p);
38 static void unwind_handler_stack(pTHX_ void *p);
41 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
49 /* MGS is typedef'ed to struct magic_state in perl.h */
52 S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
55 assert(SvMAGICAL(sv));
57 SAVEDESTRUCTOR_X(restore_magic, INT2PTR(void*, (IV)mgs_ix));
59 mgs = SSPTR(mgs_ix, MGS*);
61 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
62 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
66 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
70 =for apidoc mg_magical
72 Turns on the magical status of an SV. See C<sv_magic>.
78 Perl_mg_magical(pTHX_ SV *sv)
81 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
82 MGVTBL* vtbl = mg->mg_virtual;
84 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
88 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
97 Do magic after a value is retrieved from the SV. See C<sv_magic>.
103 Perl_mg_get(pTHX_ SV *sv)
106 MAGIC *newmg, *head, *cur, *mg;
107 I32 mgs_ix = SSNEW(sizeof(MGS));
109 save_magic(mgs_ix, sv);
111 /* We must call svt_get(sv, mg) for each valid entry in the linked
112 list of magic. svt_get() may delete the current entry, add new
113 magic to the head of the list, or upgrade the SV. AMS 20010810 */
115 newmg = cur = head = mg = SvMAGIC(sv);
117 MGVTBL *vtbl = mg->mg_virtual;
119 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
120 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
121 /* Don't restore the flags for this entry if it was deleted. */
122 if (mg->mg_flags & MGf_GSKIP)
123 (SSPTR(mgs_ix, MGS *))->mgs_flags = 0;
126 mg = mg->mg_moremagic;
129 /* Have we finished with the new entries we saw? Start again
130 where we left off (unless there are more new entries). */
138 /* Were any new entries added? */
139 if (!new && (newmg = SvMAGIC(sv)) != head) {
146 restore_magic(aTHX_ INT2PTR(void *, (IV)mgs_ix));
153 Do magic after a value is assigned to the SV. See C<sv_magic>.
159 Perl_mg_set(pTHX_ SV *sv)
165 mgs_ix = SSNEW(sizeof(MGS));
166 save_magic(mgs_ix, sv);
168 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
169 MGVTBL* vtbl = mg->mg_virtual;
170 nextmg = mg->mg_moremagic; /* it may delete itself */
171 if (mg->mg_flags & MGf_GSKIP) {
172 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
173 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
175 if (vtbl && vtbl->svt_set)
176 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
179 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
184 =for apidoc mg_length
186 Report on the SV's length. See C<sv_magic>.
192 Perl_mg_length(pTHX_ SV *sv)
197 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
198 MGVTBL* vtbl = mg->mg_virtual;
199 if (vtbl && vtbl->svt_len) {
202 mgs_ix = SSNEW(sizeof(MGS));
203 save_magic(mgs_ix, sv);
204 /* omit MGf_GSKIP -- not changed here */
205 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
206 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
213 U8 *s = (U8*)SvPV(sv, len);
214 len = Perl_utf8_length(aTHX_ s, s + len);
222 Perl_mg_size(pTHX_ SV *sv)
227 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
228 MGVTBL* vtbl = mg->mg_virtual;
229 if (vtbl && vtbl->svt_len) {
232 mgs_ix = SSNEW(sizeof(MGS));
233 save_magic(mgs_ix, sv);
234 /* omit MGf_GSKIP -- not changed here */
235 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
236 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
243 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
248 Perl_croak(aTHX_ "Size magic not implemented");
257 Clear something magical that the SV represents. See C<sv_magic>.
263 Perl_mg_clear(pTHX_ SV *sv)
268 mgs_ix = SSNEW(sizeof(MGS));
269 save_magic(mgs_ix, sv);
271 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
272 MGVTBL* vtbl = mg->mg_virtual;
273 /* omit GSKIP -- never set here */
275 if (vtbl && vtbl->svt_clear)
276 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
279 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
286 Finds the magic pointer for type matching the SV. See C<sv_magic>.
292 Perl_mg_find(pTHX_ SV *sv, int type)
297 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
298 if (mg->mg_type == type)
307 Copies the magic from one SV to another. See C<sv_magic>.
313 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
317 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
318 if (isUPPER(mg->mg_type)) {
320 mg->mg_type == PERL_MAGIC_tied ? SvTIED_obj(sv, mg) :
321 (mg->mg_type == PERL_MAGIC_regdata && mg->mg_obj)
323 toLOWER(mg->mg_type), key, klen);
333 Free any magic storage used by the SV. See C<sv_magic>.
339 Perl_mg_free(pTHX_ SV *sv)
343 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
344 MGVTBL* vtbl = mg->mg_virtual;
345 moremagic = mg->mg_moremagic;
346 if (vtbl && vtbl->svt_free)
347 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
348 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
350 Safefree(mg->mg_ptr);
351 else if (mg->mg_len == HEf_SVKEY)
352 SvREFCNT_dec((SV*)mg->mg_ptr);
354 if (mg->mg_flags & MGf_REFCOUNTED)
355 SvREFCNT_dec(mg->mg_obj);
363 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
368 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
372 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
373 if (mg->mg_obj) /* @+ */
376 return rx->lastparen;
383 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
391 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
395 if (paren <= rx->nparens &&
396 (s = rx->startp[paren]) != -1 &&
397 (t = rx->endp[paren]) != -1)
399 if (mg->mg_obj) /* @+ */
404 if (i > 0 && PL_reg_match_utf8) {
405 char *b = rx->subbeg;
407 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
417 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
419 Perl_croak(aTHX_ PL_no_modify);
425 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
432 switch (*mg->mg_ptr) {
433 case '1': case '2': case '3': case '4':
434 case '5': case '6': case '7': case '8': case '9': case '&':
435 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
437 paren = atoi(mg->mg_ptr); /* $& is in [0] */
439 if (paren <= rx->nparens &&
440 (s1 = rx->startp[paren]) != -1 &&
441 (t1 = rx->endp[paren]) != -1)
445 if (i > 0 && PL_reg_match_utf8) {
446 char *s = rx->subbeg + s1;
447 char *send = rx->subbeg + t1;
450 if (is_utf8_string((U8*)s, i))
451 i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send);
454 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
460 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
461 paren = rx->lastparen;
466 case '\016': /* ^N */
467 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
468 paren = rx->lastcloseparen;
474 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
475 if (rx->startp[0] != -1) {
486 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
487 if (rx->endp[0] != -1) {
488 i = rx->sublen - rx->endp[0];
499 if (!SvPOK(sv) && SvNIOK(sv)) {
509 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
516 switch (*mg->mg_ptr) {
517 case '\001': /* ^A */
518 sv_setsv(sv, PL_bodytarget);
520 case '\003': /* ^C */
521 sv_setiv(sv, (IV)PL_minus_c);
524 case '\004': /* ^D */
525 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
526 #if defined(YYDEBUG) && defined(DEBUGGING)
527 PL_yydebug = DEBUG_p_TEST;
530 case '\005': /* ^E */
531 if (*(mg->mg_ptr+1) == '\0') {
532 #ifdef MACOS_TRADITIONAL
536 sv_setnv(sv,(double)gMacPerl_OSErr);
537 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
542 # include <descrip.h>
543 # include <starlet.h>
545 $DESCRIPTOR(msgdsc,msg);
546 sv_setnv(sv,(NV) vaxc$errno);
547 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
548 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
554 if (!(_emx_env & 0x200)) { /* Under DOS */
555 sv_setnv(sv, (NV)errno);
556 sv_setpv(sv, errno ? Strerror(errno) : "");
558 if (errno != errno_isOS2) {
559 int tmp = _syserrno();
560 if (tmp) /* 2nd call to _syserrno() makes it 0 */
563 sv_setnv(sv, (NV)Perl_rc);
564 sv_setpv(sv, os2error(Perl_rc));
569 DWORD dwErr = GetLastError();
570 sv_setnv(sv, (NV)dwErr);
573 PerlProc_GetOSError(sv, dwErr);
580 sv_setnv(sv, (NV)errno);
581 sv_setpv(sv, errno ? Strerror(errno) : "");
586 SvNOK_on(sv); /* what a wonderful hack! */
588 else if (strEQ(mg->mg_ptr+1, "NCODING"))
589 sv_setsv(sv, PL_encoding);
591 case '\006': /* ^F */
592 sv_setiv(sv, (IV)PL_maxsysfd);
594 case '\010': /* ^H */
595 sv_setiv(sv, (IV)PL_hints);
597 case '\011': /* ^I */ /* NOT \t in EBCDIC */
599 sv_setpv(sv, PL_inplace);
601 sv_setsv(sv, &PL_sv_undef);
603 case '\017': /* ^O & ^OPEN */
604 if (*(mg->mg_ptr+1) == '\0')
605 sv_setpv(sv, PL_osname);
606 else if (strEQ(mg->mg_ptr, "\017PEN")) {
607 if (!PL_compiling.cop_io)
608 sv_setsv(sv, &PL_sv_undef);
610 sv_setsv(sv, PL_compiling.cop_io);
614 case '\020': /* ^P */
615 sv_setiv(sv, (IV)PL_perldb);
617 case '\023': /* ^S */
619 if (PL_lex_state != LEX_NOTPARSING)
622 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
627 case '\024': /* ^T */
628 if (*(mg->mg_ptr+1) == '\0') {
630 sv_setnv(sv, PL_basetime);
632 sv_setiv(sv, (IV)PL_basetime);
635 else if (strEQ(mg->mg_ptr, "\024AINT"))
636 sv_setiv(sv, PL_tainting);
638 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
639 if (*(mg->mg_ptr+1) == '\0')
640 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
641 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
642 if (PL_compiling.cop_warnings == pWARN_NONE ||
643 PL_compiling.cop_warnings == pWARN_STD)
645 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
647 else if (PL_compiling.cop_warnings == pWARN_ALL) {
648 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
651 sv_setsv(sv, PL_compiling.cop_warnings);
655 else if (strEQ(mg->mg_ptr+1, "IDE_SYSTEM_CALLS"))
656 sv_setiv(sv, (IV)PL_widesyscalls);
658 case '1': case '2': case '3': case '4':
659 case '5': case '6': case '7': case '8': case '9': case '&':
660 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
664 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
665 * XXX Does the new way break anything?
667 paren = atoi(mg->mg_ptr); /* $& is in [0] */
669 if (paren <= rx->nparens &&
670 (s1 = rx->startp[paren]) != -1 &&
671 (t1 = rx->endp[paren]) != -1)
680 bool was_tainted = FALSE;
682 was_tainted = PL_tainted;
686 if (PL_reg_match_utf8 && is_utf8_string((U8*)s, i))
691 PL_tainted = (was_tainted || RX_MATCH_TAINTED(rx));
696 sv_setsv(sv,&PL_sv_undef);
699 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
700 paren = rx->lastparen;
704 sv_setsv(sv,&PL_sv_undef);
706 case '\016': /* ^N */
707 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
708 paren = rx->lastcloseparen;
712 sv_setsv(sv,&PL_sv_undef);
715 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
716 if ((s = rx->subbeg) && rx->startp[0] != -1) {
721 sv_setsv(sv,&PL_sv_undef);
724 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
725 if (rx->subbeg && rx->endp[0] != -1) {
726 s = rx->subbeg + rx->endp[0];
727 i = rx->sublen - rx->endp[0];
731 sv_setsv(sv,&PL_sv_undef);
735 if (GvIO(PL_last_in_gv)) {
736 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
742 sv_setiv(sv, (IV)STATUS_CURRENT);
743 #ifdef COMPLEX_STATUS
744 LvTARGOFF(sv) = PL_statusvalue;
745 LvTARGLEN(sv) = PL_statusvalue_vms;
750 s = IoTOP_NAME(GvIOp(PL_defoutgv));
754 sv_setpv(sv,GvENAME(PL_defoutgv));
759 s = IoFMT_NAME(GvIOp(PL_defoutgv));
761 s = GvENAME(PL_defoutgv);
766 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
769 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
772 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
780 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
783 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
789 sv_setpv(sv,SvPVX(PL_ors_sv));
792 sv_setpv(sv,PL_ofmt);
796 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
797 sv_setpv(sv, errno ? Strerror(errno) : "");
800 int saveerrno = errno;
801 sv_setnv(sv, (NV)errno);
803 if (errno == errno_isOS2 || errno == errno_isOS2_set)
804 sv_setpv(sv, os2error(Perl_rc));
807 sv_setpv(sv, errno ? Strerror(errno) : "");
811 SvNOK_on(sv); /* what a wonderful hack! */
814 sv_setiv(sv, (IV)PL_uid);
817 sv_setiv(sv, (IV)PL_euid);
820 sv_setiv(sv, (IV)PL_gid);
822 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
826 sv_setiv(sv, (IV)PL_egid);
828 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
833 Groups_t gary[NGROUPS];
834 i = getgroups(NGROUPS,gary);
836 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
839 (void)SvIOK_on(sv); /* what a wonderful hack! */
843 #ifndef MACOS_TRADITIONAL
847 #ifdef USE_5005THREADS
849 sv_setsv(sv, thr->errsv);
851 #endif /* USE_5005THREADS */
857 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
859 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
861 if (uf && uf->uf_val)
862 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
867 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
878 #ifdef DYNAMIC_ENV_FETCH
879 /* We just undefd an environment var. Is a replacement */
880 /* waiting in the wings? */
883 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
884 s = SvPV(*valp, len);
888 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
889 /* And you'll never guess what the dog had */
890 /* in its mouth... */
892 MgTAINTEDDIR_off(mg);
894 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
895 char pathbuf[256], eltbuf[256], *cp, *elt = s;
899 do { /* DCL$PATH may be a search list */
900 while (1) { /* as may dev portion of any element */
901 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
902 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
903 cando_by_name(S_IWUSR,0,elt) ) {
908 if ((cp = strchr(elt, ':')) != Nullch)
910 if (my_trnlnm(elt, eltbuf, j++))
916 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
919 if (s && klen == 4 && strEQ(ptr,"PATH")) {
920 char *strend = s + len;
925 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
928 if (i >= sizeof tmpbuf /* too long -- assume the worst */
930 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
937 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
943 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
946 my_setenv(MgPV(mg,n_a),Nullch);
951 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
954 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
959 magic_clear_all_env(sv,mg);
960 hv_iterinit((HV*)sv);
961 while ((entry = hv_iternext((HV*)sv))) {
963 my_setenv(hv_iterkey(entry, &keylen),
964 SvPV(hv_iterval((HV*)sv, entry), n_a));
972 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
974 #if defined(VMS) || defined(EPOC)
975 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
977 # if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
980 # ifdef USE_ENVIRON_ARRAY
981 # ifndef PERL_USE_SAFE_PUTENV
984 if (environ == PL_origenviron)
985 environ = (char**)safesysmalloc(sizeof(char*));
987 for (i = 0; environ[i]; i++)
988 safesysfree(environ[i]);
989 # endif /* PERL_USE_SAFE_PUTENV */
993 # endif /* USE_ENVIRON_ARRAY */
994 # endif /* PERL_IMPLICIT_SYS || WIN32 */
995 #endif /* VMS || EPC */
999 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS)||defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1000 static int sig_handlers_initted = 0;
1002 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1003 static int sig_ignoring[SIG_SIZE]; /* which signals we are ignoring */
1005 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1006 static int sig_defaulting[SIG_SIZE];
1011 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
1015 /* Are we fetching a signal entry? */
1016 i = whichsig(MgPV(mg,n_a));
1019 sv_setsv(sv,PL_psig_ptr[i]);
1021 Sighandler_t sigstate;
1022 sigstate = rsignal_state(i);
1023 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1024 if (sig_handlers_initted && sig_ignoring[i]) sigstate = SIG_IGN;
1026 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1027 if (sig_handlers_initted && sig_defaulting[i]) sigstate = SIG_DFL;
1029 /* cache state so we don't fetch it again */
1030 if(sigstate == SIG_IGN)
1031 sv_setpv(sv,"IGNORE");
1033 sv_setsv(sv,&PL_sv_undef);
1034 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1041 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
1045 /* Are we clearing a signal entry? */
1046 i = whichsig(MgPV(mg,n_a));
1048 if(PL_psig_ptr[i]) {
1049 SvREFCNT_dec(PL_psig_ptr[i]);
1052 if(PL_psig_name[i]) {
1053 SvREFCNT_dec(PL_psig_name[i]);
1061 Perl_raise_signal(pTHX_ int sig)
1063 /* Set a flag to say this signal is pending */
1064 PL_psig_pend[sig]++;
1065 /* And one to say _a_ signal is pending */
1070 Perl_csighandler(int sig)
1072 #ifndef PERL_OLD_SIGNALS
1075 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1076 (void) rsignal(sig, &Perl_csighandler);
1077 if (sig_ignoring[sig]) return;
1079 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1080 if (sig_defaulting[sig])
1081 #ifdef KILL_BY_SIGPRC
1082 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1088 #ifdef PERL_OLD_SIGNALS
1089 /* Call the perl level handler now with risk we may be in malloc() etc. */
1090 (*PL_sighandlerp)(sig);
1092 Perl_raise_signal(aTHX_ sig);
1096 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1098 Perl_csighandler_init(void)
1101 if (sig_handlers_initted) return;
1103 for (sig = 1; sig < SIG_SIZE; sig++) {
1104 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1105 sig_defaulting[sig] = 1;
1106 (void) rsignal(sig, &Perl_csighandler);
1108 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1109 sig_ignoring[sig] = 0;
1112 sig_handlers_initted = 1;
1117 Perl_despatch_signals(pTHX)
1121 for (sig = 1; sig < SIG_SIZE; sig++) {
1122 if (PL_psig_pend[sig]) {
1123 PL_psig_pend[sig] = 0;
1124 (*PL_sighandlerp)(sig);
1130 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1139 if (strEQ(s,"__DIE__"))
1141 else if (strEQ(s,"__WARN__"))
1144 Perl_croak(aTHX_ "No such hook: %s", s);
1152 i = whichsig(s); /* ...no, a brick */
1154 if (ckWARN(WARN_SIGNAL))
1155 Perl_warner(aTHX_ WARN_SIGNAL, "No such signal: SIG%s", s);
1158 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1159 if (!sig_handlers_initted) Perl_csighandler_init();
1161 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1162 sig_ignoring[i] = 0;
1164 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1165 sig_defaulting[i] = 0;
1167 SvREFCNT_dec(PL_psig_name[i]);
1168 SvREFCNT_dec(PL_psig_ptr[i]);
1169 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1170 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1171 PL_psig_name[i] = newSVpvn(s, len);
1172 SvREADONLY_on(PL_psig_name[i]);
1174 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1176 (void)rsignal(i, &Perl_csighandler);
1178 *svp = SvREFCNT_inc(sv);
1181 s = SvPV_force(sv,len);
1182 if (strEQ(s,"IGNORE")) {
1184 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1185 sig_ignoring[i] = 1;
1186 (void)rsignal(i, &Perl_csighandler);
1188 (void)rsignal(i, SIG_IGN);
1193 else if (strEQ(s,"DEFAULT") || !*s) {
1195 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1197 sig_defaulting[i] = 1;
1198 (void)rsignal(i, &Perl_csighandler);
1201 (void)rsignal(i, SIG_DFL);
1208 * We should warn if HINT_STRICT_REFS, but without
1209 * access to a known hint bit in a known OP, we can't
1210 * tell whether HINT_STRICT_REFS is in force or not.
1212 if (!strchr(s,':') && !strchr(s,'\''))
1213 sv_insert(sv, 0, 0, "main::", 6);
1215 (void)rsignal(i, &Perl_csighandler);
1217 *svp = SvREFCNT_inc(sv);
1221 #endif /* !PERL_MICRO */
1224 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1226 PL_sub_generation++;
1231 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1233 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1234 PL_amagic_generation++;
1240 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1242 HV *hv = (HV*)LvTARG(sv);
1246 (void) hv_iterinit(hv);
1247 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1250 while (hv_iternext(hv))
1255 sv_setiv(sv, (IV)i);
1260 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1263 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1268 /* caller is responsible for stack switching/cleanup */
1270 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1276 PUSHs(SvTIED_obj(sv, mg));
1279 if (mg->mg_len >= 0)
1280 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1281 else if (mg->mg_len == HEf_SVKEY)
1282 PUSHs((SV*)mg->mg_ptr);
1284 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1285 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1293 return call_method(meth, flags);
1297 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1303 PUSHSTACKi(PERLSI_MAGIC);
1305 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1306 sv_setsv(sv, *PL_stack_sp--);
1316 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1318 magic_methpack(sv,mg,"FETCH");
1320 mg->mg_flags |= MGf_GSKIP;
1325 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1329 PUSHSTACKi(PERLSI_MAGIC);
1330 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1337 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1339 return magic_methpack(sv,mg,"DELETE");
1344 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1351 PUSHSTACKi(PERLSI_MAGIC);
1352 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1353 sv = *PL_stack_sp--;
1354 retval = (U32) SvIV(sv)-1;
1363 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1368 PUSHSTACKi(PERLSI_MAGIC);
1370 XPUSHs(SvTIED_obj(sv, mg));
1372 call_method("CLEAR", G_SCALAR|G_DISCARD);
1379 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1382 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1386 PUSHSTACKi(PERLSI_MAGIC);
1389 PUSHs(SvTIED_obj(sv, mg));
1394 if (call_method(meth, G_SCALAR))
1395 sv_setsv(key, *PL_stack_sp--);
1404 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1406 return magic_methpack(sv,mg,"EXISTS");
1410 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1420 svp = av_fetch(GvAV(gv),
1421 atoi(MgPV(mg,n_a)), FALSE);
1422 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
1428 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1430 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1435 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1437 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1442 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1444 SV* lsv = LvTARG(sv);
1446 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1447 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1448 if (mg && mg->mg_len >= 0) {
1451 sv_pos_b2u(lsv, &i);
1452 sv_setiv(sv, i + PL_curcop->cop_arybase);
1461 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1463 SV* lsv = LvTARG(sv);
1470 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1471 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1475 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1476 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1478 else if (!SvOK(sv)) {
1482 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1484 pos = SvIV(sv) - PL_curcop->cop_arybase;
1487 ulen = sv_len_utf8(lsv);
1502 sv_pos_u2b(lsv, &p, 0);
1507 mg->mg_flags &= ~MGf_MINMATCH;
1513 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1515 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1517 gv_efullname3(sv,((GV*)sv), "*");
1521 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1526 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1535 if (*s == '*' && s[1])
1537 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1542 GvGP(sv) = gp_ref(GvGP(gv));
1547 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1550 SV *lsv = LvTARG(sv);
1551 char *tmps = SvPV(lsv,len);
1552 I32 offs = LvTARGOFF(sv);
1553 I32 rem = LvTARGLEN(sv);
1556 sv_pos_u2b(lsv, &offs, &rem);
1559 if (rem + offs > len)
1561 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1568 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1571 char *tmps = SvPV(sv, len);
1572 SV *lsv = LvTARG(sv);
1573 I32 lvoff = LvTARGOFF(sv);
1574 I32 lvlen = LvTARGLEN(sv);
1577 sv_utf8_upgrade(lsv);
1578 sv_pos_u2b(lsv, &lvoff, &lvlen);
1579 sv_insert(lsv, lvoff, lvlen, tmps, len);
1582 else if (lsv && SvUTF8(lsv)) {
1583 sv_pos_u2b(lsv, &lvoff, &lvlen);
1584 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
1585 sv_insert(lsv, lvoff, lvlen, tmps, len);
1589 sv_insert(lsv, lvoff, lvlen, tmps, len);
1595 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1597 TAINT_IF((mg->mg_len & 1) ||
1598 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1603 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1605 if (PL_localizing) {
1606 if (PL_localizing == 1)
1611 else if (PL_tainted)
1619 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1621 SV *lsv = LvTARG(sv);
1628 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1633 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1635 do_vecset(sv); /* XXX slurp this routine */
1640 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1643 if (LvTARGLEN(sv)) {
1645 SV *ahv = LvTARG(sv);
1646 if (SvTYPE(ahv) == SVt_PVHV) {
1647 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1652 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1658 AV* av = (AV*)LvTARG(sv);
1659 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1660 targ = AvARRAY(av)[LvTARGOFF(sv)];
1662 if (targ && targ != &PL_sv_undef) {
1663 /* somebody else defined it for us */
1664 SvREFCNT_dec(LvTARG(sv));
1665 LvTARG(sv) = SvREFCNT_inc(targ);
1667 SvREFCNT_dec(mg->mg_obj);
1668 mg->mg_obj = Nullsv;
1669 mg->mg_flags &= ~MGf_REFCOUNTED;
1674 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1679 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1684 sv_setsv(LvTARG(sv), sv);
1685 SvSETMAGIC(LvTARG(sv));
1691 Perl_vivify_defelem(pTHX_ SV *sv)
1696 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
1699 SV *ahv = LvTARG(sv);
1701 if (SvTYPE(ahv) == SVt_PVHV) {
1702 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1707 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
1711 if (!value || value == &PL_sv_undef)
1712 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1715 AV* av = (AV*)LvTARG(sv);
1716 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1717 LvTARG(sv) = Nullsv; /* array can't be extended */
1719 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1720 if (!svp || (value = *svp) == &PL_sv_undef)
1721 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1724 (void)SvREFCNT_inc(value);
1725 SvREFCNT_dec(LvTARG(sv));
1728 SvREFCNT_dec(mg->mg_obj);
1729 mg->mg_obj = Nullsv;
1730 mg->mg_flags &= ~MGf_REFCOUNTED;
1734 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1736 AV *av = (AV*)mg->mg_obj;
1737 SV **svp = AvARRAY(av);
1738 I32 i = AvFILLp(av);
1740 if (svp[i] && svp[i] != &PL_sv_undef) {
1741 if (!SvWEAKREF(svp[i]))
1742 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1743 /* XXX Should we check that it hasn't changed? */
1745 (void)SvOK_off(svp[i]);
1746 SvWEAKREF_off(svp[i]);
1747 svp[i] = &PL_sv_undef;
1755 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1763 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1765 sv_unmagic(sv, PERL_MAGIC_bm);
1771 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1773 sv_unmagic(sv, PERL_MAGIC_fm);
1779 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1781 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1783 if (uf && uf->uf_set)
1784 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
1789 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1791 regexp *re = (regexp *)mg->mg_obj;
1796 #ifdef USE_LOCALE_COLLATE
1798 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1801 * RenE<eacute> Descartes said "I think not."
1802 * and vanished with a faint plop.
1805 Safefree(mg->mg_ptr);
1811 #endif /* USE_LOCALE_COLLATE */
1814 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1819 switch (*mg->mg_ptr) {
1820 case '\001': /* ^A */
1821 sv_setsv(PL_bodytarget, sv);
1823 case '\003': /* ^C */
1824 PL_minus_c = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1827 case '\004': /* ^D */
1828 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
1829 DEBUG_x(dump_all());
1831 case '\005': /* ^E */
1832 if (*(mg->mg_ptr+1) == '\0') {
1833 #ifdef MACOS_TRADITIONAL
1834 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1837 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1840 SetLastError( SvIV(sv) );
1843 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1845 /* will anyone ever use this? */
1846 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1852 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
1854 sv_setsv(PL_encoding, sv);
1856 PL_encoding = newSVsv(sv);
1858 case '\006': /* ^F */
1859 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1861 case '\010': /* ^H */
1862 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1864 case '\011': /* ^I */ /* NOT \t in EBCDIC */
1866 Safefree(PL_inplace);
1868 PL_inplace = savepv(SvPV(sv,len));
1870 PL_inplace = Nullch;
1872 case '\017': /* ^O */
1873 if (*(mg->mg_ptr+1) == '\0') {
1875 Safefree(PL_osname);
1877 PL_osname = savepv(SvPV(sv,len));
1881 else if (strEQ(mg->mg_ptr, "\017PEN")) {
1882 if (!PL_compiling.cop_io)
1883 PL_compiling.cop_io = newSVsv(sv);
1885 sv_setsv(PL_compiling.cop_io,sv);
1888 case '\020': /* ^P */
1889 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1890 if (PL_perldb && !PL_DBsingle)
1893 case '\024': /* ^T */
1895 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1897 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1900 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
1901 if (*(mg->mg_ptr+1) == '\0') {
1902 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1903 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1904 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1905 | (i ? G_WARN_ON : G_WARN_OFF) ;
1908 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
1909 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1910 if (!SvPOK(sv) && PL_localizing) {
1911 sv_setpvn(sv, WARN_NONEstring, WARNsize);
1912 PL_compiling.cop_warnings = pWARN_NONE;
1917 int accumulate = 0 ;
1918 int any_fatals = 0 ;
1919 char * ptr = (char*)SvPV(sv, len) ;
1920 for (i = 0 ; i < len ; ++i) {
1921 accumulate |= ptr[i] ;
1922 any_fatals |= (ptr[i] & 0xAA) ;
1925 PL_compiling.cop_warnings = pWARN_NONE;
1926 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
1927 PL_compiling.cop_warnings = pWARN_ALL;
1928 PL_dowarn |= G_WARN_ONCE ;
1931 if (specialWARN(PL_compiling.cop_warnings))
1932 PL_compiling.cop_warnings = newSVsv(sv) ;
1934 sv_setsv(PL_compiling.cop_warnings, sv);
1935 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1936 PL_dowarn |= G_WARN_ONCE ;
1942 else if (strEQ(mg->mg_ptr+1, "IDE_SYSTEM_CALLS"))
1943 PL_widesyscalls = SvTRUE(sv);
1946 if (PL_localizing) {
1947 if (PL_localizing == 1)
1948 SAVESPTR(PL_last_in_gv);
1950 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1951 IoLINES(GvIOp(PL_last_in_gv)) = (long)SvIV(sv);
1954 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
1955 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1956 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1959 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
1960 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1961 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1964 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1967 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1968 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1969 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
1972 IoPAGE(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1976 IO *io = GvIOp(PL_defoutgv);
1979 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1980 IoFLAGS(io) &= ~IOf_FLUSH;
1982 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1983 PerlIO *ofp = IoOFP(io);
1985 (void)PerlIO_flush(ofp);
1986 IoFLAGS(io) |= IOf_FLUSH;
1992 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1993 PL_multiline = (i != 0);
1996 SvREFCNT_dec(PL_rs);
1997 PL_rs = newSVsv(sv);
2001 SvREFCNT_dec(PL_ors_sv);
2002 if (SvOK(sv) || SvGMAGICAL(sv)) {
2003 PL_ors_sv = newSVsv(sv);
2011 SvREFCNT_dec(PL_ofs_sv);
2012 if (SvOK(sv) || SvGMAGICAL(sv)) {
2013 PL_ofs_sv = newSVsv(sv);
2022 PL_ofmt = savepv(SvPV(sv,len));
2025 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2028 #ifdef COMPLEX_STATUS
2029 if (PL_localizing == 2) {
2030 PL_statusvalue = LvTARGOFF(sv);
2031 PL_statusvalue_vms = LvTARGLEN(sv);
2035 #ifdef VMSISH_STATUS
2037 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
2040 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2043 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
2044 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
2047 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2048 if (PL_delaymagic) {
2049 PL_delaymagic |= DM_RUID;
2050 break; /* don't do magic till later */
2053 (void)setruid((Uid_t)PL_uid);
2056 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
2058 #ifdef HAS_SETRESUID
2059 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
2061 if (PL_uid == PL_euid) /* special case $< = $> */
2062 (void)PerlProc_setuid(PL_uid);
2064 PL_uid = PerlProc_getuid();
2065 Perl_croak(aTHX_ "setruid() not implemented");
2070 PL_uid = PerlProc_getuid();
2071 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2074 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2075 if (PL_delaymagic) {
2076 PL_delaymagic |= DM_EUID;
2077 break; /* don't do magic till later */
2080 (void)seteuid((Uid_t)PL_euid);
2083 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
2085 #ifdef HAS_SETRESUID
2086 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
2088 if (PL_euid == PL_uid) /* special case $> = $< */
2089 PerlProc_setuid(PL_euid);
2091 PL_euid = PerlProc_geteuid();
2092 Perl_croak(aTHX_ "seteuid() not implemented");
2097 PL_euid = PerlProc_geteuid();
2098 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2101 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2102 if (PL_delaymagic) {
2103 PL_delaymagic |= DM_RGID;
2104 break; /* don't do magic till later */
2107 (void)setrgid((Gid_t)PL_gid);
2110 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
2112 #ifdef HAS_SETRESGID
2113 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2115 if (PL_gid == PL_egid) /* special case $( = $) */
2116 (void)PerlProc_setgid(PL_gid);
2118 PL_gid = PerlProc_getgid();
2119 Perl_croak(aTHX_ "setrgid() not implemented");
2124 PL_gid = PerlProc_getgid();
2125 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2128 #ifdef HAS_SETGROUPS
2130 char *p = SvPV(sv, len);
2131 Groups_t gary[NGROUPS];
2136 for (i = 0; i < NGROUPS; ++i) {
2137 while (*p && !isSPACE(*p))
2146 (void)setgroups(i, gary);
2148 #else /* HAS_SETGROUPS */
2149 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2150 #endif /* HAS_SETGROUPS */
2151 if (PL_delaymagic) {
2152 PL_delaymagic |= DM_EGID;
2153 break; /* don't do magic till later */
2156 (void)setegid((Gid_t)PL_egid);
2159 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2161 #ifdef HAS_SETRESGID
2162 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2164 if (PL_egid == PL_gid) /* special case $) = $( */
2165 (void)PerlProc_setgid(PL_egid);
2167 PL_egid = PerlProc_getegid();
2168 Perl_croak(aTHX_ "setegid() not implemented");
2173 PL_egid = PerlProc_getegid();
2174 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2177 PL_chopset = SvPV_force(sv,len);
2179 #ifndef MACOS_TRADITIONAL
2181 #ifdef HAS_SETPROCTITLE
2182 /* The BSDs don't show the argv[] in ps(1) output, they
2183 * show a string from the process struct and provide
2184 * the setproctitle() routine to manipulate that. */
2187 # if __FreeBSD_version > 410001
2188 /* The leading "-" removes the "perl: " prefix,
2189 * but not the "(perl) suffix from the ps(1)
2190 * output, because that's what ps(1) shows if the
2191 * argv[] is modified. */
2192 setproctitle("-%s", s);
2193 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2194 /* This doesn't really work if you assume that
2195 * $0 = 'foobar'; will wipe out 'perl' from the $0
2196 * because in ps(1) output the result will be like
2197 * sprintf("perl: %s (perl)", s)
2198 * I guess this is a security feature:
2199 * one (a user process) cannot get rid of the original name.
2201 setproctitle("%s", s);
2208 /* See if all the arguments are contiguous in memory */
2209 for (i = 1; i < PL_origargc; i++) {
2210 if (PL_origargv[i] == s + 1
2212 || PL_origargv[i] == s + 2
2217 s += strlen(s); /* this one is ok too */
2222 /* can grab env area too? */
2223 if (PL_origenviron && (PL_origenviron[0] == s + 1)) {
2224 my_setenv("NoNe SuCh", Nullch);
2225 /* force copy of environment */
2226 for (i = 0; PL_origenviron[i]; i++)
2227 if (PL_origenviron[i] == s + 1) {
2234 PL_origalen = s - PL_origargv[0];
2236 s = SvPV_force(sv,len);
2238 if (i >= PL_origalen) {
2240 /* don't allow system to limit $0 seen by script */
2241 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2242 Copy(s, PL_origargv[0], i, char);
2243 s = PL_origargv[0]+i;
2247 Copy(s, PL_origargv[0], i, char);
2248 s = PL_origargv[0]+i;
2250 while (++i < PL_origalen)
2252 s = PL_origargv[0]+i;
2253 for (i = 1; i < PL_origargc; i++)
2254 PL_origargv[i] = Nullch;
2258 #ifdef USE_5005THREADS
2260 sv_setsv(thr->errsv, sv);
2262 #endif /* USE_5005THREADS */
2267 #ifdef USE_5005THREADS
2269 Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
2271 DEBUG_S(PerlIO_printf(Perl_debug_log,
2272 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2273 PTR2UV(thr), PTR2UV(sv)));
2275 Perl_croak(aTHX_ "panic: magic_mutexfree");
2276 MUTEX_DESTROY(MgMUTEXP(mg));
2277 COND_DESTROY(MgCONDP(mg));
2280 #endif /* USE_5005THREADS */
2283 Perl_whichsig(pTHX_ char *sig)
2285 register char **sigv;
2287 for (sigv = PL_sig_name+1; *sigv; sigv++)
2288 if (strEQ(sig,*sigv))
2289 return PL_sig_num[sigv - PL_sig_name];
2291 if (strEQ(sig,"CHLD"))
2295 if (strEQ(sig,"CLD"))
2301 #if !defined(PERL_IMPLICIT_CONTEXT)
2306 Perl_sighandler(int sig)
2308 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2309 dTHXa(PL_curinterp); /* fake TLS, because signals don't do TLS */
2316 SV *sv = Nullsv, *tSv = PL_Sv;
2322 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2323 PERL_SET_THX(aTHX); /* fake TLS, see above */
2326 if (PL_savestack_ix + 15 <= PL_savestack_max)
2328 if (PL_markstack_ptr < PL_markstack_max - 2)
2330 if (PL_retstack_ix < PL_retstack_max - 2)
2332 if (PL_scopestack_ix < PL_scopestack_max - 3)
2335 if (!PL_psig_ptr[sig])
2336 Perl_die(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
2339 /* Max number of items pushed there is 3*n or 4. We cannot fix
2340 infinity, so we fix 4 (in fact 5): */
2342 PL_savestack_ix += 5; /* Protect save in progress. */
2343 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2346 PL_markstack_ptr++; /* Protect mark. */
2349 PL_retstack[PL_retstack_ix] = NULL;
2352 PL_scopestack_ix += 1;
2353 /* sv_2cv is too complicated, try a simpler variant first: */
2354 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2355 || SvTYPE(cv) != SVt_PVCV)
2356 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2358 if (!cv || !CvROOT(cv)) {
2359 if (ckWARN(WARN_SIGNAL))
2360 Perl_warner(aTHX_ WARN_SIGNAL, "SIG%s handler \"%s\" not defined.\n",
2361 PL_sig_name[sig], (gv ? GvENAME(gv)
2368 if(PL_psig_name[sig]) {
2369 sv = SvREFCNT_inc(PL_psig_name[sig]);
2371 #if !defined(PERL_IMPLICIT_CONTEXT)
2375 sv = sv_newmortal();
2376 sv_setpv(sv,PL_sig_name[sig]);
2379 PUSHSTACKi(PERLSI_SIGNAL);
2384 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2387 if (SvTRUE(ERRSV)) {
2389 #ifdef HAS_SIGPROCMASK
2390 /* Handler "died", for example to get out of a restart-able read().
2391 * Before we re-do that on its behalf re-enable the signal which was
2392 * blocked by the system when we entered.
2396 sigaddset(&set,sig);
2397 sigprocmask(SIG_UNBLOCK, &set, NULL);
2399 /* Not clear if this will work */
2400 (void)rsignal(sig, SIG_IGN);
2401 (void)rsignal(sig, &Perl_csighandler);
2403 #endif /* !PERL_MICRO */
2404 Perl_die(aTHX_ Nullformat);
2408 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2414 PL_scopestack_ix -= 1;
2417 PL_op = myop; /* Apparently not needed... */
2419 PL_Sv = tSv; /* Restore global temporaries. */
2426 restore_magic(pTHX_ void *p)
2428 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2429 SV* sv = mgs->mgs_sv;
2434 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2437 SvFLAGS(sv) |= mgs->mgs_flags;
2441 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2444 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2446 /* If we're still on top of the stack, pop us off. (That condition
2447 * will be satisfied if restore_magic was called explicitly, but *not*
2448 * if it's being called via leave_scope.)
2449 * The reason for doing this is that otherwise, things like sv_2cv()
2450 * may leave alloc gunk on the savestack, and some code
2451 * (e.g. sighandler) doesn't expect that...
2453 if (PL_savestack_ix == mgs->mgs_ss_ix)
2455 I32 popval = SSPOPINT;
2456 assert(popval == SAVEt_DESTRUCTOR_X);
2457 PL_savestack_ix -= 2;
2459 assert(popval == SAVEt_ALLOC);
2461 PL_savestack_ix -= popval;
2467 unwind_handler_stack(pTHX_ void *p)
2469 U32 flags = *(U32*)p;
2472 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2473 /* cxstack_ix-- Not needed, die already unwound it. */
2474 #if !defined(PERL_IMPLICIT_CONTEXT)
2476 SvREFCNT_dec(sig_sv);