3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * "Sam sat on the ground and put his head in his hands. 'I wish I had never
13 * come here, and I don't want to see no more magic,' he said, and fell silent."
17 =head1 Magical Functions
24 #if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
34 # include <sys/pstat.h>
37 Signal_t Perl_csighandler(int sig);
39 /* if you only have signal() and it resets on each signal, FAKE_PERSISTENT_SIGNAL_HANDLERS fixes */
40 #if !defined(HAS_SIGACTION) && defined(VMS)
41 # define FAKE_PERSISTENT_SIGNAL_HANDLERS
43 /* if we're doing kill() with sys$sigprc on VMS, FAKE_DEFAULT_SIGNAL_HANDLERS */
44 #if defined(KILL_BY_SIGPRC)
45 # define FAKE_DEFAULT_SIGNAL_HANDLERS
48 static void restore_magic(pTHX_ void *p);
49 static void unwind_handler_stack(pTHX_ void *p);
52 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
60 /* MGS is typedef'ed to struct magic_state in perl.h */
63 S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
66 assert(SvMAGICAL(sv));
67 #ifdef PERL_COPY_ON_WRITE
68 /* Turning READONLY off for a copy-on-write scalar is a bad idea. */
73 SAVEDESTRUCTOR_X(restore_magic, INT2PTR(void*, (IV)mgs_ix));
75 mgs = SSPTR(mgs_ix, MGS*);
77 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
78 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
82 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
86 =for apidoc mg_magical
88 Turns on the magical status of an SV. See C<sv_magic>.
94 Perl_mg_magical(pTHX_ SV *sv)
97 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
98 MGVTBL* vtbl = mg->mg_virtual;
100 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
104 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
113 Do magic after a value is retrieved from the SV. See C<sv_magic>.
119 Perl_mg_get(pTHX_ SV *sv)
122 MAGIC *newmg, *head, *cur, *mg;
123 I32 mgs_ix = SSNEW(sizeof(MGS));
125 save_magic(mgs_ix, sv);
127 /* We must call svt_get(sv, mg) for each valid entry in the linked
128 list of magic. svt_get() may delete the current entry, add new
129 magic to the head of the list, or upgrade the SV. AMS 20010810 */
131 newmg = cur = head = mg = SvMAGIC(sv);
133 MGVTBL *vtbl = mg->mg_virtual;
135 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
136 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
138 /* guard against sv having been freed */
139 if (SvTYPE(sv) == SVTYPEMASK) {
140 Perl_croak(aTHX_ "Tied variable freed while still in use");
143 /* Don't restore the flags for this entry if it was deleted. */
144 if (mg->mg_flags & MGf_GSKIP)
145 (SSPTR(mgs_ix, MGS *))->mgs_flags = 0;
148 mg = mg->mg_moremagic;
151 /* Have we finished with the new entries we saw? Start again
152 where we left off (unless there are more new entries). */
160 /* Were any new entries added? */
161 if (!new && (newmg = SvMAGIC(sv)) != head) {
168 restore_magic(aTHX_ INT2PTR(void *, (IV)mgs_ix));
175 Do magic after a value is assigned to the SV. See C<sv_magic>.
181 Perl_mg_set(pTHX_ SV *sv)
187 mgs_ix = SSNEW(sizeof(MGS));
188 save_magic(mgs_ix, sv);
190 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
191 MGVTBL* vtbl = mg->mg_virtual;
192 nextmg = mg->mg_moremagic; /* it may delete itself */
193 if (mg->mg_flags & MGf_GSKIP) {
194 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
195 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
197 if (vtbl && vtbl->svt_set)
198 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
201 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
206 =for apidoc mg_length
208 Report on the SV's length. See C<sv_magic>.
214 Perl_mg_length(pTHX_ SV *sv)
219 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
220 MGVTBL* vtbl = mg->mg_virtual;
221 if (vtbl && vtbl->svt_len) {
224 mgs_ix = SSNEW(sizeof(MGS));
225 save_magic(mgs_ix, sv);
226 /* omit MGf_GSKIP -- not changed here */
227 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
228 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
235 U8 *s = (U8*)SvPV(sv, len);
236 len = Perl_utf8_length(aTHX_ s, s + len);
244 Perl_mg_size(pTHX_ SV *sv)
249 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
250 MGVTBL* vtbl = mg->mg_virtual;
251 if (vtbl && vtbl->svt_len) {
254 mgs_ix = SSNEW(sizeof(MGS));
255 save_magic(mgs_ix, sv);
256 /* omit MGf_GSKIP -- not changed here */
257 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
258 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
265 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
270 Perl_croak(aTHX_ "Size magic not implemented");
279 Clear something magical that the SV represents. See C<sv_magic>.
285 Perl_mg_clear(pTHX_ SV *sv)
290 mgs_ix = SSNEW(sizeof(MGS));
291 save_magic(mgs_ix, sv);
293 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
294 MGVTBL* vtbl = mg->mg_virtual;
295 /* omit GSKIP -- never set here */
297 if (vtbl && vtbl->svt_clear)
298 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
301 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
308 Finds the magic pointer for type matching the SV. See C<sv_magic>.
314 Perl_mg_find(pTHX_ SV *sv, int type)
319 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
320 if (mg->mg_type == type)
329 Copies the magic from one SV to another. See C<sv_magic>.
335 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
339 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
340 MGVTBL* vtbl = mg->mg_virtual;
341 if ((mg->mg_flags & MGf_COPY) && vtbl->svt_copy){
342 count += CALL_FPTR(vtbl->svt_copy)(aTHX_ sv, mg, nsv, key, klen);
344 else if (isUPPER(mg->mg_type)) {
346 mg->mg_type == PERL_MAGIC_tied ? SvTIED_obj(sv, mg) :
347 (mg->mg_type == PERL_MAGIC_regdata && mg->mg_obj)
349 toLOWER(mg->mg_type), key, klen);
359 Free any magic storage used by the SV. See C<sv_magic>.
365 Perl_mg_free(pTHX_ SV *sv)
369 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
370 MGVTBL* vtbl = mg->mg_virtual;
371 moremagic = mg->mg_moremagic;
372 if (vtbl && vtbl->svt_free)
373 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
374 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
376 Safefree(mg->mg_ptr);
377 else if (mg->mg_len == HEf_SVKEY)
378 SvREFCNT_dec((SV*)mg->mg_ptr);
380 if (mg->mg_flags & MGf_REFCOUNTED)
381 SvREFCNT_dec(mg->mg_obj);
389 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
394 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
398 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
399 if (mg->mg_obj) /* @+ */
402 return rx->lastparen;
409 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
417 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
421 if (paren <= (I32)rx->nparens &&
422 (s = rx->startp[paren]) != -1 &&
423 (t = rx->endp[paren]) != -1)
425 if (mg->mg_obj) /* @+ */
430 if (i > 0 && RX_MATCH_UTF8(rx)) {
431 char *b = rx->subbeg;
433 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
443 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
445 Perl_croak(aTHX_ PL_no_modify);
451 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
458 switch (*mg->mg_ptr) {
459 case '1': case '2': case '3': case '4':
460 case '5': case '6': case '7': case '8': case '9': case '&':
461 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
463 paren = atoi(mg->mg_ptr); /* $& is in [0] */
465 if (paren <= (I32)rx->nparens &&
466 (s1 = rx->startp[paren]) != -1 &&
467 (t1 = rx->endp[paren]) != -1)
471 if (i > 0 && RX_MATCH_UTF8(rx)) {
472 char *s = rx->subbeg + s1;
473 char *send = rx->subbeg + t1;
476 if (is_utf8_string((U8*)s, i))
477 i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send);
480 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
484 if (ckWARN(WARN_UNINITIALIZED))
489 if (ckWARN(WARN_UNINITIALIZED))
494 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
495 paren = rx->lastparen;
500 case '\016': /* ^N */
501 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
502 paren = rx->lastcloseparen;
508 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
509 if (rx->startp[0] != -1) {
520 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
521 if (rx->endp[0] != -1) {
522 i = rx->sublen - rx->endp[0];
533 if (!SvPOK(sv) && SvNIOK(sv)) {
543 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
550 switch (*mg->mg_ptr) {
551 case '\001': /* ^A */
552 sv_setsv(sv, PL_bodytarget);
554 case '\003': /* ^C */
555 sv_setiv(sv, (IV)PL_minus_c);
558 case '\004': /* ^D */
559 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
560 #if defined(YYDEBUG) && defined(DEBUGGING)
561 PL_yydebug = DEBUG_p_TEST;
564 case '\005': /* ^E */
565 if (*(mg->mg_ptr+1) == '\0') {
566 #ifdef MACOS_TRADITIONAL
570 sv_setnv(sv,(double)gMacPerl_OSErr);
571 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
576 # include <descrip.h>
577 # include <starlet.h>
579 $DESCRIPTOR(msgdsc,msg);
580 sv_setnv(sv,(NV) vaxc$errno);
581 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
582 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
588 if (!(_emx_env & 0x200)) { /* Under DOS */
589 sv_setnv(sv, (NV)errno);
590 sv_setpv(sv, errno ? Strerror(errno) : "");
592 if (errno != errno_isOS2) {
593 int tmp = _syserrno();
594 if (tmp) /* 2nd call to _syserrno() makes it 0 */
597 sv_setnv(sv, (NV)Perl_rc);
598 sv_setpv(sv, os2error(Perl_rc));
603 DWORD dwErr = GetLastError();
604 sv_setnv(sv, (NV)dwErr);
607 PerlProc_GetOSError(sv, dwErr);
614 sv_setnv(sv, (NV)errno);
615 sv_setpv(sv, errno ? Strerror(errno) : "");
620 SvNOK_on(sv); /* what a wonderful hack! */
622 else if (strEQ(mg->mg_ptr+1, "NCODING"))
623 sv_setsv(sv, PL_encoding);
625 case '\006': /* ^F */
626 sv_setiv(sv, (IV)PL_maxsysfd);
628 case '\010': /* ^H */
629 sv_setiv(sv, (IV)PL_hints);
631 case '\011': /* ^I */ /* NOT \t in EBCDIC */
633 sv_setpv(sv, PL_inplace);
635 sv_setsv(sv, &PL_sv_undef);
637 case '\017': /* ^O & ^OPEN */
638 if (*(mg->mg_ptr+1) == '\0')
639 sv_setpv(sv, PL_osname);
640 else if (strEQ(mg->mg_ptr, "\017PEN")) {
641 if (!PL_compiling.cop_io)
642 sv_setsv(sv, &PL_sv_undef);
644 sv_setsv(sv, PL_compiling.cop_io);
648 case '\020': /* ^P */
649 sv_setiv(sv, (IV)PL_perldb);
651 case '\023': /* ^S */
652 if (*(mg->mg_ptr+1) == '\0') {
653 if (PL_lex_state != LEX_NOTPARSING)
656 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
661 case '\024': /* ^T */
662 if (*(mg->mg_ptr+1) == '\0') {
664 sv_setnv(sv, PL_basetime);
666 sv_setiv(sv, (IV)PL_basetime);
669 else if (strEQ(mg->mg_ptr, "\024AINT"))
670 sv_setiv(sv, PL_tainting
671 ? (PL_taint_warn || PL_unsafe ? -1 : 1)
674 case '\025': /* $^UNICODE */
675 if (strEQ(mg->mg_ptr, "\025NICODE"))
676 sv_setuv(sv, (UV) PL_unicode);
678 case '\027': /* ^W & $^WARNING_BITS */
679 if (*(mg->mg_ptr+1) == '\0')
680 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
681 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
682 if (PL_compiling.cop_warnings == pWARN_NONE ||
683 PL_compiling.cop_warnings == pWARN_STD)
685 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
687 else if (PL_compiling.cop_warnings == pWARN_ALL) {
688 /* Get the bit mask for $warnings::Bits{all}, because
689 * it could have been extended by warnings::register */
691 HV *bits=get_hv("warnings::Bits", FALSE);
692 if (bits && (bits_all=hv_fetch(bits, "all", 3, FALSE))) {
693 sv_setsv(sv, *bits_all);
696 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
700 sv_setsv(sv, PL_compiling.cop_warnings);
705 case '1': case '2': case '3': case '4':
706 case '5': case '6': case '7': case '8': case '9': case '&':
707 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
711 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
712 * XXX Does the new way break anything?
714 paren = atoi(mg->mg_ptr); /* $& is in [0] */
716 if (paren <= (I32)rx->nparens &&
717 (s1 = rx->startp[paren]) != -1 &&
718 (t1 = rx->endp[paren]) != -1)
728 if (RX_MATCH_UTF8(rx) && is_utf8_string((U8*)s, i))
733 if (RX_MATCH_TAINTED(rx)) {
734 MAGIC* mg = SvMAGIC(sv);
737 SvMAGIC(sv) = mg->mg_moremagic;
739 if ((mgt = SvMAGIC(sv))) {
740 mg->mg_moremagic = mgt;
750 sv_setsv(sv,&PL_sv_undef);
753 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
754 paren = rx->lastparen;
758 sv_setsv(sv,&PL_sv_undef);
760 case '\016': /* ^N */
761 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
762 paren = rx->lastcloseparen;
766 sv_setsv(sv,&PL_sv_undef);
769 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
770 if ((s = rx->subbeg) && rx->startp[0] != -1) {
775 sv_setsv(sv,&PL_sv_undef);
778 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
779 if (rx->subbeg && rx->endp[0] != -1) {
780 s = rx->subbeg + rx->endp[0];
781 i = rx->sublen - rx->endp[0];
785 sv_setsv(sv,&PL_sv_undef);
789 if (GvIO(PL_last_in_gv)) {
790 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
796 sv_setiv(sv, (IV)STATUS_CURRENT);
797 #ifdef COMPLEX_STATUS
798 LvTARGOFF(sv) = PL_statusvalue;
799 LvTARGLEN(sv) = PL_statusvalue_vms;
804 s = IoTOP_NAME(GvIOp(PL_defoutgv));
808 sv_setpv(sv,GvENAME(PL_defoutgv));
813 s = IoFMT_NAME(GvIOp(PL_defoutgv));
815 s = GvENAME(PL_defoutgv);
820 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
823 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
826 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
834 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
837 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
843 sv_copypv(sv, PL_ors_sv);
846 sv_setpv(sv,PL_ofmt);
850 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
851 sv_setpv(sv, errno ? Strerror(errno) : "");
854 int saveerrno = errno;
855 sv_setnv(sv, (NV)errno);
857 if (errno == errno_isOS2 || errno == errno_isOS2_set)
858 sv_setpv(sv, os2error(Perl_rc));
861 sv_setpv(sv, errno ? Strerror(errno) : "");
865 SvNOK_on(sv); /* what a wonderful hack! */
868 sv_setiv(sv, (IV)PL_uid);
871 sv_setiv(sv, (IV)PL_euid);
874 sv_setiv(sv, (IV)PL_gid);
876 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
880 sv_setiv(sv, (IV)PL_egid);
882 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
887 Groups_t gary[NGROUPS];
888 i = getgroups(NGROUPS,gary);
890 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
893 (void)SvIOK_on(sv); /* what a wonderful hack! */
897 #ifndef MACOS_TRADITIONAL
906 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
908 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
910 if (uf && uf->uf_val)
911 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
916 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
926 #ifdef DYNAMIC_ENV_FETCH
927 /* We just undefd an environment var. Is a replacement */
928 /* waiting in the wings? */
931 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
932 s = SvPV(*valp, len);
936 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
937 /* And you'll never guess what the dog had */
938 /* in its mouth... */
940 MgTAINTEDDIR_off(mg);
942 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
943 char pathbuf[256], eltbuf[256], *cp, *elt = s;
947 do { /* DCL$PATH may be a search list */
948 while (1) { /* as may dev portion of any element */
949 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
950 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
951 cando_by_name(S_IWUSR,0,elt) ) {
956 if ((cp = strchr(elt, ':')) != Nullch)
958 if (my_trnlnm(elt, eltbuf, j++))
964 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
967 if (s && klen == 4 && strEQ(ptr,"PATH")) {
968 char *strend = s + len;
974 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
977 if (i >= sizeof tmpbuf /* too long -- assume the worst */
979 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
986 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
992 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
995 my_setenv(MgPV(mg,n_a),Nullch);
1000 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
1003 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1005 if (PL_localizing) {
1008 magic_clear_all_env(sv,mg);
1009 hv_iterinit((HV*)sv);
1010 while ((entry = hv_iternext((HV*)sv))) {
1012 my_setenv(hv_iterkey(entry, &keylen),
1013 SvPV(hv_iterval((HV*)sv, entry), n_a));
1021 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
1023 #if defined(VMS) || defined(EPOC)
1024 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1026 # if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
1029 # ifdef USE_ENVIRON_ARRAY
1030 # if defined(USE_ITHREADS)
1031 /* only the parent thread can clobber the process environment */
1032 if (PL_curinterp == aTHX)
1035 # ifndef PERL_USE_SAFE_PUTENV
1038 if (environ == PL_origenviron)
1039 environ = (char**)safesysmalloc(sizeof(char*));
1041 for (i = 0; environ[i]; i++)
1042 safesysfree(environ[i]);
1043 # endif /* PERL_USE_SAFE_PUTENV */
1045 environ[0] = Nullch;
1047 # endif /* USE_ENVIRON_ARRAY */
1048 # endif /* PERL_IMPLICIT_SYS || WIN32 */
1049 #endif /* VMS || EPC */
1053 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS)||defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1054 static int sig_handlers_initted = 0;
1056 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1057 static int sig_ignoring[SIG_SIZE]; /* which signals we are ignoring */
1059 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1060 static int sig_defaulting[SIG_SIZE];
1064 #ifdef HAS_SIGPROCMASK
1066 restore_sigmask(pTHX_ SV *save_sv)
1068 sigset_t *ossetp = (sigset_t *) SvPV_nolen( save_sv );
1069 (void)sigprocmask(SIG_SETMASK, ossetp, (sigset_t *)0);
1073 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
1077 /* Are we fetching a signal entry? */
1078 i = whichsig(MgPV(mg,n_a));
1081 sv_setsv(sv,PL_psig_ptr[i]);
1083 Sighandler_t sigstate;
1084 sigstate = rsignal_state(i);
1085 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1086 if (sig_handlers_initted && sig_ignoring[i]) sigstate = SIG_IGN;
1088 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1089 if (sig_handlers_initted && sig_defaulting[i]) sigstate = SIG_DFL;
1091 /* cache state so we don't fetch it again */
1092 if(sigstate == SIG_IGN)
1093 sv_setpv(sv,"IGNORE");
1095 sv_setsv(sv,&PL_sv_undef);
1096 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1103 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
1105 /* XXX Some of this code was copied from Perl_magic_setsig. A little
1106 * refactoring might be in order.
1114 if (strEQ(s,"__DIE__"))
1116 else if (strEQ(s,"__WARN__"))
1119 Perl_croak(aTHX_ "No such hook: %s", s);
1123 SvREFCNT_dec(to_dec);
1128 /* Are we clearing a signal entry? */
1131 #ifdef HAS_SIGPROCMASK
1134 /* Avoid having the signal arrive at a bad time, if possible. */
1137 sigprocmask(SIG_BLOCK, &set, &save);
1139 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1140 SAVEFREESV(save_sv);
1141 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1144 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1145 if (!sig_handlers_initted) Perl_csighandler_init();
1147 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1148 sig_defaulting[i] = 1;
1149 (void)rsignal(i, &Perl_csighandler);
1151 (void)rsignal(i, SIG_DFL);
1153 if(PL_psig_name[i]) {
1154 SvREFCNT_dec(PL_psig_name[i]);
1157 if(PL_psig_ptr[i]) {
1158 to_dec=PL_psig_ptr[i];
1161 SvREFCNT_dec(to_dec);
1171 Perl_raise_signal(pTHX_ int sig)
1173 /* Set a flag to say this signal is pending */
1174 PL_psig_pend[sig]++;
1175 /* And one to say _a_ signal is pending */
1180 Perl_csighandler(int sig)
1182 #ifdef PERL_GET_SIG_CONTEXT
1183 dTHXa(PERL_GET_SIG_CONTEXT);
1187 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1188 (void) rsignal(sig, &Perl_csighandler);
1189 if (sig_ignoring[sig]) return;
1191 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1192 if (sig_defaulting[sig])
1193 #ifdef KILL_BY_SIGPRC
1194 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1199 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
1200 /* Call the perl level handler now--
1201 * with risk we may be in malloc() etc. */
1202 (*PL_sighandlerp)(sig);
1204 Perl_raise_signal(aTHX_ sig);
1207 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1209 Perl_csighandler_init(void)
1212 if (sig_handlers_initted) return;
1214 for (sig = 1; sig < SIG_SIZE; sig++) {
1215 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1217 sig_defaulting[sig] = 1;
1218 (void) rsignal(sig, &Perl_csighandler);
1220 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1221 sig_ignoring[sig] = 0;
1224 sig_handlers_initted = 1;
1229 Perl_despatch_signals(pTHX)
1233 for (sig = 1; sig < SIG_SIZE; sig++) {
1234 if (PL_psig_pend[sig]) {
1235 PERL_BLOCKSIG_ADD(set, sig);
1236 PL_psig_pend[sig] = 0;
1237 PERL_BLOCKSIG_BLOCK(set);
1238 (*PL_sighandlerp)(sig);
1239 PERL_BLOCKSIG_UNBLOCK(set);
1245 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1250 /* Need to be careful with SvREFCNT_dec(), because that can have side
1251 * effects (due to closures). We must make sure that the new disposition
1252 * is in place before it is called.
1256 #ifdef HAS_SIGPROCMASK
1263 if (strEQ(s,"__DIE__"))
1265 else if (strEQ(s,"__WARN__"))
1268 Perl_croak(aTHX_ "No such hook: %s", s);
1276 i = whichsig(s); /* ...no, a brick */
1278 if (ckWARN(WARN_SIGNAL))
1279 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
1282 #ifdef HAS_SIGPROCMASK
1283 /* Avoid having the signal arrive at a bad time, if possible. */
1286 sigprocmask(SIG_BLOCK, &set, &save);
1288 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1289 SAVEFREESV(save_sv);
1290 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1293 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1294 if (!sig_handlers_initted) Perl_csighandler_init();
1296 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1297 sig_ignoring[i] = 0;
1299 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1300 sig_defaulting[i] = 0;
1302 SvREFCNT_dec(PL_psig_name[i]);
1303 to_dec = PL_psig_ptr[i];
1304 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1305 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1306 PL_psig_name[i] = newSVpvn(s, len);
1307 SvREADONLY_on(PL_psig_name[i]);
1309 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1311 (void)rsignal(i, &Perl_csighandler);
1312 #ifdef HAS_SIGPROCMASK
1317 *svp = SvREFCNT_inc(sv);
1319 SvREFCNT_dec(to_dec);
1322 s = SvPV_force(sv,len);
1323 if (strEQ(s,"IGNORE")) {
1325 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1326 sig_ignoring[i] = 1;
1327 (void)rsignal(i, &Perl_csighandler);
1329 (void)rsignal(i, SIG_IGN);
1333 else if (strEQ(s,"DEFAULT") || !*s) {
1335 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1337 sig_defaulting[i] = 1;
1338 (void)rsignal(i, &Perl_csighandler);
1341 (void)rsignal(i, SIG_DFL);
1346 * We should warn if HINT_STRICT_REFS, but without
1347 * access to a known hint bit in a known OP, we can't
1348 * tell whether HINT_STRICT_REFS is in force or not.
1350 if (!strchr(s,':') && !strchr(s,'\''))
1351 sv_insert(sv, 0, 0, "main::", 6);
1353 (void)rsignal(i, &Perl_csighandler);
1355 *svp = SvREFCNT_inc(sv);
1357 #ifdef HAS_SIGPROCMASK
1362 SvREFCNT_dec(to_dec);
1365 #endif /* !PERL_MICRO */
1368 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1370 PL_sub_generation++;
1375 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1377 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1378 PL_amagic_generation++;
1384 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1386 HV *hv = (HV*)LvTARG(sv);
1390 (void) hv_iterinit(hv);
1391 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1394 while (hv_iternext(hv))
1399 sv_setiv(sv, (IV)i);
1404 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1407 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1412 /* caller is responsible for stack switching/cleanup */
1414 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1420 PUSHs(SvTIED_obj(sv, mg));
1423 if (mg->mg_len >= 0)
1424 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1425 else if (mg->mg_len == HEf_SVKEY)
1426 PUSHs((SV*)mg->mg_ptr);
1428 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1429 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1437 return call_method(meth, flags);
1441 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1447 PUSHSTACKi(PERLSI_MAGIC);
1449 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1450 sv_setsv(sv, *PL_stack_sp--);
1460 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1462 magic_methpack(sv,mg,"FETCH");
1464 mg->mg_flags |= MGf_GSKIP;
1469 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1473 PUSHSTACKi(PERLSI_MAGIC);
1474 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1481 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1483 return magic_methpack(sv,mg,"DELETE");
1488 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1495 PUSHSTACKi(PERLSI_MAGIC);
1496 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1497 sv = *PL_stack_sp--;
1498 retval = (U32) SvIV(sv)-1;
1507 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1512 PUSHSTACKi(PERLSI_MAGIC);
1514 XPUSHs(SvTIED_obj(sv, mg));
1516 call_method("CLEAR", G_SCALAR|G_DISCARD);
1523 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1526 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1530 PUSHSTACKi(PERLSI_MAGIC);
1533 PUSHs(SvTIED_obj(sv, mg));
1538 if (call_method(meth, G_SCALAR))
1539 sv_setsv(key, *PL_stack_sp--);
1548 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1550 return magic_methpack(sv,mg,"EXISTS");
1554 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1564 svp = av_fetch(GvAV(gv),
1565 atoi(MgPV(mg,n_a)), FALSE);
1566 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp)))) {
1567 /* set or clear breakpoint in the relevant control op */
1569 o->op_flags |= OPf_SPECIAL;
1571 o->op_flags &= ~OPf_SPECIAL;
1577 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1579 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1584 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1586 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1591 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1593 SV* lsv = LvTARG(sv);
1595 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1596 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1597 if (mg && mg->mg_len >= 0) {
1600 sv_pos_b2u(lsv, &i);
1601 sv_setiv(sv, i + PL_curcop->cop_arybase);
1610 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1612 SV* lsv = LvTARG(sv);
1619 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1620 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1624 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1625 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1627 else if (!SvOK(sv)) {
1631 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1633 pos = SvIV(sv) - PL_curcop->cop_arybase;
1636 ulen = sv_len_utf8(lsv);
1646 else if (pos > (SSize_t)len)
1651 sv_pos_u2b(lsv, &p, 0);
1656 mg->mg_flags &= ~MGf_MINMATCH;
1662 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1664 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1666 gv_efullname3(sv,((GV*)sv), "*");
1670 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1675 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1684 if (*s == '*' && s[1])
1686 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1691 GvGP(sv) = gp_ref(GvGP(gv));
1696 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1699 SV *lsv = LvTARG(sv);
1700 char *tmps = SvPV(lsv,len);
1701 I32 offs = LvTARGOFF(sv);
1702 I32 rem = LvTARGLEN(sv);
1705 sv_pos_u2b(lsv, &offs, &rem);
1706 if (offs > (I32)len)
1708 if (rem + offs > (I32)len)
1710 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1717 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1720 char *tmps = SvPV(sv, len);
1721 SV *lsv = LvTARG(sv);
1722 I32 lvoff = LvTARGOFF(sv);
1723 I32 lvlen = LvTARGLEN(sv);
1726 sv_utf8_upgrade(lsv);
1727 sv_pos_u2b(lsv, &lvoff, &lvlen);
1728 sv_insert(lsv, lvoff, lvlen, tmps, len);
1731 else if (lsv && SvUTF8(lsv)) {
1732 sv_pos_u2b(lsv, &lvoff, &lvlen);
1733 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
1734 sv_insert(lsv, lvoff, lvlen, tmps, len);
1738 sv_insert(lsv, lvoff, lvlen, tmps, len);
1744 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1746 TAINT_IF((mg->mg_len & 1) ||
1747 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1752 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1754 if (PL_localizing) {
1755 if (PL_localizing == 1)
1760 else if (PL_tainted)
1768 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1770 SV *lsv = LvTARG(sv);
1777 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1782 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1784 do_vecset(sv); /* XXX slurp this routine */
1789 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1792 if (LvTARGLEN(sv)) {
1794 SV *ahv = LvTARG(sv);
1795 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1800 AV* av = (AV*)LvTARG(sv);
1801 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1802 targ = AvARRAY(av)[LvTARGOFF(sv)];
1804 if (targ && targ != &PL_sv_undef) {
1805 /* somebody else defined it for us */
1806 SvREFCNT_dec(LvTARG(sv));
1807 LvTARG(sv) = SvREFCNT_inc(targ);
1809 SvREFCNT_dec(mg->mg_obj);
1810 mg->mg_obj = Nullsv;
1811 mg->mg_flags &= ~MGf_REFCOUNTED;
1816 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1821 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1826 sv_setsv(LvTARG(sv), sv);
1827 SvSETMAGIC(LvTARG(sv));
1833 Perl_vivify_defelem(pTHX_ SV *sv)
1838 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
1841 SV *ahv = LvTARG(sv);
1843 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1846 if (!value || value == &PL_sv_undef)
1847 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1850 AV* av = (AV*)LvTARG(sv);
1851 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1852 LvTARG(sv) = Nullsv; /* array can't be extended */
1854 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1855 if (!svp || (value = *svp) == &PL_sv_undef)
1856 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1859 (void)SvREFCNT_inc(value);
1860 SvREFCNT_dec(LvTARG(sv));
1863 SvREFCNT_dec(mg->mg_obj);
1864 mg->mg_obj = Nullsv;
1865 mg->mg_flags &= ~MGf_REFCOUNTED;
1869 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1871 AV *av = (AV*)mg->mg_obj;
1872 SV **svp = AvARRAY(av);
1873 I32 i = AvFILLp(av);
1875 if (svp[i] && svp[i] != &PL_sv_undef) {
1876 if (!SvWEAKREF(svp[i]))
1877 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1878 /* XXX Should we check that it hasn't changed? */
1880 (void)SvOK_off(svp[i]);
1881 SvWEAKREF_off(svp[i]);
1882 svp[i] = &PL_sv_undef;
1890 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1898 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1900 sv_unmagic(sv, PERL_MAGIC_bm);
1906 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1908 sv_unmagic(sv, PERL_MAGIC_fm);
1914 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1916 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1918 if (uf && uf->uf_set)
1919 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
1924 Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
1926 sv_unmagic(sv, PERL_MAGIC_qr);
1931 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1933 regexp *re = (regexp *)mg->mg_obj;
1938 #ifdef USE_LOCALE_COLLATE
1940 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1943 * RenE<eacute> Descartes said "I think not."
1944 * and vanished with a faint plop.
1947 Safefree(mg->mg_ptr);
1953 #endif /* USE_LOCALE_COLLATE */
1955 /* Just clear the UTF-8 cache data. */
1957 Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
1959 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
1961 mg->mg_len = -1; /* The mg_len holds the len cache. */
1966 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1971 switch (*mg->mg_ptr) {
1972 case '\001': /* ^A */
1973 sv_setsv(PL_bodytarget, sv);
1975 case '\003': /* ^C */
1976 PL_minus_c = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1979 case '\004': /* ^D */
1980 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
1981 DEBUG_x(dump_all());
1983 case '\005': /* ^E */
1984 if (*(mg->mg_ptr+1) == '\0') {
1985 #ifdef MACOS_TRADITIONAL
1986 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1989 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1992 SetLastError( SvIV(sv) );
1995 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1997 /* will anyone ever use this? */
1998 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
2004 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
2006 SvREFCNT_dec(PL_encoding);
2007 if (SvOK(sv) || SvGMAGICAL(sv)) {
2008 PL_encoding = newSVsv(sv);
2011 PL_encoding = Nullsv;
2015 case '\006': /* ^F */
2016 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2018 case '\010': /* ^H */
2019 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2021 case '\011': /* ^I */ /* NOT \t in EBCDIC */
2023 Safefree(PL_inplace);
2025 PL_inplace = savepv(SvPV(sv,len));
2027 PL_inplace = Nullch;
2029 case '\017': /* ^O */
2030 if (*(mg->mg_ptr+1) == '\0') {
2032 Safefree(PL_osname);
2034 PL_osname = savepv(SvPV(sv,len));
2038 else if (strEQ(mg->mg_ptr, "\017PEN")) {
2039 if (!PL_compiling.cop_io)
2040 PL_compiling.cop_io = newSVsv(sv);
2042 sv_setsv(PL_compiling.cop_io,sv);
2045 case '\020': /* ^P */
2046 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2047 if (PL_perldb && !PL_DBsingle)
2050 case '\024': /* ^T */
2052 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
2054 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2057 case '\027': /* ^W & $^WARNING_BITS */
2058 if (*(mg->mg_ptr+1) == '\0') {
2059 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2060 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2061 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
2062 | (i ? G_WARN_ON : G_WARN_OFF) ;
2065 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
2066 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2067 if (!SvPOK(sv) && PL_localizing) {
2068 sv_setpvn(sv, WARN_NONEstring, WARNsize);
2069 PL_compiling.cop_warnings = pWARN_NONE;
2074 int accumulate = 0 ;
2075 int any_fatals = 0 ;
2076 char * ptr = (char*)SvPV(sv, len) ;
2077 for (i = 0 ; i < len ; ++i) {
2078 accumulate |= ptr[i] ;
2079 any_fatals |= (ptr[i] & 0xAA) ;
2082 PL_compiling.cop_warnings = pWARN_NONE;
2083 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
2084 PL_compiling.cop_warnings = pWARN_ALL;
2085 PL_dowarn |= G_WARN_ONCE ;
2088 if (specialWARN(PL_compiling.cop_warnings))
2089 PL_compiling.cop_warnings = newSVsv(sv) ;
2091 sv_setsv(PL_compiling.cop_warnings, sv);
2092 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2093 PL_dowarn |= G_WARN_ONCE ;
2101 if (PL_localizing) {
2102 if (PL_localizing == 1)
2103 SAVESPTR(PL_last_in_gv);
2105 else if (SvOK(sv) && GvIO(PL_last_in_gv))
2106 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
2109 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2110 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
2111 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
2114 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2115 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
2116 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
2119 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2122 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2123 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2124 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
2127 IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2131 IO *io = GvIOp(PL_defoutgv);
2134 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
2135 IoFLAGS(io) &= ~IOf_FLUSH;
2137 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2138 PerlIO *ofp = IoOFP(io);
2140 (void)PerlIO_flush(ofp);
2141 IoFLAGS(io) |= IOf_FLUSH;
2147 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2148 PL_multiline = (i != 0);
2151 SvREFCNT_dec(PL_rs);
2152 PL_rs = newSVsv(sv);
2156 SvREFCNT_dec(PL_ors_sv);
2157 if (SvOK(sv) || SvGMAGICAL(sv)) {
2158 PL_ors_sv = newSVsv(sv);
2166 SvREFCNT_dec(PL_ofs_sv);
2167 if (SvOK(sv) || SvGMAGICAL(sv)) {
2168 PL_ofs_sv = newSVsv(sv);
2177 PL_ofmt = savepv(SvPV(sv,len));
2180 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2183 #ifdef COMPLEX_STATUS
2184 if (PL_localizing == 2) {
2185 PL_statusvalue = LvTARGOFF(sv);
2186 PL_statusvalue_vms = LvTARGLEN(sv);
2190 #ifdef VMSISH_STATUS
2192 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
2195 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2200 # define PERL_VMS_BANG vaxc$errno
2202 # define PERL_VMS_BANG 0
2204 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
2205 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
2209 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2210 if (PL_delaymagic) {
2211 PL_delaymagic |= DM_RUID;
2212 break; /* don't do magic till later */
2215 (void)setruid((Uid_t)PL_uid);
2218 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
2220 #ifdef HAS_SETRESUID
2221 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
2223 if (PL_uid == PL_euid) /* special case $< = $> */
2224 (void)PerlProc_setuid(PL_uid);
2226 PL_uid = PerlProc_getuid();
2227 Perl_croak(aTHX_ "setruid() not implemented");
2232 PL_uid = PerlProc_getuid();
2233 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2236 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2237 if (PL_delaymagic) {
2238 PL_delaymagic |= DM_EUID;
2239 break; /* don't do magic till later */
2242 (void)seteuid((Uid_t)PL_euid);
2245 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
2247 #ifdef HAS_SETRESUID
2248 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
2250 if (PL_euid == PL_uid) /* special case $> = $< */
2251 PerlProc_setuid(PL_euid);
2253 PL_euid = PerlProc_geteuid();
2254 Perl_croak(aTHX_ "seteuid() not implemented");
2259 PL_euid = PerlProc_geteuid();
2260 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2263 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2264 if (PL_delaymagic) {
2265 PL_delaymagic |= DM_RGID;
2266 break; /* don't do magic till later */
2269 (void)setrgid((Gid_t)PL_gid);
2272 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
2274 #ifdef HAS_SETRESGID
2275 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2277 if (PL_gid == PL_egid) /* special case $( = $) */
2278 (void)PerlProc_setgid(PL_gid);
2280 PL_gid = PerlProc_getgid();
2281 Perl_croak(aTHX_ "setrgid() not implemented");
2286 PL_gid = PerlProc_getgid();
2287 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2290 #ifdef HAS_SETGROUPS
2292 char *p = SvPV(sv, len);
2293 Groups_t gary[NGROUPS];
2298 for (i = 0; i < NGROUPS; ++i) {
2299 while (*p && !isSPACE(*p))
2308 (void)setgroups(i, gary);
2310 #else /* HAS_SETGROUPS */
2311 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2312 #endif /* HAS_SETGROUPS */
2313 if (PL_delaymagic) {
2314 PL_delaymagic |= DM_EGID;
2315 break; /* don't do magic till later */
2318 (void)setegid((Gid_t)PL_egid);
2321 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2323 #ifdef HAS_SETRESGID
2324 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2326 if (PL_egid == PL_gid) /* special case $) = $( */
2327 (void)PerlProc_setgid(PL_egid);
2329 PL_egid = PerlProc_getegid();
2330 Perl_croak(aTHX_ "setegid() not implemented");
2335 PL_egid = PerlProc_getegid();
2336 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2339 PL_chopset = SvPV_force(sv,len);
2341 #ifndef MACOS_TRADITIONAL
2343 LOCK_DOLLARZERO_MUTEX;
2344 #ifdef HAS_SETPROCTITLE
2345 /* The BSDs don't show the argv[] in ps(1) output, they
2346 * show a string from the process struct and provide
2347 * the setproctitle() routine to manipulate that. */
2350 # if __FreeBSD_version > 410001
2351 /* The leading "-" removes the "perl: " prefix,
2352 * but not the "(perl) suffix from the ps(1)
2353 * output, because that's what ps(1) shows if the
2354 * argv[] is modified. */
2355 setproctitle("-%s", s);
2356 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2357 /* This doesn't really work if you assume that
2358 * $0 = 'foobar'; will wipe out 'perl' from the $0
2359 * because in ps(1) output the result will be like
2360 * sprintf("perl: %s (perl)", s)
2361 * I guess this is a security feature:
2362 * one (a user process) cannot get rid of the original name.
2364 setproctitle("%s", s);
2368 #if defined(__hpux) && defined(PSTAT_SETCMD)
2373 pstat(PSTAT_SETCMD, un, len, 0, 0);
2379 /* See if all the arguments are contiguous in memory */
2380 for (i = 1; i < PL_origargc; i++) {
2381 if (PL_origargv[i] == s + 1
2383 || PL_origargv[i] == s + 2
2388 s += strlen(s); /* this one is ok too */
2393 /* can grab env area too? */
2396 && PL_curinterp == aTHX
2398 && (PL_origenviron[0] == s + 1))
2400 my_setenv("NoNe SuCh", Nullch);
2401 /* force copy of environment */
2402 for (i = 0; PL_origenviron[i]; i++)
2403 if (PL_origenviron[i] == s + 1) {
2410 PL_origalen = s - PL_origargv[0];
2412 s = SvPV_force(sv,len);
2414 if (i >= (I32)PL_origalen) {
2416 /* don't allow system to limit $0 seen by script */
2417 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2418 Copy(s, PL_origargv[0], i, char);
2419 s = PL_origargv[0]+i;
2423 Copy(s, PL_origargv[0], i, char);
2424 s = PL_origargv[0]+i;
2426 while (++i < (I32)PL_origalen)
2428 for (i = 1; i < PL_origargc; i++)
2429 PL_origargv[i] = Nullch;
2431 UNLOCK_DOLLARZERO_MUTEX;
2439 Perl_whichsig(pTHX_ char *sig)
2441 register char **sigv;
2443 for (sigv = PL_sig_name; *sigv; sigv++)
2444 if (strEQ(sig,*sigv))
2445 return PL_sig_num[sigv - PL_sig_name];
2447 if (strEQ(sig,"CHLD"))
2451 if (strEQ(sig,"CLD"))
2457 #if !defined(PERL_IMPLICIT_CONTEXT)
2462 Perl_sighandler(int sig)
2464 #ifdef PERL_GET_SIG_CONTEXT
2465 dTHXa(PERL_GET_SIG_CONTEXT);
2472 SV *sv = Nullsv, *tSv = PL_Sv;
2478 if (PL_savestack_ix + 15 <= PL_savestack_max)
2480 if (PL_markstack_ptr < PL_markstack_max - 2)
2482 if (PL_retstack_ix < PL_retstack_max - 2)
2484 if (PL_scopestack_ix < PL_scopestack_max - 3)
2487 if (!PL_psig_ptr[sig]) {
2488 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
2493 /* Max number of items pushed there is 3*n or 4. We cannot fix
2494 infinity, so we fix 4 (in fact 5): */
2496 PL_savestack_ix += 5; /* Protect save in progress. */
2497 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2500 PL_markstack_ptr++; /* Protect mark. */
2503 PL_retstack[PL_retstack_ix] = NULL;
2506 PL_scopestack_ix += 1;
2507 /* sv_2cv is too complicated, try a simpler variant first: */
2508 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2509 || SvTYPE(cv) != SVt_PVCV)
2510 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2512 if (!cv || !CvROOT(cv)) {
2513 if (ckWARN(WARN_SIGNAL))
2514 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
2515 PL_sig_name[sig], (gv ? GvENAME(gv)
2522 if(PL_psig_name[sig]) {
2523 sv = SvREFCNT_inc(PL_psig_name[sig]);
2525 #if !defined(PERL_IMPLICIT_CONTEXT)
2529 sv = sv_newmortal();
2530 sv_setpv(sv,PL_sig_name[sig]);
2533 PUSHSTACKi(PERLSI_SIGNAL);
2538 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2541 if (SvTRUE(ERRSV)) {
2543 #ifdef HAS_SIGPROCMASK
2544 /* Handler "died", for example to get out of a restart-able read().
2545 * Before we re-do that on its behalf re-enable the signal which was
2546 * blocked by the system when we entered.
2550 sigaddset(&set,sig);
2551 sigprocmask(SIG_UNBLOCK, &set, NULL);
2553 /* Not clear if this will work */
2554 (void)rsignal(sig, SIG_IGN);
2555 (void)rsignal(sig, &Perl_csighandler);
2557 #endif /* !PERL_MICRO */
2558 Perl_die(aTHX_ Nullformat);
2562 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2568 PL_scopestack_ix -= 1;
2571 PL_op = myop; /* Apparently not needed... */
2573 PL_Sv = tSv; /* Restore global temporaries. */
2580 restore_magic(pTHX_ void *p)
2582 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2583 SV* sv = mgs->mgs_sv;
2588 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2591 SvFLAGS(sv) |= mgs->mgs_flags;
2595 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2598 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2600 /* If we're still on top of the stack, pop us off. (That condition
2601 * will be satisfied if restore_magic was called explicitly, but *not*
2602 * if it's being called via leave_scope.)
2603 * The reason for doing this is that otherwise, things like sv_2cv()
2604 * may leave alloc gunk on the savestack, and some code
2605 * (e.g. sighandler) doesn't expect that...
2607 if (PL_savestack_ix == mgs->mgs_ss_ix)
2609 I32 popval = SSPOPINT;
2610 assert(popval == SAVEt_DESTRUCTOR_X);
2611 PL_savestack_ix -= 2;
2613 assert(popval == SAVEt_ALLOC);
2615 PL_savestack_ix -= popval;
2621 unwind_handler_stack(pTHX_ void *p)
2623 U32 flags = *(U32*)p;
2626 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2627 /* cxstack_ix-- Not needed, die already unwound it. */
2628 #if !defined(PERL_IMPLICIT_CONTEXT)
2630 SvREFCNT_dec(sig_sv);