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)
696 bool was_tainted = FALSE;
698 was_tainted = PL_tainted;
702 if (PL_reg_match_utf8 && is_utf8_string((U8*)s, i))
707 PL_tainted = (was_tainted || RX_MATCH_TAINTED(rx));
712 sv_setsv(sv,&PL_sv_undef);
715 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
716 paren = rx->lastparen;
720 sv_setsv(sv,&PL_sv_undef);
722 case '\016': /* ^N */
723 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
724 paren = rx->lastcloseparen;
728 sv_setsv(sv,&PL_sv_undef);
731 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
732 if ((s = rx->subbeg) && rx->startp[0] != -1) {
737 sv_setsv(sv,&PL_sv_undef);
740 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
741 if (rx->subbeg && rx->endp[0] != -1) {
742 s = rx->subbeg + rx->endp[0];
743 i = rx->sublen - rx->endp[0];
747 sv_setsv(sv,&PL_sv_undef);
751 if (GvIO(PL_last_in_gv)) {
752 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
758 sv_setiv(sv, (IV)STATUS_CURRENT);
759 #ifdef COMPLEX_STATUS
760 LvTARGOFF(sv) = PL_statusvalue;
761 LvTARGLEN(sv) = PL_statusvalue_vms;
766 s = IoTOP_NAME(GvIOp(PL_defoutgv));
770 sv_setpv(sv,GvENAME(PL_defoutgv));
775 s = IoFMT_NAME(GvIOp(PL_defoutgv));
777 s = GvENAME(PL_defoutgv);
782 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
785 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
788 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
796 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
799 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
805 sv_setpv(sv,SvPVX(PL_ors_sv));
808 sv_setpv(sv,PL_ofmt);
812 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
813 sv_setpv(sv, errno ? Strerror(errno) : "");
816 int saveerrno = errno;
817 sv_setnv(sv, (NV)errno);
819 if (errno == errno_isOS2 || errno == errno_isOS2_set)
820 sv_setpv(sv, os2error(Perl_rc));
823 sv_setpv(sv, errno ? Strerror(errno) : "");
827 SvNOK_on(sv); /* what a wonderful hack! */
830 sv_setiv(sv, (IV)PL_uid);
833 sv_setiv(sv, (IV)PL_euid);
836 sv_setiv(sv, (IV)PL_gid);
838 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
842 sv_setiv(sv, (IV)PL_egid);
844 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
849 Groups_t gary[NGROUPS];
850 i = getgroups(NGROUPS,gary);
852 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
855 (void)SvIOK_on(sv); /* what a wonderful hack! */
859 #ifndef MACOS_TRADITIONAL
863 #ifdef USE_5005THREADS
865 sv_setsv(sv, thr->errsv);
867 #endif /* USE_5005THREADS */
873 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
875 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
877 if (uf && uf->uf_val)
878 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
883 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
893 #ifdef DYNAMIC_ENV_FETCH
894 /* We just undefd an environment var. Is a replacement */
895 /* waiting in the wings? */
898 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
899 s = SvPV(*valp, len);
903 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
904 /* And you'll never guess what the dog had */
905 /* in its mouth... */
907 MgTAINTEDDIR_off(mg);
909 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
910 char pathbuf[256], eltbuf[256], *cp, *elt = s;
914 do { /* DCL$PATH may be a search list */
915 while (1) { /* as may dev portion of any element */
916 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
917 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
918 cando_by_name(S_IWUSR,0,elt) ) {
923 if ((cp = strchr(elt, ':')) != Nullch)
925 if (my_trnlnm(elt, eltbuf, j++))
931 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
934 if (s && klen == 4 && strEQ(ptr,"PATH")) {
935 char *strend = s + len;
941 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
944 if (i >= sizeof tmpbuf /* too long -- assume the worst */
946 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
953 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
959 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
962 my_setenv(MgPV(mg,n_a),Nullch);
967 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
970 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
975 magic_clear_all_env(sv,mg);
976 hv_iterinit((HV*)sv);
977 while ((entry = hv_iternext((HV*)sv))) {
979 my_setenv(hv_iterkey(entry, &keylen),
980 SvPV(hv_iterval((HV*)sv, entry), n_a));
988 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
990 #if defined(VMS) || defined(EPOC)
991 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
993 # if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
996 # ifdef USE_ENVIRON_ARRAY
997 # if defined(USE_ITHREADS)
998 /* only the parent thread can clobber the process environment */
999 if (PL_curinterp == aTHX)
1002 # ifndef PERL_USE_SAFE_PUTENV
1005 if (environ == PL_origenviron)
1006 environ = (char**)safesysmalloc(sizeof(char*));
1008 for (i = 0; environ[i]; i++)
1009 safesysfree(environ[i]);
1010 # endif /* PERL_USE_SAFE_PUTENV */
1012 environ[0] = Nullch;
1014 # endif /* USE_ENVIRON_ARRAY */
1015 # endif /* PERL_IMPLICIT_SYS || WIN32 */
1016 #endif /* VMS || EPC */
1020 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS)||defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1021 static int sig_handlers_initted = 0;
1023 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1024 static int sig_ignoring[SIG_SIZE]; /* which signals we are ignoring */
1026 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1027 static int sig_defaulting[SIG_SIZE];
1032 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
1036 /* Are we fetching a signal entry? */
1037 i = whichsig(MgPV(mg,n_a));
1040 sv_setsv(sv,PL_psig_ptr[i]);
1042 Sighandler_t sigstate;
1043 sigstate = rsignal_state(i);
1044 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1045 if (sig_handlers_initted && sig_ignoring[i]) sigstate = SIG_IGN;
1047 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1048 if (sig_handlers_initted && sig_defaulting[i]) sigstate = SIG_DFL;
1050 /* cache state so we don't fetch it again */
1051 if(sigstate == SIG_IGN)
1052 sv_setpv(sv,"IGNORE");
1054 sv_setsv(sv,&PL_sv_undef);
1055 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1062 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
1066 /* Are we clearing a signal entry? */
1067 i = whichsig(MgPV(mg,n_a));
1069 if(PL_psig_ptr[i]) {
1070 SvREFCNT_dec(PL_psig_ptr[i]);
1073 if(PL_psig_name[i]) {
1074 SvREFCNT_dec(PL_psig_name[i]);
1082 Perl_raise_signal(pTHX_ int sig)
1084 /* Set a flag to say this signal is pending */
1085 PL_psig_pend[sig]++;
1086 /* And one to say _a_ signal is pending */
1091 Perl_csighandler(int sig)
1093 #ifdef PERL_GET_SIG_CONTEXT
1094 dTHXa(PERL_GET_SIG_CONTEXT);
1098 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1099 (void) rsignal(sig, &Perl_csighandler);
1100 if (sig_ignoring[sig]) return;
1102 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1103 if (sig_defaulting[sig])
1104 #ifdef KILL_BY_SIGPRC
1105 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1111 #ifdef PERL_OLD_SIGNALS
1112 /* Call the perl level handler now with risk we may be in malloc() etc. */
1113 (*PL_sighandlerp)(sig);
1115 Perl_raise_signal(aTHX_ sig);
1119 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1121 Perl_csighandler_init(void)
1124 if (sig_handlers_initted) return;
1126 for (sig = 1; sig < SIG_SIZE; sig++) {
1127 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1129 sig_defaulting[sig] = 1;
1130 (void) rsignal(sig, &Perl_csighandler);
1132 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1133 sig_ignoring[sig] = 0;
1136 sig_handlers_initted = 1;
1141 Perl_despatch_signals(pTHX)
1145 for (sig = 1; sig < SIG_SIZE; sig++) {
1146 if (PL_psig_pend[sig]) {
1147 PL_psig_pend[sig] = 0;
1148 (*PL_sighandlerp)(sig);
1154 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1163 if (strEQ(s,"__DIE__"))
1165 else if (strEQ(s,"__WARN__"))
1168 Perl_croak(aTHX_ "No such hook: %s", s);
1176 i = whichsig(s); /* ...no, a brick */
1178 if (ckWARN(WARN_SIGNAL))
1179 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
1182 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1183 if (!sig_handlers_initted) Perl_csighandler_init();
1185 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1186 sig_ignoring[i] = 0;
1188 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1189 sig_defaulting[i] = 0;
1191 SvREFCNT_dec(PL_psig_name[i]);
1192 SvREFCNT_dec(PL_psig_ptr[i]);
1193 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1194 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1195 PL_psig_name[i] = newSVpvn(s, len);
1196 SvREADONLY_on(PL_psig_name[i]);
1198 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1200 (void)rsignal(i, &Perl_csighandler);
1202 *svp = SvREFCNT_inc(sv);
1205 s = SvPV_force(sv,len);
1206 if (strEQ(s,"IGNORE")) {
1208 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1209 sig_ignoring[i] = 1;
1210 (void)rsignal(i, &Perl_csighandler);
1212 (void)rsignal(i, SIG_IGN);
1217 else if (strEQ(s,"DEFAULT") || !*s) {
1219 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1221 sig_defaulting[i] = 1;
1222 (void)rsignal(i, &Perl_csighandler);
1225 (void)rsignal(i, SIG_DFL);
1232 * We should warn if HINT_STRICT_REFS, but without
1233 * access to a known hint bit in a known OP, we can't
1234 * tell whether HINT_STRICT_REFS is in force or not.
1236 if (!strchr(s,':') && !strchr(s,'\''))
1237 sv_insert(sv, 0, 0, "main::", 6);
1239 (void)rsignal(i, &Perl_csighandler);
1241 *svp = SvREFCNT_inc(sv);
1245 #endif /* !PERL_MICRO */
1248 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1250 PL_sub_generation++;
1255 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1257 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1258 PL_amagic_generation++;
1264 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1266 HV *hv = (HV*)LvTARG(sv);
1270 (void) hv_iterinit(hv);
1271 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1274 while (hv_iternext(hv))
1279 sv_setiv(sv, (IV)i);
1284 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1287 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1292 /* caller is responsible for stack switching/cleanup */
1294 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1300 PUSHs(SvTIED_obj(sv, mg));
1303 if (mg->mg_len >= 0)
1304 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1305 else if (mg->mg_len == HEf_SVKEY)
1306 PUSHs((SV*)mg->mg_ptr);
1308 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1309 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1317 return call_method(meth, flags);
1321 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1327 PUSHSTACKi(PERLSI_MAGIC);
1329 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1330 sv_setsv(sv, *PL_stack_sp--);
1340 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1342 magic_methpack(sv,mg,"FETCH");
1344 mg->mg_flags |= MGf_GSKIP;
1349 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1353 PUSHSTACKi(PERLSI_MAGIC);
1354 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1361 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1363 return magic_methpack(sv,mg,"DELETE");
1368 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1375 PUSHSTACKi(PERLSI_MAGIC);
1376 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1377 sv = *PL_stack_sp--;
1378 retval = (U32) SvIV(sv)-1;
1387 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1392 PUSHSTACKi(PERLSI_MAGIC);
1394 XPUSHs(SvTIED_obj(sv, mg));
1396 call_method("CLEAR", G_SCALAR|G_DISCARD);
1403 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1406 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1410 PUSHSTACKi(PERLSI_MAGIC);
1413 PUSHs(SvTIED_obj(sv, mg));
1418 if (call_method(meth, G_SCALAR))
1419 sv_setsv(key, *PL_stack_sp--);
1428 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1430 return magic_methpack(sv,mg,"EXISTS");
1434 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1444 svp = av_fetch(GvAV(gv),
1445 atoi(MgPV(mg,n_a)), FALSE);
1446 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
1447 o->op_private = (U8)i;
1452 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1454 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1459 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1461 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1466 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1468 SV* lsv = LvTARG(sv);
1470 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1471 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1472 if (mg && mg->mg_len >= 0) {
1475 sv_pos_b2u(lsv, &i);
1476 sv_setiv(sv, i + PL_curcop->cop_arybase);
1485 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1487 SV* lsv = LvTARG(sv);
1494 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1495 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1499 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1500 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1502 else if (!SvOK(sv)) {
1506 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1508 pos = SvIV(sv) - PL_curcop->cop_arybase;
1511 ulen = sv_len_utf8(lsv);
1521 else if (pos > (SSize_t)len)
1526 sv_pos_u2b(lsv, &p, 0);
1531 mg->mg_flags &= ~MGf_MINMATCH;
1537 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1539 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1541 gv_efullname3(sv,((GV*)sv), "*");
1545 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1550 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1559 if (*s == '*' && s[1])
1561 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1566 GvGP(sv) = gp_ref(GvGP(gv));
1571 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1574 SV *lsv = LvTARG(sv);
1575 char *tmps = SvPV(lsv,len);
1576 I32 offs = LvTARGOFF(sv);
1577 I32 rem = LvTARGLEN(sv);
1580 sv_pos_u2b(lsv, &offs, &rem);
1581 if (offs > (I32)len)
1583 if (rem + offs > (I32)len)
1585 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1592 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1595 char *tmps = SvPV(sv, len);
1596 SV *lsv = LvTARG(sv);
1597 I32 lvoff = LvTARGOFF(sv);
1598 I32 lvlen = LvTARGLEN(sv);
1601 sv_utf8_upgrade(lsv);
1602 sv_pos_u2b(lsv, &lvoff, &lvlen);
1603 sv_insert(lsv, lvoff, lvlen, tmps, len);
1606 else if (lsv && SvUTF8(lsv)) {
1607 sv_pos_u2b(lsv, &lvoff, &lvlen);
1608 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
1609 sv_insert(lsv, lvoff, lvlen, tmps, len);
1613 sv_insert(lsv, lvoff, lvlen, tmps, len);
1619 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1621 TAINT_IF((mg->mg_len & 1) ||
1622 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1627 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1629 if (PL_localizing) {
1630 if (PL_localizing == 1)
1635 else if (PL_tainted)
1643 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1645 SV *lsv = LvTARG(sv);
1652 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1657 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1659 do_vecset(sv); /* XXX slurp this routine */
1664 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1667 if (LvTARGLEN(sv)) {
1669 SV *ahv = LvTARG(sv);
1670 if (SvTYPE(ahv) == SVt_PVHV) {
1671 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1676 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1682 AV* av = (AV*)LvTARG(sv);
1683 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1684 targ = AvARRAY(av)[LvTARGOFF(sv)];
1686 if (targ && targ != &PL_sv_undef) {
1687 /* somebody else defined it for us */
1688 SvREFCNT_dec(LvTARG(sv));
1689 LvTARG(sv) = SvREFCNT_inc(targ);
1691 SvREFCNT_dec(mg->mg_obj);
1692 mg->mg_obj = Nullsv;
1693 mg->mg_flags &= ~MGf_REFCOUNTED;
1698 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1703 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1708 sv_setsv(LvTARG(sv), sv);
1709 SvSETMAGIC(LvTARG(sv));
1715 Perl_vivify_defelem(pTHX_ SV *sv)
1720 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
1723 SV *ahv = LvTARG(sv);
1725 if (SvTYPE(ahv) == SVt_PVHV) {
1726 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1731 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
1735 if (!value || value == &PL_sv_undef)
1736 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1739 AV* av = (AV*)LvTARG(sv);
1740 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1741 LvTARG(sv) = Nullsv; /* array can't be extended */
1743 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1744 if (!svp || (value = *svp) == &PL_sv_undef)
1745 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1748 (void)SvREFCNT_inc(value);
1749 SvREFCNT_dec(LvTARG(sv));
1752 SvREFCNT_dec(mg->mg_obj);
1753 mg->mg_obj = Nullsv;
1754 mg->mg_flags &= ~MGf_REFCOUNTED;
1758 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1760 AV *av = (AV*)mg->mg_obj;
1761 SV **svp = AvARRAY(av);
1762 I32 i = AvFILLp(av);
1764 if (svp[i] && svp[i] != &PL_sv_undef) {
1765 if (!SvWEAKREF(svp[i]))
1766 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1767 /* XXX Should we check that it hasn't changed? */
1769 (void)SvOK_off(svp[i]);
1770 SvWEAKREF_off(svp[i]);
1771 svp[i] = &PL_sv_undef;
1779 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1787 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1789 sv_unmagic(sv, PERL_MAGIC_bm);
1795 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1797 sv_unmagic(sv, PERL_MAGIC_fm);
1803 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1805 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1807 if (uf && uf->uf_set)
1808 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
1813 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1815 regexp *re = (regexp *)mg->mg_obj;
1820 #ifdef USE_LOCALE_COLLATE
1822 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1825 * RenE<eacute> Descartes said "I think not."
1826 * and vanished with a faint plop.
1829 Safefree(mg->mg_ptr);
1835 #endif /* USE_LOCALE_COLLATE */
1838 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1843 switch (*mg->mg_ptr) {
1844 case '\001': /* ^A */
1845 sv_setsv(PL_bodytarget, sv);
1847 case '\003': /* ^C */
1848 PL_minus_c = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1851 case '\004': /* ^D */
1852 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
1853 DEBUG_x(dump_all());
1855 case '\005': /* ^E */
1856 if (*(mg->mg_ptr+1) == '\0') {
1857 #ifdef MACOS_TRADITIONAL
1858 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1861 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1864 SetLastError( SvIV(sv) );
1867 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1869 /* will anyone ever use this? */
1870 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1876 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
1878 sv_setsv(PL_encoding, sv);
1880 PL_encoding = newSVsv(sv);
1882 case '\006': /* ^F */
1883 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1885 case '\010': /* ^H */
1886 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1888 case '\011': /* ^I */ /* NOT \t in EBCDIC */
1890 Safefree(PL_inplace);
1892 PL_inplace = savepv(SvPV(sv,len));
1894 PL_inplace = Nullch;
1896 case '\017': /* ^O */
1897 if (*(mg->mg_ptr+1) == '\0') {
1899 Safefree(PL_osname);
1901 PL_osname = savepv(SvPV(sv,len));
1905 else if (strEQ(mg->mg_ptr, "\017PEN")) {
1906 if (!PL_compiling.cop_io)
1907 PL_compiling.cop_io = newSVsv(sv);
1909 sv_setsv(PL_compiling.cop_io,sv);
1912 case '\020': /* ^P */
1913 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1914 if (PL_perldb && !PL_DBsingle)
1917 case '\024': /* ^T */
1919 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1921 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1924 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
1925 if (*(mg->mg_ptr+1) == '\0') {
1926 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1927 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1928 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1929 | (i ? G_WARN_ON : G_WARN_OFF) ;
1932 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
1933 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1934 if (!SvPOK(sv) && PL_localizing) {
1935 sv_setpvn(sv, WARN_NONEstring, WARNsize);
1936 PL_compiling.cop_warnings = pWARN_NONE;
1941 int accumulate = 0 ;
1942 int any_fatals = 0 ;
1943 char * ptr = (char*)SvPV(sv, len) ;
1944 for (i = 0 ; i < len ; ++i) {
1945 accumulate |= ptr[i] ;
1946 any_fatals |= (ptr[i] & 0xAA) ;
1949 PL_compiling.cop_warnings = pWARN_NONE;
1950 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
1951 PL_compiling.cop_warnings = pWARN_ALL;
1952 PL_dowarn |= G_WARN_ONCE ;
1955 if (specialWARN(PL_compiling.cop_warnings))
1956 PL_compiling.cop_warnings = newSVsv(sv) ;
1958 sv_setsv(PL_compiling.cop_warnings, sv);
1959 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1960 PL_dowarn |= G_WARN_ONCE ;
1966 else if (strEQ(mg->mg_ptr+1, "IDE_SYSTEM_CALLS"))
1967 PL_widesyscalls = (bool)SvTRUE(sv);
1970 if (PL_localizing) {
1971 if (PL_localizing == 1)
1972 SAVESPTR(PL_last_in_gv);
1974 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1975 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
1978 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
1979 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1980 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1983 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
1984 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1985 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1988 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1991 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1992 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1993 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
1996 IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2000 IO *io = GvIOp(PL_defoutgv);
2003 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
2004 IoFLAGS(io) &= ~IOf_FLUSH;
2006 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2007 PerlIO *ofp = IoOFP(io);
2009 (void)PerlIO_flush(ofp);
2010 IoFLAGS(io) |= IOf_FLUSH;
2016 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2017 PL_multiline = (i != 0);
2020 SvREFCNT_dec(PL_rs);
2021 PL_rs = newSVsv(sv);
2025 SvREFCNT_dec(PL_ors_sv);
2026 if (SvOK(sv) || SvGMAGICAL(sv)) {
2027 PL_ors_sv = newSVsv(sv);
2035 SvREFCNT_dec(PL_ofs_sv);
2036 if (SvOK(sv) || SvGMAGICAL(sv)) {
2037 PL_ofs_sv = newSVsv(sv);
2046 PL_ofmt = savepv(SvPV(sv,len));
2049 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2052 #ifdef COMPLEX_STATUS
2053 if (PL_localizing == 2) {
2054 PL_statusvalue = LvTARGOFF(sv);
2055 PL_statusvalue_vms = LvTARGLEN(sv);
2059 #ifdef VMSISH_STATUS
2061 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
2064 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2067 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
2068 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
2071 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2072 if (PL_delaymagic) {
2073 PL_delaymagic |= DM_RUID;
2074 break; /* don't do magic till later */
2077 (void)setruid((Uid_t)PL_uid);
2080 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
2082 #ifdef HAS_SETRESUID
2083 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
2085 if (PL_uid == PL_euid) /* special case $< = $> */
2086 (void)PerlProc_setuid(PL_uid);
2088 PL_uid = PerlProc_getuid();
2089 Perl_croak(aTHX_ "setruid() not implemented");
2094 PL_uid = PerlProc_getuid();
2095 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2098 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2099 if (PL_delaymagic) {
2100 PL_delaymagic |= DM_EUID;
2101 break; /* don't do magic till later */
2104 (void)seteuid((Uid_t)PL_euid);
2107 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
2109 #ifdef HAS_SETRESUID
2110 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
2112 if (PL_euid == PL_uid) /* special case $> = $< */
2113 PerlProc_setuid(PL_euid);
2115 PL_euid = PerlProc_geteuid();
2116 Perl_croak(aTHX_ "seteuid() not implemented");
2121 PL_euid = PerlProc_geteuid();
2122 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2125 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2126 if (PL_delaymagic) {
2127 PL_delaymagic |= DM_RGID;
2128 break; /* don't do magic till later */
2131 (void)setrgid((Gid_t)PL_gid);
2134 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
2136 #ifdef HAS_SETRESGID
2137 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2139 if (PL_gid == PL_egid) /* special case $( = $) */
2140 (void)PerlProc_setgid(PL_gid);
2142 PL_gid = PerlProc_getgid();
2143 Perl_croak(aTHX_ "setrgid() not implemented");
2148 PL_gid = PerlProc_getgid();
2149 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2152 #ifdef HAS_SETGROUPS
2154 char *p = SvPV(sv, len);
2155 Groups_t gary[NGROUPS];
2160 for (i = 0; i < NGROUPS; ++i) {
2161 while (*p && !isSPACE(*p))
2170 (void)setgroups(i, gary);
2172 #else /* HAS_SETGROUPS */
2173 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2174 #endif /* HAS_SETGROUPS */
2175 if (PL_delaymagic) {
2176 PL_delaymagic |= DM_EGID;
2177 break; /* don't do magic till later */
2180 (void)setegid((Gid_t)PL_egid);
2183 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2185 #ifdef HAS_SETRESGID
2186 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2188 if (PL_egid == PL_gid) /* special case $) = $( */
2189 (void)PerlProc_setgid(PL_egid);
2191 PL_egid = PerlProc_getegid();
2192 Perl_croak(aTHX_ "setegid() not implemented");
2197 PL_egid = PerlProc_getegid();
2198 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2201 PL_chopset = SvPV_force(sv,len);
2203 #ifndef MACOS_TRADITIONAL
2205 #ifdef HAS_SETPROCTITLE
2206 /* The BSDs don't show the argv[] in ps(1) output, they
2207 * show a string from the process struct and provide
2208 * the setproctitle() routine to manipulate that. */
2211 # if __FreeBSD_version > 410001
2212 /* The leading "-" removes the "perl: " prefix,
2213 * but not the "(perl) suffix from the ps(1)
2214 * output, because that's what ps(1) shows if the
2215 * argv[] is modified. */
2216 setproctitle("-%s", s);
2217 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2218 /* This doesn't really work if you assume that
2219 * $0 = 'foobar'; will wipe out 'perl' from the $0
2220 * because in ps(1) output the result will be like
2221 * sprintf("perl: %s (perl)", s)
2222 * I guess this is a security feature:
2223 * one (a user process) cannot get rid of the original name.
2225 setproctitle("%s", s);
2232 /* See if all the arguments are contiguous in memory */
2233 for (i = 1; i < PL_origargc; i++) {
2234 if (PL_origargv[i] == s + 1
2236 || PL_origargv[i] == s + 2
2241 s += strlen(s); /* this one is ok too */
2246 /* can grab env area too? */
2249 && PL_curinterp == aTHX
2251 && (PL_origenviron[0] == s + 1))
2253 my_setenv("NoNe SuCh", Nullch);
2254 /* force copy of environment */
2255 for (i = 0; PL_origenviron[i]; i++)
2256 if (PL_origenviron[i] == s + 1) {
2263 PL_origalen = s - PL_origargv[0];
2265 s = SvPV_force(sv,len);
2267 if (i >= (I32)PL_origalen) {
2269 /* don't allow system to limit $0 seen by script */
2270 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2271 Copy(s, PL_origargv[0], i, char);
2272 s = PL_origargv[0]+i;
2276 Copy(s, PL_origargv[0], i, char);
2277 s = PL_origargv[0]+i;
2279 while (++i < (I32)PL_origalen)
2281 for (i = 1; i < PL_origargc; i++)
2282 PL_origargv[i] = Nullch;
2286 #ifdef USE_5005THREADS
2288 sv_setsv(thr->errsv, sv);
2290 #endif /* USE_5005THREADS */
2295 #ifdef USE_5005THREADS
2297 Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
2299 DEBUG_S(PerlIO_printf(Perl_debug_log,
2300 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2301 PTR2UV(thr), PTR2UV(sv)));
2303 Perl_croak(aTHX_ "panic: magic_mutexfree");
2304 MUTEX_DESTROY(MgMUTEXP(mg));
2305 COND_DESTROY(MgCONDP(mg));
2308 #endif /* USE_5005THREADS */
2311 Perl_whichsig(pTHX_ char *sig)
2313 register char **sigv;
2315 for (sigv = PL_sig_name+1; *sigv; sigv++)
2316 if (strEQ(sig,*sigv))
2317 return PL_sig_num[sigv - PL_sig_name];
2319 if (strEQ(sig,"CHLD"))
2323 if (strEQ(sig,"CLD"))
2329 #if !defined(PERL_IMPLICIT_CONTEXT)
2334 Perl_sighandler(int sig)
2336 #ifdef PERL_GET_SIG_CONTEXT
2337 dTHXa(PERL_GET_SIG_CONTEXT);
2344 SV *sv = Nullsv, *tSv = PL_Sv;
2350 if (PL_savestack_ix + 15 <= PL_savestack_max)
2352 if (PL_markstack_ptr < PL_markstack_max - 2)
2354 if (PL_retstack_ix < PL_retstack_max - 2)
2356 if (PL_scopestack_ix < PL_scopestack_max - 3)
2359 if (!PL_psig_ptr[sig]) {
2360 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
2365 /* Max number of items pushed there is 3*n or 4. We cannot fix
2366 infinity, so we fix 4 (in fact 5): */
2368 PL_savestack_ix += 5; /* Protect save in progress. */
2369 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2372 PL_markstack_ptr++; /* Protect mark. */
2375 PL_retstack[PL_retstack_ix] = NULL;
2378 PL_scopestack_ix += 1;
2379 /* sv_2cv is too complicated, try a simpler variant first: */
2380 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2381 || SvTYPE(cv) != SVt_PVCV)
2382 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2384 if (!cv || !CvROOT(cv)) {
2385 if (ckWARN(WARN_SIGNAL))
2386 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
2387 PL_sig_name[sig], (gv ? GvENAME(gv)
2394 if(PL_psig_name[sig]) {
2395 sv = SvREFCNT_inc(PL_psig_name[sig]);
2397 #if !defined(PERL_IMPLICIT_CONTEXT)
2401 sv = sv_newmortal();
2402 sv_setpv(sv,PL_sig_name[sig]);
2405 PUSHSTACKi(PERLSI_SIGNAL);
2410 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2413 if (SvTRUE(ERRSV)) {
2415 #ifdef HAS_SIGPROCMASK
2416 /* Handler "died", for example to get out of a restart-able read().
2417 * Before we re-do that on its behalf re-enable the signal which was
2418 * blocked by the system when we entered.
2422 sigaddset(&set,sig);
2423 sigprocmask(SIG_UNBLOCK, &set, NULL);
2425 /* Not clear if this will work */
2426 (void)rsignal(sig, SIG_IGN);
2427 (void)rsignal(sig, &Perl_csighandler);
2429 #endif /* !PERL_MICRO */
2430 Perl_die(aTHX_ Nullformat);
2434 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2440 PL_scopestack_ix -= 1;
2443 PL_op = myop; /* Apparently not needed... */
2445 PL_Sv = tSv; /* Restore global temporaries. */
2452 restore_magic(pTHX_ void *p)
2454 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2455 SV* sv = mgs->mgs_sv;
2460 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2463 SvFLAGS(sv) |= mgs->mgs_flags;
2467 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2470 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2472 /* If we're still on top of the stack, pop us off. (That condition
2473 * will be satisfied if restore_magic was called explicitly, but *not*
2474 * if it's being called via leave_scope.)
2475 * The reason for doing this is that otherwise, things like sv_2cv()
2476 * may leave alloc gunk on the savestack, and some code
2477 * (e.g. sighandler) doesn't expect that...
2479 if (PL_savestack_ix == mgs->mgs_ss_ix)
2481 I32 popval = SSPOPINT;
2482 assert(popval == SAVEt_DESTRUCTOR_X);
2483 PL_savestack_ix -= 2;
2485 assert(popval == SAVEt_ALLOC);
2487 PL_savestack_ix -= popval;
2493 unwind_handler_stack(pTHX_ void *p)
2495 U32 flags = *(U32*)p;
2498 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2499 /* cxstack_ix-- Not needed, die already unwound it. */
2500 #if !defined(PERL_IMPLICIT_CONTEXT)
2502 SvREFCNT_dec(sig_sv);