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)
877 #ifdef DYNAMIC_ENV_FETCH
878 /* We just undefd an environment var. Is a replacement */
879 /* waiting in the wings? */
882 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
883 s = SvPV(*valp, len);
887 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
888 /* And you'll never guess what the dog had */
889 /* in its mouth... */
891 MgTAINTEDDIR_off(mg);
893 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
894 char pathbuf[256], eltbuf[256], *cp, *elt = s;
898 do { /* DCL$PATH may be a search list */
899 while (1) { /* as may dev portion of any element */
900 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
901 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
902 cando_by_name(S_IWUSR,0,elt) ) {
907 if ((cp = strchr(elt, ':')) != Nullch)
909 if (my_trnlnm(elt, eltbuf, j++))
915 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
918 if (s && klen == 4 && strEQ(ptr,"PATH")) {
919 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
1106 sig_defaulting[sig] = 1;
1107 (void) rsignal(sig, &Perl_csighandler);
1109 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1110 sig_ignoring[sig] = 0;
1113 sig_handlers_initted = 1;
1118 Perl_despatch_signals(pTHX)
1122 for (sig = 1; sig < SIG_SIZE; sig++) {
1123 if (PL_psig_pend[sig]) {
1124 PL_psig_pend[sig] = 0;
1125 (*PL_sighandlerp)(sig);
1131 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1140 if (strEQ(s,"__DIE__"))
1142 else if (strEQ(s,"__WARN__"))
1145 Perl_croak(aTHX_ "No such hook: %s", s);
1153 i = whichsig(s); /* ...no, a brick */
1155 if (ckWARN(WARN_SIGNAL))
1156 Perl_warner(aTHX_ WARN_SIGNAL, "No such signal: SIG%s", s);
1159 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1160 if (!sig_handlers_initted) Perl_csighandler_init();
1162 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1163 sig_ignoring[i] = 0;
1165 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1166 sig_defaulting[i] = 0;
1168 SvREFCNT_dec(PL_psig_name[i]);
1169 SvREFCNT_dec(PL_psig_ptr[i]);
1170 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1171 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1172 PL_psig_name[i] = newSVpvn(s, len);
1173 SvREADONLY_on(PL_psig_name[i]);
1175 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1177 (void)rsignal(i, &Perl_csighandler);
1179 *svp = SvREFCNT_inc(sv);
1182 s = SvPV_force(sv,len);
1183 if (strEQ(s,"IGNORE")) {
1185 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1186 sig_ignoring[i] = 1;
1187 (void)rsignal(i, &Perl_csighandler);
1189 (void)rsignal(i, SIG_IGN);
1194 else if (strEQ(s,"DEFAULT") || !*s) {
1196 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1198 sig_defaulting[i] = 1;
1199 (void)rsignal(i, &Perl_csighandler);
1202 (void)rsignal(i, SIG_DFL);
1209 * We should warn if HINT_STRICT_REFS, but without
1210 * access to a known hint bit in a known OP, we can't
1211 * tell whether HINT_STRICT_REFS is in force or not.
1213 if (!strchr(s,':') && !strchr(s,'\''))
1214 sv_insert(sv, 0, 0, "main::", 6);
1216 (void)rsignal(i, &Perl_csighandler);
1218 *svp = SvREFCNT_inc(sv);
1222 #endif /* !PERL_MICRO */
1225 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1227 PL_sub_generation++;
1232 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1234 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1235 PL_amagic_generation++;
1241 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1243 HV *hv = (HV*)LvTARG(sv);
1247 (void) hv_iterinit(hv);
1248 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1251 while (hv_iternext(hv))
1256 sv_setiv(sv, (IV)i);
1261 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1264 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1269 /* caller is responsible for stack switching/cleanup */
1271 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1277 PUSHs(SvTIED_obj(sv, mg));
1280 if (mg->mg_len >= 0)
1281 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1282 else if (mg->mg_len == HEf_SVKEY)
1283 PUSHs((SV*)mg->mg_ptr);
1285 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1286 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1294 return call_method(meth, flags);
1298 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1304 PUSHSTACKi(PERLSI_MAGIC);
1306 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1307 sv_setsv(sv, *PL_stack_sp--);
1317 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1319 magic_methpack(sv,mg,"FETCH");
1321 mg->mg_flags |= MGf_GSKIP;
1326 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1330 PUSHSTACKi(PERLSI_MAGIC);
1331 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1338 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1340 return magic_methpack(sv,mg,"DELETE");
1345 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1352 PUSHSTACKi(PERLSI_MAGIC);
1353 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1354 sv = *PL_stack_sp--;
1355 retval = (U32) SvIV(sv)-1;
1364 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1369 PUSHSTACKi(PERLSI_MAGIC);
1371 XPUSHs(SvTIED_obj(sv, mg));
1373 call_method("CLEAR", G_SCALAR|G_DISCARD);
1380 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1383 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1387 PUSHSTACKi(PERLSI_MAGIC);
1390 PUSHs(SvTIED_obj(sv, mg));
1395 if (call_method(meth, G_SCALAR))
1396 sv_setsv(key, *PL_stack_sp--);
1405 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1407 return magic_methpack(sv,mg,"EXISTS");
1411 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1421 svp = av_fetch(GvAV(gv),
1422 atoi(MgPV(mg,n_a)), FALSE);
1423 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
1429 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1431 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1436 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1438 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1443 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1445 SV* lsv = LvTARG(sv);
1447 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1448 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1449 if (mg && mg->mg_len >= 0) {
1452 sv_pos_b2u(lsv, &i);
1453 sv_setiv(sv, i + PL_curcop->cop_arybase);
1462 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1464 SV* lsv = LvTARG(sv);
1471 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1472 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1476 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1477 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1479 else if (!SvOK(sv)) {
1483 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1485 pos = SvIV(sv) - PL_curcop->cop_arybase;
1488 ulen = sv_len_utf8(lsv);
1503 sv_pos_u2b(lsv, &p, 0);
1508 mg->mg_flags &= ~MGf_MINMATCH;
1514 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1516 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1518 gv_efullname3(sv,((GV*)sv), "*");
1522 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1527 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1536 if (*s == '*' && s[1])
1538 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1543 GvGP(sv) = gp_ref(GvGP(gv));
1548 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1551 SV *lsv = LvTARG(sv);
1552 char *tmps = SvPV(lsv,len);
1553 I32 offs = LvTARGOFF(sv);
1554 I32 rem = LvTARGLEN(sv);
1557 sv_pos_u2b(lsv, &offs, &rem);
1560 if (rem + offs > len)
1562 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1569 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1572 char *tmps = SvPV(sv, len);
1573 SV *lsv = LvTARG(sv);
1574 I32 lvoff = LvTARGOFF(sv);
1575 I32 lvlen = LvTARGLEN(sv);
1578 sv_utf8_upgrade(lsv);
1579 sv_pos_u2b(lsv, &lvoff, &lvlen);
1580 sv_insert(lsv, lvoff, lvlen, tmps, len);
1583 else if (lsv && SvUTF8(lsv)) {
1584 sv_pos_u2b(lsv, &lvoff, &lvlen);
1585 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
1586 sv_insert(lsv, lvoff, lvlen, tmps, len);
1590 sv_insert(lsv, lvoff, lvlen, tmps, len);
1596 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1598 TAINT_IF((mg->mg_len & 1) ||
1599 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1604 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1606 if (PL_localizing) {
1607 if (PL_localizing == 1)
1612 else if (PL_tainted)
1620 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1622 SV *lsv = LvTARG(sv);
1629 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1634 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1636 do_vecset(sv); /* XXX slurp this routine */
1641 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1644 if (LvTARGLEN(sv)) {
1646 SV *ahv = LvTARG(sv);
1647 if (SvTYPE(ahv) == SVt_PVHV) {
1648 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1653 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1659 AV* av = (AV*)LvTARG(sv);
1660 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1661 targ = AvARRAY(av)[LvTARGOFF(sv)];
1663 if (targ && targ != &PL_sv_undef) {
1664 /* somebody else defined it for us */
1665 SvREFCNT_dec(LvTARG(sv));
1666 LvTARG(sv) = SvREFCNT_inc(targ);
1668 SvREFCNT_dec(mg->mg_obj);
1669 mg->mg_obj = Nullsv;
1670 mg->mg_flags &= ~MGf_REFCOUNTED;
1675 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1680 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1685 sv_setsv(LvTARG(sv), sv);
1686 SvSETMAGIC(LvTARG(sv));
1692 Perl_vivify_defelem(pTHX_ SV *sv)
1697 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
1700 SV *ahv = LvTARG(sv);
1702 if (SvTYPE(ahv) == SVt_PVHV) {
1703 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1708 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
1712 if (!value || value == &PL_sv_undef)
1713 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1716 AV* av = (AV*)LvTARG(sv);
1717 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1718 LvTARG(sv) = Nullsv; /* array can't be extended */
1720 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1721 if (!svp || (value = *svp) == &PL_sv_undef)
1722 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1725 (void)SvREFCNT_inc(value);
1726 SvREFCNT_dec(LvTARG(sv));
1729 SvREFCNT_dec(mg->mg_obj);
1730 mg->mg_obj = Nullsv;
1731 mg->mg_flags &= ~MGf_REFCOUNTED;
1735 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1737 AV *av = (AV*)mg->mg_obj;
1738 SV **svp = AvARRAY(av);
1739 I32 i = AvFILLp(av);
1741 if (svp[i] && svp[i] != &PL_sv_undef) {
1742 if (!SvWEAKREF(svp[i]))
1743 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1744 /* XXX Should we check that it hasn't changed? */
1746 (void)SvOK_off(svp[i]);
1747 SvWEAKREF_off(svp[i]);
1748 svp[i] = &PL_sv_undef;
1756 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1764 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1766 sv_unmagic(sv, PERL_MAGIC_bm);
1772 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1774 sv_unmagic(sv, PERL_MAGIC_fm);
1780 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1782 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1784 if (uf && uf->uf_set)
1785 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
1790 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1792 regexp *re = (regexp *)mg->mg_obj;
1797 #ifdef USE_LOCALE_COLLATE
1799 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1802 * RenE<eacute> Descartes said "I think not."
1803 * and vanished with a faint plop.
1806 Safefree(mg->mg_ptr);
1812 #endif /* USE_LOCALE_COLLATE */
1815 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1820 switch (*mg->mg_ptr) {
1821 case '\001': /* ^A */
1822 sv_setsv(PL_bodytarget, sv);
1824 case '\003': /* ^C */
1825 PL_minus_c = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1828 case '\004': /* ^D */
1829 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
1830 DEBUG_x(dump_all());
1832 case '\005': /* ^E */
1833 if (*(mg->mg_ptr+1) == '\0') {
1834 #ifdef MACOS_TRADITIONAL
1835 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1838 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1841 SetLastError( SvIV(sv) );
1844 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1846 /* will anyone ever use this? */
1847 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1853 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
1855 sv_setsv(PL_encoding, sv);
1857 PL_encoding = newSVsv(sv);
1859 case '\006': /* ^F */
1860 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1862 case '\010': /* ^H */
1863 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1865 case '\011': /* ^I */ /* NOT \t in EBCDIC */
1867 Safefree(PL_inplace);
1869 PL_inplace = savepv(SvPV(sv,len));
1871 PL_inplace = Nullch;
1873 case '\017': /* ^O */
1874 if (*(mg->mg_ptr+1) == '\0') {
1876 Safefree(PL_osname);
1878 PL_osname = savepv(SvPV(sv,len));
1882 else if (strEQ(mg->mg_ptr, "\017PEN")) {
1883 if (!PL_compiling.cop_io)
1884 PL_compiling.cop_io = newSVsv(sv);
1886 sv_setsv(PL_compiling.cop_io,sv);
1889 case '\020': /* ^P */
1890 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1891 if (PL_perldb && !PL_DBsingle)
1894 case '\024': /* ^T */
1896 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1898 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1901 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
1902 if (*(mg->mg_ptr+1) == '\0') {
1903 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1904 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1905 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1906 | (i ? G_WARN_ON : G_WARN_OFF) ;
1909 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
1910 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1911 if (!SvPOK(sv) && PL_localizing) {
1912 sv_setpvn(sv, WARN_NONEstring, WARNsize);
1913 PL_compiling.cop_warnings = pWARN_NONE;
1918 int accumulate = 0 ;
1919 int any_fatals = 0 ;
1920 char * ptr = (char*)SvPV(sv, len) ;
1921 for (i = 0 ; i < len ; ++i) {
1922 accumulate |= ptr[i] ;
1923 any_fatals |= (ptr[i] & 0xAA) ;
1926 PL_compiling.cop_warnings = pWARN_NONE;
1927 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
1928 PL_compiling.cop_warnings = pWARN_ALL;
1929 PL_dowarn |= G_WARN_ONCE ;
1932 if (specialWARN(PL_compiling.cop_warnings))
1933 PL_compiling.cop_warnings = newSVsv(sv) ;
1935 sv_setsv(PL_compiling.cop_warnings, sv);
1936 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1937 PL_dowarn |= G_WARN_ONCE ;
1943 else if (strEQ(mg->mg_ptr+1, "IDE_SYSTEM_CALLS"))
1944 PL_widesyscalls = SvTRUE(sv);
1947 if (PL_localizing) {
1948 if (PL_localizing == 1)
1949 SAVESPTR(PL_last_in_gv);
1951 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1952 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
1955 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
1956 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1957 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1960 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
1961 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1962 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1965 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1968 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1969 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1970 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
1973 IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1977 IO *io = GvIOp(PL_defoutgv);
1980 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1981 IoFLAGS(io) &= ~IOf_FLUSH;
1983 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1984 PerlIO *ofp = IoOFP(io);
1986 (void)PerlIO_flush(ofp);
1987 IoFLAGS(io) |= IOf_FLUSH;
1993 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1994 PL_multiline = (i != 0);
1997 SvREFCNT_dec(PL_rs);
1998 PL_rs = newSVsv(sv);
2002 SvREFCNT_dec(PL_ors_sv);
2003 if (SvOK(sv) || SvGMAGICAL(sv)) {
2004 PL_ors_sv = newSVsv(sv);
2012 SvREFCNT_dec(PL_ofs_sv);
2013 if (SvOK(sv) || SvGMAGICAL(sv)) {
2014 PL_ofs_sv = newSVsv(sv);
2023 PL_ofmt = savepv(SvPV(sv,len));
2026 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2029 #ifdef COMPLEX_STATUS
2030 if (PL_localizing == 2) {
2031 PL_statusvalue = LvTARGOFF(sv);
2032 PL_statusvalue_vms = LvTARGLEN(sv);
2036 #ifdef VMSISH_STATUS
2038 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
2041 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2044 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
2045 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
2048 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2049 if (PL_delaymagic) {
2050 PL_delaymagic |= DM_RUID;
2051 break; /* don't do magic till later */
2054 (void)setruid((Uid_t)PL_uid);
2057 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
2059 #ifdef HAS_SETRESUID
2060 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
2062 if (PL_uid == PL_euid) /* special case $< = $> */
2063 (void)PerlProc_setuid(PL_uid);
2065 PL_uid = PerlProc_getuid();
2066 Perl_croak(aTHX_ "setruid() not implemented");
2071 PL_uid = PerlProc_getuid();
2072 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2075 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2076 if (PL_delaymagic) {
2077 PL_delaymagic |= DM_EUID;
2078 break; /* don't do magic till later */
2081 (void)seteuid((Uid_t)PL_euid);
2084 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
2086 #ifdef HAS_SETRESUID
2087 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
2089 if (PL_euid == PL_uid) /* special case $> = $< */
2090 PerlProc_setuid(PL_euid);
2092 PL_euid = PerlProc_geteuid();
2093 Perl_croak(aTHX_ "seteuid() not implemented");
2098 PL_euid = PerlProc_geteuid();
2099 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2102 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2103 if (PL_delaymagic) {
2104 PL_delaymagic |= DM_RGID;
2105 break; /* don't do magic till later */
2108 (void)setrgid((Gid_t)PL_gid);
2111 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
2113 #ifdef HAS_SETRESGID
2114 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2116 if (PL_gid == PL_egid) /* special case $( = $) */
2117 (void)PerlProc_setgid(PL_gid);
2119 PL_gid = PerlProc_getgid();
2120 Perl_croak(aTHX_ "setrgid() not implemented");
2125 PL_gid = PerlProc_getgid();
2126 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2129 #ifdef HAS_SETGROUPS
2131 char *p = SvPV(sv, len);
2132 Groups_t gary[NGROUPS];
2137 for (i = 0; i < NGROUPS; ++i) {
2138 while (*p && !isSPACE(*p))
2147 (void)setgroups(i, gary);
2149 #else /* HAS_SETGROUPS */
2150 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2151 #endif /* HAS_SETGROUPS */
2152 if (PL_delaymagic) {
2153 PL_delaymagic |= DM_EGID;
2154 break; /* don't do magic till later */
2157 (void)setegid((Gid_t)PL_egid);
2160 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2162 #ifdef HAS_SETRESGID
2163 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2165 if (PL_egid == PL_gid) /* special case $) = $( */
2166 (void)PerlProc_setgid(PL_egid);
2168 PL_egid = PerlProc_getegid();
2169 Perl_croak(aTHX_ "setegid() not implemented");
2174 PL_egid = PerlProc_getegid();
2175 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2178 PL_chopset = SvPV_force(sv,len);
2180 #ifndef MACOS_TRADITIONAL
2182 #ifdef HAS_SETPROCTITLE
2183 /* The BSDs don't show the argv[] in ps(1) output, they
2184 * show a string from the process struct and provide
2185 * the setproctitle() routine to manipulate that. */
2188 # if __FreeBSD_version > 410001
2189 /* The leading "-" removes the "perl: " prefix,
2190 * but not the "(perl) suffix from the ps(1)
2191 * output, because that's what ps(1) shows if the
2192 * argv[] is modified. */
2193 setproctitle("-%s", s);
2194 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2195 /* This doesn't really work if you assume that
2196 * $0 = 'foobar'; will wipe out 'perl' from the $0
2197 * because in ps(1) output the result will be like
2198 * sprintf("perl: %s (perl)", s)
2199 * I guess this is a security feature:
2200 * one (a user process) cannot get rid of the original name.
2202 setproctitle("%s", s);
2209 /* See if all the arguments are contiguous in memory */
2210 for (i = 1; i < PL_origargc; i++) {
2211 if (PL_origargv[i] == s + 1
2213 || PL_origargv[i] == s + 2
2218 s += strlen(s); /* this one is ok too */
2223 /* can grab env area too? */
2224 if (PL_origenviron && (PL_origenviron[0] == s + 1)) {
2225 my_setenv("NoNe SuCh", Nullch);
2226 /* force copy of environment */
2227 for (i = 0; PL_origenviron[i]; i++)
2228 if (PL_origenviron[i] == s + 1) {
2235 PL_origalen = s - PL_origargv[0];
2237 s = SvPV_force(sv,len);
2239 if (i >= PL_origalen) {
2241 /* don't allow system to limit $0 seen by script */
2242 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2243 Copy(s, PL_origargv[0], i, char);
2244 s = PL_origargv[0]+i;
2248 Copy(s, PL_origargv[0], i, char);
2249 s = PL_origargv[0]+i;
2251 while (++i < PL_origalen)
2253 s = PL_origargv[0]+i;
2254 for (i = 1; i < PL_origargc; i++)
2255 PL_origargv[i] = Nullch;
2259 #ifdef USE_5005THREADS
2261 sv_setsv(thr->errsv, sv);
2263 #endif /* USE_5005THREADS */
2268 #ifdef USE_5005THREADS
2270 Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
2272 DEBUG_S(PerlIO_printf(Perl_debug_log,
2273 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2274 PTR2UV(thr), PTR2UV(sv)));
2276 Perl_croak(aTHX_ "panic: magic_mutexfree");
2277 MUTEX_DESTROY(MgMUTEXP(mg));
2278 COND_DESTROY(MgCONDP(mg));
2281 #endif /* USE_5005THREADS */
2284 Perl_whichsig(pTHX_ char *sig)
2286 register char **sigv;
2288 for (sigv = PL_sig_name+1; *sigv; sigv++)
2289 if (strEQ(sig,*sigv))
2290 return PL_sig_num[sigv - PL_sig_name];
2292 if (strEQ(sig,"CHLD"))
2296 if (strEQ(sig,"CLD"))
2302 #if !defined(PERL_IMPLICIT_CONTEXT)
2307 Perl_sighandler(int sig)
2309 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2310 dTHXa(PL_curinterp); /* fake TLS, because signals don't do TLS */
2317 SV *sv = Nullsv, *tSv = PL_Sv;
2323 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2324 PERL_SET_THX(aTHX); /* fake TLS, see above */
2327 if (PL_savestack_ix + 15 <= PL_savestack_max)
2329 if (PL_markstack_ptr < PL_markstack_max - 2)
2331 if (PL_retstack_ix < PL_retstack_max - 2)
2333 if (PL_scopestack_ix < PL_scopestack_max - 3)
2336 if (!PL_psig_ptr[sig])
2337 Perl_die(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
2340 /* Max number of items pushed there is 3*n or 4. We cannot fix
2341 infinity, so we fix 4 (in fact 5): */
2343 PL_savestack_ix += 5; /* Protect save in progress. */
2344 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2347 PL_markstack_ptr++; /* Protect mark. */
2350 PL_retstack[PL_retstack_ix] = NULL;
2353 PL_scopestack_ix += 1;
2354 /* sv_2cv is too complicated, try a simpler variant first: */
2355 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2356 || SvTYPE(cv) != SVt_PVCV)
2357 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2359 if (!cv || !CvROOT(cv)) {
2360 if (ckWARN(WARN_SIGNAL))
2361 Perl_warner(aTHX_ WARN_SIGNAL, "SIG%s handler \"%s\" not defined.\n",
2362 PL_sig_name[sig], (gv ? GvENAME(gv)
2369 if(PL_psig_name[sig]) {
2370 sv = SvREFCNT_inc(PL_psig_name[sig]);
2372 #if !defined(PERL_IMPLICIT_CONTEXT)
2376 sv = sv_newmortal();
2377 sv_setpv(sv,PL_sig_name[sig]);
2380 PUSHSTACKi(PERLSI_SIGNAL);
2385 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2388 if (SvTRUE(ERRSV)) {
2390 #ifdef HAS_SIGPROCMASK
2391 /* Handler "died", for example to get out of a restart-able read().
2392 * Before we re-do that on its behalf re-enable the signal which was
2393 * blocked by the system when we entered.
2397 sigaddset(&set,sig);
2398 sigprocmask(SIG_UNBLOCK, &set, NULL);
2400 /* Not clear if this will work */
2401 (void)rsignal(sig, SIG_IGN);
2402 (void)rsignal(sig, &Perl_csighandler);
2404 #endif /* !PERL_MICRO */
2405 Perl_die(aTHX_ Nullformat);
2409 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2415 PL_scopestack_ix -= 1;
2418 PL_op = myop; /* Apparently not needed... */
2420 PL_Sv = tSv; /* Restore global temporaries. */
2427 restore_magic(pTHX_ void *p)
2429 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2430 SV* sv = mgs->mgs_sv;
2435 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2438 SvFLAGS(sv) |= mgs->mgs_flags;
2442 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2445 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2447 /* If we're still on top of the stack, pop us off. (That condition
2448 * will be satisfied if restore_magic was called explicitly, but *not*
2449 * if it's being called via leave_scope.)
2450 * The reason for doing this is that otherwise, things like sv_2cv()
2451 * may leave alloc gunk on the savestack, and some code
2452 * (e.g. sighandler) doesn't expect that...
2454 if (PL_savestack_ix == mgs->mgs_ss_ix)
2456 I32 popval = SSPOPINT;
2457 assert(popval == SAVEt_DESTRUCTOR_X);
2458 PL_savestack_ix -= 2;
2460 assert(popval == SAVEt_ALLOC);
2462 PL_savestack_ix -= popval;
2468 unwind_handler_stack(pTHX_ void *p)
2470 U32 flags = *(U32*)p;
2473 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2474 /* cxstack_ix-- Not needed, die already unwound it. */
2475 #if !defined(PERL_IMPLICIT_CONTEXT)
2477 SvREFCNT_dec(sig_sv);