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 <= (I32)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 <= (I32)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 <= (I32)rx->nparens &&
686 (s1 = rx->startp[paren]) != -1 &&
687 (t1 = rx->endp[paren]) != -1)
697 if (PL_reg_match_utf8 && is_utf8_string((U8*)s, i))
702 PL_tainted = PL_tainted || !!RX_MATCH_TAINTED(rx);
703 if (RX_MATCH_TAINTED(rx)) {
704 MAGIC* mg = SvMAGIC(sv);
706 SvMAGIC(sv) = mg->mg_moremagic;
708 if ((mgt = SvMAGIC(sv))) {
709 mg->mg_moremagic = mgt;
718 sv_setsv(sv,&PL_sv_undef);
721 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
722 paren = rx->lastparen;
726 sv_setsv(sv,&PL_sv_undef);
728 case '\016': /* ^N */
729 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
730 paren = rx->lastcloseparen;
734 sv_setsv(sv,&PL_sv_undef);
737 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
738 if ((s = rx->subbeg) && rx->startp[0] != -1) {
743 sv_setsv(sv,&PL_sv_undef);
746 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
747 if (rx->subbeg && rx->endp[0] != -1) {
748 s = rx->subbeg + rx->endp[0];
749 i = rx->sublen - rx->endp[0];
753 sv_setsv(sv,&PL_sv_undef);
757 if (GvIO(PL_last_in_gv)) {
758 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
764 sv_setiv(sv, (IV)STATUS_CURRENT);
765 #ifdef COMPLEX_STATUS
766 LvTARGOFF(sv) = PL_statusvalue;
767 LvTARGLEN(sv) = PL_statusvalue_vms;
772 s = IoTOP_NAME(GvIOp(PL_defoutgv));
776 sv_setpv(sv,GvENAME(PL_defoutgv));
781 s = IoFMT_NAME(GvIOp(PL_defoutgv));
783 s = GvENAME(PL_defoutgv);
788 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
791 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
794 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
802 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
805 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
811 sv_setpv(sv,SvPVX(PL_ors_sv));
814 sv_setpv(sv,PL_ofmt);
818 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
819 sv_setpv(sv, errno ? Strerror(errno) : "");
822 int saveerrno = errno;
823 sv_setnv(sv, (NV)errno);
825 if (errno == errno_isOS2 || errno == errno_isOS2_set)
826 sv_setpv(sv, os2error(Perl_rc));
829 sv_setpv(sv, errno ? Strerror(errno) : "");
833 SvNOK_on(sv); /* what a wonderful hack! */
836 sv_setiv(sv, (IV)PL_uid);
839 sv_setiv(sv, (IV)PL_euid);
842 sv_setiv(sv, (IV)PL_gid);
844 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
848 sv_setiv(sv, (IV)PL_egid);
850 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
855 Groups_t gary[NGROUPS];
856 i = getgroups(NGROUPS,gary);
858 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
861 (void)SvIOK_on(sv); /* what a wonderful hack! */
865 #ifndef MACOS_TRADITIONAL
869 #ifdef USE_5005THREADS
871 sv_setsv(sv, thr->errsv);
873 #endif /* USE_5005THREADS */
879 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
881 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
883 if (uf && uf->uf_val)
884 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
889 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
899 #ifdef DYNAMIC_ENV_FETCH
900 /* We just undefd an environment var. Is a replacement */
901 /* waiting in the wings? */
904 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
905 s = SvPV(*valp, len);
909 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
910 /* And you'll never guess what the dog had */
911 /* in its mouth... */
913 MgTAINTEDDIR_off(mg);
915 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
916 char pathbuf[256], eltbuf[256], *cp, *elt = s;
920 do { /* DCL$PATH may be a search list */
921 while (1) { /* as may dev portion of any element */
922 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
923 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
924 cando_by_name(S_IWUSR,0,elt) ) {
929 if ((cp = strchr(elt, ':')) != Nullch)
931 if (my_trnlnm(elt, eltbuf, j++))
937 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
940 if (s && klen == 4 && strEQ(ptr,"PATH")) {
941 char *strend = s + len;
947 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
950 if (i >= sizeof tmpbuf /* too long -- assume the worst */
952 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
959 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
965 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
968 my_setenv(MgPV(mg,n_a),Nullch);
973 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
976 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
981 magic_clear_all_env(sv,mg);
982 hv_iterinit((HV*)sv);
983 while ((entry = hv_iternext((HV*)sv))) {
985 my_setenv(hv_iterkey(entry, &keylen),
986 SvPV(hv_iterval((HV*)sv, entry), n_a));
994 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
996 #if defined(VMS) || defined(EPOC)
997 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
999 # if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
1002 # ifdef USE_ENVIRON_ARRAY
1003 # if defined(USE_ITHREADS)
1004 /* only the parent thread can clobber the process environment */
1005 if (PL_curinterp == aTHX)
1008 # ifndef PERL_USE_SAFE_PUTENV
1011 if (environ == PL_origenviron)
1012 environ = (char**)safesysmalloc(sizeof(char*));
1014 for (i = 0; environ[i]; i++)
1015 safesysfree(environ[i]);
1016 # endif /* PERL_USE_SAFE_PUTENV */
1018 environ[0] = Nullch;
1020 # endif /* USE_ENVIRON_ARRAY */
1021 # endif /* PERL_IMPLICIT_SYS || WIN32 */
1022 #endif /* VMS || EPC */
1026 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS)||defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1027 static int sig_handlers_initted = 0;
1029 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1030 static int sig_ignoring[SIG_SIZE]; /* which signals we are ignoring */
1032 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1033 static int sig_defaulting[SIG_SIZE];
1038 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
1042 /* Are we fetching a signal entry? */
1043 i = whichsig(MgPV(mg,n_a));
1046 sv_setsv(sv,PL_psig_ptr[i]);
1048 Sighandler_t sigstate;
1049 sigstate = rsignal_state(i);
1050 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1051 if (sig_handlers_initted && sig_ignoring[i]) sigstate = SIG_IGN;
1053 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1054 if (sig_handlers_initted && sig_defaulting[i]) sigstate = SIG_DFL;
1056 /* cache state so we don't fetch it again */
1057 if(sigstate == SIG_IGN)
1058 sv_setpv(sv,"IGNORE");
1060 sv_setsv(sv,&PL_sv_undef);
1061 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1068 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
1072 /* Are we clearing a signal entry? */
1073 i = whichsig(MgPV(mg,n_a));
1075 if(PL_psig_ptr[i]) {
1076 SvREFCNT_dec(PL_psig_ptr[i]);
1079 if(PL_psig_name[i]) {
1080 SvREFCNT_dec(PL_psig_name[i]);
1088 Perl_raise_signal(pTHX_ int sig)
1090 /* Set a flag to say this signal is pending */
1091 PL_psig_pend[sig]++;
1092 /* And one to say _a_ signal is pending */
1097 Perl_csighandler(int sig)
1099 #ifdef PERL_GET_SIG_CONTEXT
1100 dTHXa(PERL_GET_SIG_CONTEXT);
1104 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1105 (void) rsignal(sig, &Perl_csighandler);
1106 if (sig_ignoring[sig]) return;
1108 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1109 if (sig_defaulting[sig])
1110 #ifdef KILL_BY_SIGPRC
1111 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1117 #ifdef PERL_OLD_SIGNALS
1118 /* Call the perl level handler now with risk we may be in malloc() etc. */
1119 (*PL_sighandlerp)(sig);
1121 Perl_raise_signal(aTHX_ sig);
1125 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1127 Perl_csighandler_init(void)
1130 if (sig_handlers_initted) return;
1132 for (sig = 1; sig < SIG_SIZE; sig++) {
1133 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1135 sig_defaulting[sig] = 1;
1136 (void) rsignal(sig, &Perl_csighandler);
1138 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1139 sig_ignoring[sig] = 0;
1142 sig_handlers_initted = 1;
1147 Perl_despatch_signals(pTHX)
1151 for (sig = 1; sig < SIG_SIZE; sig++) {
1152 if (PL_psig_pend[sig]) {
1153 PL_psig_pend[sig] = 0;
1154 (*PL_sighandlerp)(sig);
1160 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1169 if (strEQ(s,"__DIE__"))
1171 else if (strEQ(s,"__WARN__"))
1174 Perl_croak(aTHX_ "No such hook: %s", s);
1182 i = whichsig(s); /* ...no, a brick */
1184 if (ckWARN(WARN_SIGNAL))
1185 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
1188 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1189 if (!sig_handlers_initted) Perl_csighandler_init();
1191 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1192 sig_ignoring[i] = 0;
1194 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1195 sig_defaulting[i] = 0;
1197 SvREFCNT_dec(PL_psig_name[i]);
1198 SvREFCNT_dec(PL_psig_ptr[i]);
1199 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1200 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1201 PL_psig_name[i] = newSVpvn(s, len);
1202 SvREADONLY_on(PL_psig_name[i]);
1204 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1206 (void)rsignal(i, &Perl_csighandler);
1208 *svp = SvREFCNT_inc(sv);
1211 s = SvPV_force(sv,len);
1212 if (strEQ(s,"IGNORE")) {
1214 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1215 sig_ignoring[i] = 1;
1216 (void)rsignal(i, &Perl_csighandler);
1218 (void)rsignal(i, SIG_IGN);
1223 else if (strEQ(s,"DEFAULT") || !*s) {
1225 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1227 sig_defaulting[i] = 1;
1228 (void)rsignal(i, &Perl_csighandler);
1231 (void)rsignal(i, SIG_DFL);
1238 * We should warn if HINT_STRICT_REFS, but without
1239 * access to a known hint bit in a known OP, we can't
1240 * tell whether HINT_STRICT_REFS is in force or not.
1242 if (!strchr(s,':') && !strchr(s,'\''))
1243 sv_insert(sv, 0, 0, "main::", 6);
1245 (void)rsignal(i, &Perl_csighandler);
1247 *svp = SvREFCNT_inc(sv);
1251 #endif /* !PERL_MICRO */
1254 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1256 PL_sub_generation++;
1261 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1263 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1264 PL_amagic_generation++;
1270 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1272 HV *hv = (HV*)LvTARG(sv);
1276 (void) hv_iterinit(hv);
1277 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1280 while (hv_iternext(hv))
1285 sv_setiv(sv, (IV)i);
1290 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1293 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1298 /* caller is responsible for stack switching/cleanup */
1300 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1306 PUSHs(SvTIED_obj(sv, mg));
1309 if (mg->mg_len >= 0)
1310 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1311 else if (mg->mg_len == HEf_SVKEY)
1312 PUSHs((SV*)mg->mg_ptr);
1314 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1315 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1323 return call_method(meth, flags);
1327 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1333 PUSHSTACKi(PERLSI_MAGIC);
1335 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1336 sv_setsv(sv, *PL_stack_sp--);
1346 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1348 magic_methpack(sv,mg,"FETCH");
1350 mg->mg_flags |= MGf_GSKIP;
1355 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1359 PUSHSTACKi(PERLSI_MAGIC);
1360 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1367 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1369 return magic_methpack(sv,mg,"DELETE");
1374 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1381 PUSHSTACKi(PERLSI_MAGIC);
1382 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1383 sv = *PL_stack_sp--;
1384 retval = (U32) SvIV(sv)-1;
1393 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1398 PUSHSTACKi(PERLSI_MAGIC);
1400 XPUSHs(SvTIED_obj(sv, mg));
1402 call_method("CLEAR", G_SCALAR|G_DISCARD);
1409 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1412 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1416 PUSHSTACKi(PERLSI_MAGIC);
1419 PUSHs(SvTIED_obj(sv, mg));
1424 if (call_method(meth, G_SCALAR))
1425 sv_setsv(key, *PL_stack_sp--);
1434 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1436 return magic_methpack(sv,mg,"EXISTS");
1440 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1450 svp = av_fetch(GvAV(gv),
1451 atoi(MgPV(mg,n_a)), FALSE);
1452 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
1453 o->op_private = (U8)i;
1458 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1460 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1465 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1467 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1472 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1474 SV* lsv = LvTARG(sv);
1476 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1477 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1478 if (mg && mg->mg_len >= 0) {
1481 sv_pos_b2u(lsv, &i);
1482 sv_setiv(sv, i + PL_curcop->cop_arybase);
1491 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1493 SV* lsv = LvTARG(sv);
1500 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1501 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1505 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1506 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1508 else if (!SvOK(sv)) {
1512 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1514 pos = SvIV(sv) - PL_curcop->cop_arybase;
1517 ulen = sv_len_utf8(lsv);
1527 else if (pos > (SSize_t)len)
1532 sv_pos_u2b(lsv, &p, 0);
1537 mg->mg_flags &= ~MGf_MINMATCH;
1543 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1545 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1547 gv_efullname3(sv,((GV*)sv), "*");
1551 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1556 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1565 if (*s == '*' && s[1])
1567 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1572 GvGP(sv) = gp_ref(GvGP(gv));
1577 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1580 SV *lsv = LvTARG(sv);
1581 char *tmps = SvPV(lsv,len);
1582 I32 offs = LvTARGOFF(sv);
1583 I32 rem = LvTARGLEN(sv);
1586 sv_pos_u2b(lsv, &offs, &rem);
1587 if (offs > (I32)len)
1589 if (rem + offs > (I32)len)
1591 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1598 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1601 char *tmps = SvPV(sv, len);
1602 SV *lsv = LvTARG(sv);
1603 I32 lvoff = LvTARGOFF(sv);
1604 I32 lvlen = LvTARGLEN(sv);
1607 sv_utf8_upgrade(lsv);
1608 sv_pos_u2b(lsv, &lvoff, &lvlen);
1609 sv_insert(lsv, lvoff, lvlen, tmps, len);
1612 else if (lsv && SvUTF8(lsv)) {
1613 sv_pos_u2b(lsv, &lvoff, &lvlen);
1614 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
1615 sv_insert(lsv, lvoff, lvlen, tmps, len);
1619 sv_insert(lsv, lvoff, lvlen, tmps, len);
1625 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1627 TAINT_IF((mg->mg_len & 1) ||
1628 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1633 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1635 if (PL_localizing) {
1636 if (PL_localizing == 1)
1641 else if (PL_tainted)
1649 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1651 SV *lsv = LvTARG(sv);
1658 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1663 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1665 do_vecset(sv); /* XXX slurp this routine */
1670 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1673 if (LvTARGLEN(sv)) {
1675 SV *ahv = LvTARG(sv);
1676 if (SvTYPE(ahv) == SVt_PVHV) {
1677 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1682 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1688 AV* av = (AV*)LvTARG(sv);
1689 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1690 targ = AvARRAY(av)[LvTARGOFF(sv)];
1692 if (targ && targ != &PL_sv_undef) {
1693 /* somebody else defined it for us */
1694 SvREFCNT_dec(LvTARG(sv));
1695 LvTARG(sv) = SvREFCNT_inc(targ);
1697 SvREFCNT_dec(mg->mg_obj);
1698 mg->mg_obj = Nullsv;
1699 mg->mg_flags &= ~MGf_REFCOUNTED;
1704 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1709 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1714 sv_setsv(LvTARG(sv), sv);
1715 SvSETMAGIC(LvTARG(sv));
1721 Perl_vivify_defelem(pTHX_ SV *sv)
1726 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
1729 SV *ahv = LvTARG(sv);
1731 if (SvTYPE(ahv) == SVt_PVHV) {
1732 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1737 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
1741 if (!value || value == &PL_sv_undef)
1742 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1745 AV* av = (AV*)LvTARG(sv);
1746 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1747 LvTARG(sv) = Nullsv; /* array can't be extended */
1749 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1750 if (!svp || (value = *svp) == &PL_sv_undef)
1751 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1754 (void)SvREFCNT_inc(value);
1755 SvREFCNT_dec(LvTARG(sv));
1758 SvREFCNT_dec(mg->mg_obj);
1759 mg->mg_obj = Nullsv;
1760 mg->mg_flags &= ~MGf_REFCOUNTED;
1764 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1766 AV *av = (AV*)mg->mg_obj;
1767 SV **svp = AvARRAY(av);
1768 I32 i = AvFILLp(av);
1770 if (svp[i] && svp[i] != &PL_sv_undef) {
1771 if (!SvWEAKREF(svp[i]))
1772 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1773 /* XXX Should we check that it hasn't changed? */
1775 (void)SvOK_off(svp[i]);
1776 SvWEAKREF_off(svp[i]);
1777 svp[i] = &PL_sv_undef;
1785 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1793 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1795 sv_unmagic(sv, PERL_MAGIC_bm);
1801 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1803 sv_unmagic(sv, PERL_MAGIC_fm);
1809 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1811 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1813 if (uf && uf->uf_set)
1814 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
1819 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1821 regexp *re = (regexp *)mg->mg_obj;
1826 #ifdef USE_LOCALE_COLLATE
1828 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1831 * RenE<eacute> Descartes said "I think not."
1832 * and vanished with a faint plop.
1835 Safefree(mg->mg_ptr);
1841 #endif /* USE_LOCALE_COLLATE */
1844 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1849 switch (*mg->mg_ptr) {
1850 case '\001': /* ^A */
1851 sv_setsv(PL_bodytarget, sv);
1853 case '\003': /* ^C */
1854 PL_minus_c = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1857 case '\004': /* ^D */
1858 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
1859 DEBUG_x(dump_all());
1861 case '\005': /* ^E */
1862 if (*(mg->mg_ptr+1) == '\0') {
1863 #ifdef MACOS_TRADITIONAL
1864 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1867 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1870 SetLastError( SvIV(sv) );
1873 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1875 /* will anyone ever use this? */
1876 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1882 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
1884 sv_setsv(PL_encoding, sv);
1886 PL_encoding = newSVsv(sv);
1888 case '\006': /* ^F */
1889 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1891 case '\010': /* ^H */
1892 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1894 case '\011': /* ^I */ /* NOT \t in EBCDIC */
1896 Safefree(PL_inplace);
1898 PL_inplace = savepv(SvPV(sv,len));
1900 PL_inplace = Nullch;
1902 case '\017': /* ^O */
1903 if (*(mg->mg_ptr+1) == '\0') {
1905 Safefree(PL_osname);
1907 PL_osname = savepv(SvPV(sv,len));
1911 else if (strEQ(mg->mg_ptr, "\017PEN")) {
1912 if (!PL_compiling.cop_io)
1913 PL_compiling.cop_io = newSVsv(sv);
1915 sv_setsv(PL_compiling.cop_io,sv);
1918 case '\020': /* ^P */
1919 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1920 if (PL_perldb && !PL_DBsingle)
1923 case '\024': /* ^T */
1925 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1927 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1930 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
1931 if (*(mg->mg_ptr+1) == '\0') {
1932 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1933 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1934 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1935 | (i ? G_WARN_ON : G_WARN_OFF) ;
1938 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
1939 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1940 if (!SvPOK(sv) && PL_localizing) {
1941 sv_setpvn(sv, WARN_NONEstring, WARNsize);
1942 PL_compiling.cop_warnings = pWARN_NONE;
1947 int accumulate = 0 ;
1948 int any_fatals = 0 ;
1949 char * ptr = (char*)SvPV(sv, len) ;
1950 for (i = 0 ; i < len ; ++i) {
1951 accumulate |= ptr[i] ;
1952 any_fatals |= (ptr[i] & 0xAA) ;
1955 PL_compiling.cop_warnings = pWARN_NONE;
1956 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
1957 PL_compiling.cop_warnings = pWARN_ALL;
1958 PL_dowarn |= G_WARN_ONCE ;
1961 if (specialWARN(PL_compiling.cop_warnings))
1962 PL_compiling.cop_warnings = newSVsv(sv) ;
1964 sv_setsv(PL_compiling.cop_warnings, sv);
1965 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1966 PL_dowarn |= G_WARN_ONCE ;
1972 else if (strEQ(mg->mg_ptr+1, "IDE_SYSTEM_CALLS"))
1973 PL_widesyscalls = (bool)SvTRUE(sv);
1976 if (PL_localizing) {
1977 if (PL_localizing == 1)
1978 SAVESPTR(PL_last_in_gv);
1980 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1981 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
1984 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
1985 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1986 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1989 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
1990 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1991 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1994 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1997 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1998 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1999 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
2002 IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2006 IO *io = GvIOp(PL_defoutgv);
2009 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
2010 IoFLAGS(io) &= ~IOf_FLUSH;
2012 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2013 PerlIO *ofp = IoOFP(io);
2015 (void)PerlIO_flush(ofp);
2016 IoFLAGS(io) |= IOf_FLUSH;
2022 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2023 PL_multiline = (i != 0);
2026 SvREFCNT_dec(PL_rs);
2027 PL_rs = newSVsv(sv);
2031 SvREFCNT_dec(PL_ors_sv);
2032 if (SvOK(sv) || SvGMAGICAL(sv)) {
2033 PL_ors_sv = newSVsv(sv);
2041 SvREFCNT_dec(PL_ofs_sv);
2042 if (SvOK(sv) || SvGMAGICAL(sv)) {
2043 PL_ofs_sv = newSVsv(sv);
2052 PL_ofmt = savepv(SvPV(sv,len));
2055 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2058 #ifdef COMPLEX_STATUS
2059 if (PL_localizing == 2) {
2060 PL_statusvalue = LvTARGOFF(sv);
2061 PL_statusvalue_vms = LvTARGLEN(sv);
2065 #ifdef VMSISH_STATUS
2067 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
2070 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2073 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
2074 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
2077 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2078 if (PL_delaymagic) {
2079 PL_delaymagic |= DM_RUID;
2080 break; /* don't do magic till later */
2083 (void)setruid((Uid_t)PL_uid);
2086 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
2088 #ifdef HAS_SETRESUID
2089 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
2091 if (PL_uid == PL_euid) /* special case $< = $> */
2092 (void)PerlProc_setuid(PL_uid);
2094 PL_uid = PerlProc_getuid();
2095 Perl_croak(aTHX_ "setruid() not implemented");
2100 PL_uid = PerlProc_getuid();
2101 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2104 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2105 if (PL_delaymagic) {
2106 PL_delaymagic |= DM_EUID;
2107 break; /* don't do magic till later */
2110 (void)seteuid((Uid_t)PL_euid);
2113 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
2115 #ifdef HAS_SETRESUID
2116 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
2118 if (PL_euid == PL_uid) /* special case $> = $< */
2119 PerlProc_setuid(PL_euid);
2121 PL_euid = PerlProc_geteuid();
2122 Perl_croak(aTHX_ "seteuid() not implemented");
2127 PL_euid = PerlProc_geteuid();
2128 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2131 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2132 if (PL_delaymagic) {
2133 PL_delaymagic |= DM_RGID;
2134 break; /* don't do magic till later */
2137 (void)setrgid((Gid_t)PL_gid);
2140 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
2142 #ifdef HAS_SETRESGID
2143 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2145 if (PL_gid == PL_egid) /* special case $( = $) */
2146 (void)PerlProc_setgid(PL_gid);
2148 PL_gid = PerlProc_getgid();
2149 Perl_croak(aTHX_ "setrgid() not implemented");
2154 PL_gid = PerlProc_getgid();
2155 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2158 #ifdef HAS_SETGROUPS
2160 char *p = SvPV(sv, len);
2161 Groups_t gary[NGROUPS];
2166 for (i = 0; i < NGROUPS; ++i) {
2167 while (*p && !isSPACE(*p))
2176 (void)setgroups(i, gary);
2178 #else /* HAS_SETGROUPS */
2179 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2180 #endif /* HAS_SETGROUPS */
2181 if (PL_delaymagic) {
2182 PL_delaymagic |= DM_EGID;
2183 break; /* don't do magic till later */
2186 (void)setegid((Gid_t)PL_egid);
2189 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2191 #ifdef HAS_SETRESGID
2192 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2194 if (PL_egid == PL_gid) /* special case $) = $( */
2195 (void)PerlProc_setgid(PL_egid);
2197 PL_egid = PerlProc_getegid();
2198 Perl_croak(aTHX_ "setegid() not implemented");
2203 PL_egid = PerlProc_getegid();
2204 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2207 PL_chopset = SvPV_force(sv,len);
2209 #ifndef MACOS_TRADITIONAL
2211 #ifdef HAS_SETPROCTITLE
2212 /* The BSDs don't show the argv[] in ps(1) output, they
2213 * show a string from the process struct and provide
2214 * the setproctitle() routine to manipulate that. */
2217 # if __FreeBSD_version > 410001
2218 /* The leading "-" removes the "perl: " prefix,
2219 * but not the "(perl) suffix from the ps(1)
2220 * output, because that's what ps(1) shows if the
2221 * argv[] is modified. */
2222 setproctitle("-%s", s);
2223 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2224 /* This doesn't really work if you assume that
2225 * $0 = 'foobar'; will wipe out 'perl' from the $0
2226 * because in ps(1) output the result will be like
2227 * sprintf("perl: %s (perl)", s)
2228 * I guess this is a security feature:
2229 * one (a user process) cannot get rid of the original name.
2231 setproctitle("%s", s);
2238 /* See if all the arguments are contiguous in memory */
2239 for (i = 1; i < PL_origargc; i++) {
2240 if (PL_origargv[i] == s + 1
2242 || PL_origargv[i] == s + 2
2247 s += strlen(s); /* this one is ok too */
2252 /* can grab env area too? */
2255 && PL_curinterp == aTHX
2257 && (PL_origenviron[0] == s + 1))
2259 my_setenv("NoNe SuCh", Nullch);
2260 /* force copy of environment */
2261 for (i = 0; PL_origenviron[i]; i++)
2262 if (PL_origenviron[i] == s + 1) {
2269 PL_origalen = s - PL_origargv[0];
2271 s = SvPV_force(sv,len);
2273 if (i >= (I32)PL_origalen) {
2275 /* don't allow system to limit $0 seen by script */
2276 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2277 Copy(s, PL_origargv[0], i, char);
2278 s = PL_origargv[0]+i;
2282 Copy(s, PL_origargv[0], i, char);
2283 s = PL_origargv[0]+i;
2285 while (++i < (I32)PL_origalen)
2287 for (i = 1; i < PL_origargc; i++)
2288 PL_origargv[i] = Nullch;
2292 #ifdef USE_5005THREADS
2294 sv_setsv(thr->errsv, sv);
2296 #endif /* USE_5005THREADS */
2301 #ifdef USE_5005THREADS
2303 Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
2305 DEBUG_S(PerlIO_printf(Perl_debug_log,
2306 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2307 PTR2UV(thr), PTR2UV(sv)));
2309 Perl_croak(aTHX_ "panic: magic_mutexfree");
2310 MUTEX_DESTROY(MgMUTEXP(mg));
2311 COND_DESTROY(MgCONDP(mg));
2314 #endif /* USE_5005THREADS */
2317 Perl_whichsig(pTHX_ char *sig)
2319 register char **sigv;
2321 for (sigv = PL_sig_name+1; *sigv; sigv++)
2322 if (strEQ(sig,*sigv))
2323 return PL_sig_num[sigv - PL_sig_name];
2325 if (strEQ(sig,"CHLD"))
2329 if (strEQ(sig,"CLD"))
2335 #if !defined(PERL_IMPLICIT_CONTEXT)
2340 Perl_sighandler(int sig)
2342 #ifdef PERL_GET_SIG_CONTEXT
2343 dTHXa(PERL_GET_SIG_CONTEXT);
2350 SV *sv = Nullsv, *tSv = PL_Sv;
2356 if (PL_savestack_ix + 15 <= PL_savestack_max)
2358 if (PL_markstack_ptr < PL_markstack_max - 2)
2360 if (PL_retstack_ix < PL_retstack_max - 2)
2362 if (PL_scopestack_ix < PL_scopestack_max - 3)
2365 if (!PL_psig_ptr[sig]) {
2366 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
2371 /* Max number of items pushed there is 3*n or 4. We cannot fix
2372 infinity, so we fix 4 (in fact 5): */
2374 PL_savestack_ix += 5; /* Protect save in progress. */
2375 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2378 PL_markstack_ptr++; /* Protect mark. */
2381 PL_retstack[PL_retstack_ix] = NULL;
2384 PL_scopestack_ix += 1;
2385 /* sv_2cv is too complicated, try a simpler variant first: */
2386 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2387 || SvTYPE(cv) != SVt_PVCV)
2388 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2390 if (!cv || !CvROOT(cv)) {
2391 if (ckWARN(WARN_SIGNAL))
2392 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
2393 PL_sig_name[sig], (gv ? GvENAME(gv)
2400 if(PL_psig_name[sig]) {
2401 sv = SvREFCNT_inc(PL_psig_name[sig]);
2403 #if !defined(PERL_IMPLICIT_CONTEXT)
2407 sv = sv_newmortal();
2408 sv_setpv(sv,PL_sig_name[sig]);
2411 PUSHSTACKi(PERLSI_SIGNAL);
2416 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2419 if (SvTRUE(ERRSV)) {
2421 #ifdef HAS_SIGPROCMASK
2422 /* Handler "died", for example to get out of a restart-able read().
2423 * Before we re-do that on its behalf re-enable the signal which was
2424 * blocked by the system when we entered.
2428 sigaddset(&set,sig);
2429 sigprocmask(SIG_UNBLOCK, &set, NULL);
2431 /* Not clear if this will work */
2432 (void)rsignal(sig, SIG_IGN);
2433 (void)rsignal(sig, &Perl_csighandler);
2435 #endif /* !PERL_MICRO */
2436 Perl_die(aTHX_ Nullformat);
2440 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2446 PL_scopestack_ix -= 1;
2449 PL_op = myop; /* Apparently not needed... */
2451 PL_Sv = tSv; /* Restore global temporaries. */
2458 restore_magic(pTHX_ void *p)
2460 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2461 SV* sv = mgs->mgs_sv;
2466 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2469 SvFLAGS(sv) |= mgs->mgs_flags;
2473 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2476 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2478 /* If we're still on top of the stack, pop us off. (That condition
2479 * will be satisfied if restore_magic was called explicitly, but *not*
2480 * if it's being called via leave_scope.)
2481 * The reason for doing this is that otherwise, things like sv_2cv()
2482 * may leave alloc gunk on the savestack, and some code
2483 * (e.g. sighandler) doesn't expect that...
2485 if (PL_savestack_ix == mgs->mgs_ss_ix)
2487 I32 popval = SSPOPINT;
2488 assert(popval == SAVEt_DESTRUCTOR_X);
2489 PL_savestack_ix -= 2;
2491 assert(popval == SAVEt_ALLOC);
2493 PL_savestack_ix -= popval;
2499 unwind_handler_stack(pTHX_ void *p)
2501 U32 flags = *(U32*)p;
2504 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2505 /* cxstack_ix-- Not needed, die already unwound it. */
2506 #if !defined(PERL_IMPLICIT_CONTEXT)
2508 SvREFCNT_dec(sig_sv);