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! */
895 #ifndef MACOS_TRADITIONAL
904 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
906 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
908 if (uf && uf->uf_val)
909 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
914 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
924 #ifdef DYNAMIC_ENV_FETCH
925 /* We just undefd an environment var. Is a replacement */
926 /* waiting in the wings? */
929 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
930 s = SvPV(*valp, len);
934 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
935 /* And you'll never guess what the dog had */
936 /* in its mouth... */
938 MgTAINTEDDIR_off(mg);
940 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
941 char pathbuf[256], eltbuf[256], *cp, *elt = s;
945 do { /* DCL$PATH may be a search list */
946 while (1) { /* as may dev portion of any element */
947 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
948 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
949 cando_by_name(S_IWUSR,0,elt) ) {
954 if ((cp = strchr(elt, ':')) != Nullch)
956 if (my_trnlnm(elt, eltbuf, j++))
962 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
965 if (s && klen == 4 && strEQ(ptr,"PATH")) {
966 char *strend = s + len;
972 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
975 if (i >= sizeof tmpbuf /* too long -- assume the worst */
977 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
984 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
990 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
993 my_setenv(MgPV(mg,n_a),Nullch);
998 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
1001 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1003 if (PL_localizing) {
1006 magic_clear_all_env(sv,mg);
1007 hv_iterinit((HV*)sv);
1008 while ((entry = hv_iternext((HV*)sv))) {
1010 my_setenv(hv_iterkey(entry, &keylen),
1011 SvPV(hv_iterval((HV*)sv, entry), n_a));
1019 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
1021 #if defined(VMS) || defined(EPOC)
1022 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1024 # if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
1027 # ifdef USE_ENVIRON_ARRAY
1028 # if defined(USE_ITHREADS)
1029 /* only the parent thread can clobber the process environment */
1030 if (PL_curinterp == aTHX)
1033 # ifndef PERL_USE_SAFE_PUTENV
1036 if (environ == PL_origenviron)
1037 environ = (char**)safesysmalloc(sizeof(char*));
1039 for (i = 0; environ[i]; i++)
1040 safesysfree(environ[i]);
1041 # endif /* PERL_USE_SAFE_PUTENV */
1043 environ[0] = Nullch;
1045 # endif /* USE_ENVIRON_ARRAY */
1046 # endif /* PERL_IMPLICIT_SYS || WIN32 */
1047 #endif /* VMS || EPC */
1051 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS)||defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1052 static int sig_handlers_initted = 0;
1054 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1055 static int sig_ignoring[SIG_SIZE]; /* which signals we are ignoring */
1057 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1058 static int sig_defaulting[SIG_SIZE];
1062 #ifdef HAS_SIGPROCMASK
1064 restore_sigmask(pTHX_ SV *save_sv)
1066 sigset_t *ossetp = (sigset_t *) SvPV_nolen( save_sv );
1067 (void)sigprocmask(SIG_SETMASK, ossetp, (sigset_t *)0);
1071 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
1075 /* Are we fetching a signal entry? */
1076 i = whichsig(MgPV(mg,n_a));
1079 sv_setsv(sv,PL_psig_ptr[i]);
1081 Sighandler_t sigstate;
1082 sigstate = rsignal_state(i);
1083 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1084 if (sig_handlers_initted && sig_ignoring[i]) sigstate = SIG_IGN;
1086 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1087 if (sig_handlers_initted && sig_defaulting[i]) sigstate = SIG_DFL;
1089 /* cache state so we don't fetch it again */
1090 if(sigstate == SIG_IGN)
1091 sv_setpv(sv,"IGNORE");
1093 sv_setsv(sv,&PL_sv_undef);
1094 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1101 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
1103 /* XXX Some of this code was copied from Perl_magic_setsig. A little
1104 * refactoring might be in order.
1112 if (strEQ(s,"__DIE__"))
1114 else if (strEQ(s,"__WARN__"))
1117 Perl_croak(aTHX_ "No such hook: %s", s);
1121 SvREFCNT_dec(to_dec);
1126 /* Are we clearing a signal entry? */
1129 #ifdef HAS_SIGPROCMASK
1132 /* Avoid having the signal arrive at a bad time, if possible. */
1135 sigprocmask(SIG_BLOCK, &set, &save);
1137 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1138 SAVEFREESV(save_sv);
1139 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1142 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1143 if (!sig_handlers_initted) Perl_csighandler_init();
1145 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1146 sig_defaulting[i] = 1;
1147 (void)rsignal(i, &Perl_csighandler);
1149 (void)rsignal(i, SIG_DFL);
1151 if(PL_psig_name[i]) {
1152 SvREFCNT_dec(PL_psig_name[i]);
1155 if(PL_psig_ptr[i]) {
1156 to_dec=PL_psig_ptr[i];
1159 SvREFCNT_dec(to_dec);
1169 Perl_raise_signal(pTHX_ int sig)
1171 /* Set a flag to say this signal is pending */
1172 PL_psig_pend[sig]++;
1173 /* And one to say _a_ signal is pending */
1178 Perl_csighandler(int sig)
1180 #ifdef PERL_GET_SIG_CONTEXT
1181 dTHXa(PERL_GET_SIG_CONTEXT);
1185 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1186 (void) rsignal(sig, &Perl_csighandler);
1187 if (sig_ignoring[sig]) return;
1189 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1190 if (sig_defaulting[sig])
1191 #ifdef KILL_BY_SIGPRC
1192 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1197 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
1198 /* Call the perl level handler now--
1199 * with risk we may be in malloc() etc. */
1200 (*PL_sighandlerp)(sig);
1202 Perl_raise_signal(aTHX_ sig);
1205 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1207 Perl_csighandler_init(void)
1210 if (sig_handlers_initted) return;
1212 for (sig = 1; sig < SIG_SIZE; sig++) {
1213 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1215 sig_defaulting[sig] = 1;
1216 (void) rsignal(sig, &Perl_csighandler);
1218 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1219 sig_ignoring[sig] = 0;
1222 sig_handlers_initted = 1;
1227 Perl_despatch_signals(pTHX)
1231 for (sig = 1; sig < SIG_SIZE; sig++) {
1232 if (PL_psig_pend[sig]) {
1233 PERL_BLOCKSIG_ADD(set, sig);
1234 PL_psig_pend[sig] = 0;
1235 PERL_BLOCKSIG_BLOCK(set);
1236 (*PL_sighandlerp)(sig);
1237 PERL_BLOCKSIG_UNBLOCK(set);
1243 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1248 /* Need to be careful with SvREFCNT_dec(), because that can have side
1249 * effects (due to closures). We must make sure that the new disposition
1250 * is in place before it is called.
1254 #ifdef HAS_SIGPROCMASK
1261 if (strEQ(s,"__DIE__"))
1263 else if (strEQ(s,"__WARN__"))
1266 Perl_croak(aTHX_ "No such hook: %s", s);
1274 i = whichsig(s); /* ...no, a brick */
1276 if (ckWARN(WARN_SIGNAL))
1277 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
1280 #ifdef HAS_SIGPROCMASK
1281 /* Avoid having the signal arrive at a bad time, if possible. */
1284 sigprocmask(SIG_BLOCK, &set, &save);
1286 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1287 SAVEFREESV(save_sv);
1288 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1291 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1292 if (!sig_handlers_initted) Perl_csighandler_init();
1294 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1295 sig_ignoring[i] = 0;
1297 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1298 sig_defaulting[i] = 0;
1300 SvREFCNT_dec(PL_psig_name[i]);
1301 to_dec = PL_psig_ptr[i];
1302 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1303 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1304 PL_psig_name[i] = newSVpvn(s, len);
1305 SvREADONLY_on(PL_psig_name[i]);
1307 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1309 (void)rsignal(i, &Perl_csighandler);
1310 #ifdef HAS_SIGPROCMASK
1315 *svp = SvREFCNT_inc(sv);
1317 SvREFCNT_dec(to_dec);
1320 s = SvPV_force(sv,len);
1321 if (strEQ(s,"IGNORE")) {
1323 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1324 sig_ignoring[i] = 1;
1325 (void)rsignal(i, &Perl_csighandler);
1327 (void)rsignal(i, SIG_IGN);
1331 else if (strEQ(s,"DEFAULT") || !*s) {
1333 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1335 sig_defaulting[i] = 1;
1336 (void)rsignal(i, &Perl_csighandler);
1339 (void)rsignal(i, SIG_DFL);
1344 * We should warn if HINT_STRICT_REFS, but without
1345 * access to a known hint bit in a known OP, we can't
1346 * tell whether HINT_STRICT_REFS is in force or not.
1348 if (!strchr(s,':') && !strchr(s,'\''))
1349 sv_insert(sv, 0, 0, "main::", 6);
1351 (void)rsignal(i, &Perl_csighandler);
1353 *svp = SvREFCNT_inc(sv);
1355 #ifdef HAS_SIGPROCMASK
1360 SvREFCNT_dec(to_dec);
1363 #endif /* !PERL_MICRO */
1366 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1368 PL_sub_generation++;
1373 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1375 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1376 PL_amagic_generation++;
1382 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1384 HV *hv = (HV*)LvTARG(sv);
1388 (void) hv_iterinit(hv);
1389 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1392 while (hv_iternext(hv))
1397 sv_setiv(sv, (IV)i);
1402 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1405 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1410 /* caller is responsible for stack switching/cleanup */
1412 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1418 PUSHs(SvTIED_obj(sv, mg));
1421 if (mg->mg_len >= 0)
1422 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1423 else if (mg->mg_len == HEf_SVKEY)
1424 PUSHs((SV*)mg->mg_ptr);
1426 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1427 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1435 return call_method(meth, flags);
1439 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1445 PUSHSTACKi(PERLSI_MAGIC);
1447 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1448 sv_setsv(sv, *PL_stack_sp--);
1458 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1460 magic_methpack(sv,mg,"FETCH");
1462 mg->mg_flags |= MGf_GSKIP;
1467 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1471 PUSHSTACKi(PERLSI_MAGIC);
1472 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1479 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1481 return magic_methpack(sv,mg,"DELETE");
1486 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1493 PUSHSTACKi(PERLSI_MAGIC);
1494 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1495 sv = *PL_stack_sp--;
1496 retval = (U32) SvIV(sv)-1;
1505 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1510 PUSHSTACKi(PERLSI_MAGIC);
1512 XPUSHs(SvTIED_obj(sv, mg));
1514 call_method("CLEAR", G_SCALAR|G_DISCARD);
1521 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1524 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1528 PUSHSTACKi(PERLSI_MAGIC);
1531 PUSHs(SvTIED_obj(sv, mg));
1536 if (call_method(meth, G_SCALAR))
1537 sv_setsv(key, *PL_stack_sp--);
1546 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1548 return magic_methpack(sv,mg,"EXISTS");
1552 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1562 svp = av_fetch(GvAV(gv),
1563 atoi(MgPV(mg,n_a)), FALSE);
1564 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp)))) {
1565 /* set or clear breakpoint in the relevant control op */
1567 o->op_flags |= OPf_SPECIAL;
1569 o->op_flags &= ~OPf_SPECIAL;
1575 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1577 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1582 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1584 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1589 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1591 SV* lsv = LvTARG(sv);
1593 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1594 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1595 if (mg && mg->mg_len >= 0) {
1598 sv_pos_b2u(lsv, &i);
1599 sv_setiv(sv, i + PL_curcop->cop_arybase);
1608 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1610 SV* lsv = LvTARG(sv);
1617 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1618 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1622 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1623 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1625 else if (!SvOK(sv)) {
1629 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1631 pos = SvIV(sv) - PL_curcop->cop_arybase;
1634 ulen = sv_len_utf8(lsv);
1644 else if (pos > (SSize_t)len)
1649 sv_pos_u2b(lsv, &p, 0);
1654 mg->mg_flags &= ~MGf_MINMATCH;
1660 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1662 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1664 gv_efullname3(sv,((GV*)sv), "*");
1668 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1673 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1682 if (*s == '*' && s[1])
1684 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1689 GvGP(sv) = gp_ref(GvGP(gv));
1694 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1697 SV *lsv = LvTARG(sv);
1698 char *tmps = SvPV(lsv,len);
1699 I32 offs = LvTARGOFF(sv);
1700 I32 rem = LvTARGLEN(sv);
1703 sv_pos_u2b(lsv, &offs, &rem);
1704 if (offs > (I32)len)
1706 if (rem + offs > (I32)len)
1708 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1715 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1718 char *tmps = SvPV(sv, len);
1719 SV *lsv = LvTARG(sv);
1720 I32 lvoff = LvTARGOFF(sv);
1721 I32 lvlen = LvTARGLEN(sv);
1724 sv_utf8_upgrade(lsv);
1725 sv_pos_u2b(lsv, &lvoff, &lvlen);
1726 sv_insert(lsv, lvoff, lvlen, tmps, len);
1729 else if (lsv && SvUTF8(lsv)) {
1730 sv_pos_u2b(lsv, &lvoff, &lvlen);
1731 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
1732 sv_insert(lsv, lvoff, lvlen, tmps, len);
1736 sv_insert(lsv, lvoff, lvlen, tmps, len);
1742 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1744 TAINT_IF((mg->mg_len & 1) ||
1745 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1750 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1752 if (PL_localizing) {
1753 if (PL_localizing == 1)
1758 else if (PL_tainted)
1766 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1768 SV *lsv = LvTARG(sv);
1775 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1780 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1782 do_vecset(sv); /* XXX slurp this routine */
1787 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1790 if (LvTARGLEN(sv)) {
1792 SV *ahv = LvTARG(sv);
1793 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1798 AV* av = (AV*)LvTARG(sv);
1799 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1800 targ = AvARRAY(av)[LvTARGOFF(sv)];
1802 if (targ && targ != &PL_sv_undef) {
1803 /* somebody else defined it for us */
1804 SvREFCNT_dec(LvTARG(sv));
1805 LvTARG(sv) = SvREFCNT_inc(targ);
1807 SvREFCNT_dec(mg->mg_obj);
1808 mg->mg_obj = Nullsv;
1809 mg->mg_flags &= ~MGf_REFCOUNTED;
1814 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1819 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1824 sv_setsv(LvTARG(sv), sv);
1825 SvSETMAGIC(LvTARG(sv));
1831 Perl_vivify_defelem(pTHX_ SV *sv)
1836 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
1839 SV *ahv = LvTARG(sv);
1841 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1844 if (!value || value == &PL_sv_undef)
1845 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1848 AV* av = (AV*)LvTARG(sv);
1849 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1850 LvTARG(sv) = Nullsv; /* array can't be extended */
1852 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1853 if (!svp || (value = *svp) == &PL_sv_undef)
1854 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1857 (void)SvREFCNT_inc(value);
1858 SvREFCNT_dec(LvTARG(sv));
1861 SvREFCNT_dec(mg->mg_obj);
1862 mg->mg_obj = Nullsv;
1863 mg->mg_flags &= ~MGf_REFCOUNTED;
1867 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1869 AV *av = (AV*)mg->mg_obj;
1870 SV **svp = AvARRAY(av);
1871 I32 i = AvFILLp(av);
1873 if (svp[i] && svp[i] != &PL_sv_undef) {
1874 if (!SvWEAKREF(svp[i]))
1875 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1876 /* XXX Should we check that it hasn't changed? */
1878 (void)SvOK_off(svp[i]);
1879 SvWEAKREF_off(svp[i]);
1880 svp[i] = &PL_sv_undef;
1884 SvREFCNT_dec(av); /* remove extra count added by sv_add_backref() */
1889 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1897 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1899 sv_unmagic(sv, PERL_MAGIC_bm);
1905 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1907 sv_unmagic(sv, PERL_MAGIC_fm);
1913 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1915 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1917 if (uf && uf->uf_set)
1918 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
1923 Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
1925 sv_unmagic(sv, PERL_MAGIC_qr);
1930 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1932 regexp *re = (regexp *)mg->mg_obj;
1937 #ifdef USE_LOCALE_COLLATE
1939 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1942 * RenE<eacute> Descartes said "I think not."
1943 * and vanished with a faint plop.
1946 Safefree(mg->mg_ptr);
1952 #endif /* USE_LOCALE_COLLATE */
1954 /* Just clear the UTF-8 cache data. */
1956 Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
1958 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
1960 mg->mg_len = -1; /* The mg_len holds the len cache. */
1965 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1970 switch (*mg->mg_ptr) {
1971 case '\001': /* ^A */
1972 sv_setsv(PL_bodytarget, sv);
1974 case '\003': /* ^C */
1975 PL_minus_c = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1978 case '\004': /* ^D */
1981 PL_debug = get_debug_opts(&s) | DEBUG_TOP_FLAG;
1982 DEBUG_x(dump_all());
1984 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
1987 case '\005': /* ^E */
1988 if (*(mg->mg_ptr+1) == '\0') {
1989 #ifdef MACOS_TRADITIONAL
1990 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1993 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1996 SetLastError( SvIV(sv) );
1999 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2001 /* will anyone ever use this? */
2002 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
2008 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
2010 SvREFCNT_dec(PL_encoding);
2011 if (SvOK(sv) || SvGMAGICAL(sv)) {
2012 PL_encoding = newSVsv(sv);
2015 PL_encoding = Nullsv;
2019 case '\006': /* ^F */
2020 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2022 case '\010': /* ^H */
2023 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2025 case '\011': /* ^I */ /* NOT \t in EBCDIC */
2027 Safefree(PL_inplace);
2029 PL_inplace = savepv(SvPV(sv,len));
2031 PL_inplace = Nullch;
2033 case '\017': /* ^O */
2034 if (*(mg->mg_ptr+1) == '\0') {
2036 Safefree(PL_osname);
2038 PL_osname = savepv(SvPV(sv,len));
2042 else if (strEQ(mg->mg_ptr, "\017PEN")) {
2043 if (!PL_compiling.cop_io)
2044 PL_compiling.cop_io = newSVsv(sv);
2046 sv_setsv(PL_compiling.cop_io,sv);
2049 case '\020': /* ^P */
2050 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2051 if (PL_perldb && !PL_DBsingle)
2054 case '\024': /* ^T */
2056 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
2058 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2061 case '\027': /* ^W & $^WARNING_BITS */
2062 if (*(mg->mg_ptr+1) == '\0') {
2063 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2064 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2065 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
2066 | (i ? G_WARN_ON : G_WARN_OFF) ;
2069 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
2070 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2071 if (!SvPOK(sv) && PL_localizing) {
2072 sv_setpvn(sv, WARN_NONEstring, WARNsize);
2073 PL_compiling.cop_warnings = pWARN_NONE;
2078 int accumulate = 0 ;
2079 int any_fatals = 0 ;
2080 char * ptr = (char*)SvPV(sv, len) ;
2081 for (i = 0 ; i < len ; ++i) {
2082 accumulate |= ptr[i] ;
2083 any_fatals |= (ptr[i] & 0xAA) ;
2086 PL_compiling.cop_warnings = pWARN_NONE;
2087 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
2088 PL_compiling.cop_warnings = pWARN_ALL;
2089 PL_dowarn |= G_WARN_ONCE ;
2092 if (specialWARN(PL_compiling.cop_warnings))
2093 PL_compiling.cop_warnings = newSVsv(sv) ;
2095 sv_setsv(PL_compiling.cop_warnings, sv);
2096 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2097 PL_dowarn |= G_WARN_ONCE ;
2105 if (PL_localizing) {
2106 if (PL_localizing == 1)
2107 SAVESPTR(PL_last_in_gv);
2109 else if (SvOK(sv) && GvIO(PL_last_in_gv))
2110 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
2113 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2114 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
2115 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
2118 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2119 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
2120 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
2123 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2126 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2127 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2128 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
2131 IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2135 IO *io = GvIOp(PL_defoutgv);
2138 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
2139 IoFLAGS(io) &= ~IOf_FLUSH;
2141 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2142 PerlIO *ofp = IoOFP(io);
2144 (void)PerlIO_flush(ofp);
2145 IoFLAGS(io) |= IOf_FLUSH;
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);
2376 /* PL_origalen is set in perl_parse(). */
2377 s = SvPV_force(sv,len);
2378 if (len >= (STRLEN)PL_origalen) {
2379 /* Longer than original, will be truncated. */
2380 Copy(s, PL_origargv[0], PL_origalen, char);
2381 PL_origargv[0][PL_origalen - 1] = 0;
2384 /* Shorter than original, will be padded. */
2385 Copy(s, PL_origargv[0], len, char);
2386 PL_origargv[0][len] = 0;
2387 memset(PL_origargv[0] + len + 1,
2388 /* Is the space counterintuitive? Yes.
2389 * (You were expecting \0?)
2390 * Does it work? Seems to. (In Linux 2.4.20 at least.)
2393 PL_origalen - len - 1);
2394 for (i = 1; i < PL_origargc; i++)
2397 UNLOCK_DOLLARZERO_MUTEX;
2405 Perl_whichsig(pTHX_ char *sig)
2407 register char **sigv;
2409 for (sigv = PL_sig_name; *sigv; sigv++)
2410 if (strEQ(sig,*sigv))
2411 return PL_sig_num[sigv - PL_sig_name];
2413 if (strEQ(sig,"CHLD"))
2417 if (strEQ(sig,"CLD"))
2423 #if !defined(PERL_IMPLICIT_CONTEXT)
2428 Perl_sighandler(int sig)
2430 #ifdef PERL_GET_SIG_CONTEXT
2431 dTHXa(PERL_GET_SIG_CONTEXT);
2438 SV *sv = Nullsv, *tSv = PL_Sv;
2444 if (PL_savestack_ix + 15 <= PL_savestack_max)
2446 if (PL_markstack_ptr < PL_markstack_max - 2)
2448 if (PL_retstack_ix < PL_retstack_max - 2)
2450 if (PL_scopestack_ix < PL_scopestack_max - 3)
2453 if (!PL_psig_ptr[sig]) {
2454 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
2459 /* Max number of items pushed there is 3*n or 4. We cannot fix
2460 infinity, so we fix 4 (in fact 5): */
2462 PL_savestack_ix += 5; /* Protect save in progress. */
2463 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2466 PL_markstack_ptr++; /* Protect mark. */
2469 PL_retstack[PL_retstack_ix] = NULL;
2472 PL_scopestack_ix += 1;
2473 /* sv_2cv is too complicated, try a simpler variant first: */
2474 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2475 || SvTYPE(cv) != SVt_PVCV)
2476 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2478 if (!cv || !CvROOT(cv)) {
2479 if (ckWARN(WARN_SIGNAL))
2480 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
2481 PL_sig_name[sig], (gv ? GvENAME(gv)
2488 if(PL_psig_name[sig]) {
2489 sv = SvREFCNT_inc(PL_psig_name[sig]);
2491 #if !defined(PERL_IMPLICIT_CONTEXT)
2495 sv = sv_newmortal();
2496 sv_setpv(sv,PL_sig_name[sig]);
2499 PUSHSTACKi(PERLSI_SIGNAL);
2504 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2507 if (SvTRUE(ERRSV)) {
2509 #ifdef HAS_SIGPROCMASK
2510 /* Handler "died", for example to get out of a restart-able read().
2511 * Before we re-do that on its behalf re-enable the signal which was
2512 * blocked by the system when we entered.
2516 sigaddset(&set,sig);
2517 sigprocmask(SIG_UNBLOCK, &set, NULL);
2519 /* Not clear if this will work */
2520 (void)rsignal(sig, SIG_IGN);
2521 (void)rsignal(sig, &Perl_csighandler);
2523 #endif /* !PERL_MICRO */
2524 Perl_die(aTHX_ Nullformat);
2528 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2534 PL_scopestack_ix -= 1;
2537 PL_op = myop; /* Apparently not needed... */
2539 PL_Sv = tSv; /* Restore global temporaries. */
2546 restore_magic(pTHX_ void *p)
2548 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2549 SV* sv = mgs->mgs_sv;
2554 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2557 SvFLAGS(sv) |= mgs->mgs_flags;
2561 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2564 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2566 /* If we're still on top of the stack, pop us off. (That condition
2567 * will be satisfied if restore_magic was called explicitly, but *not*
2568 * if it's being called via leave_scope.)
2569 * The reason for doing this is that otherwise, things like sv_2cv()
2570 * may leave alloc gunk on the savestack, and some code
2571 * (e.g. sighandler) doesn't expect that...
2573 if (PL_savestack_ix == mgs->mgs_ss_ix)
2575 I32 popval = SSPOPINT;
2576 assert(popval == SAVEt_DESTRUCTOR_X);
2577 PL_savestack_ix -= 2;
2579 assert(popval == SAVEt_ALLOC);
2581 PL_savestack_ix -= popval;
2587 unwind_handler_stack(pTHX_ void *p)
2589 U32 flags = *(U32*)p;
2592 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2593 /* cxstack_ix-- Not needed, die already unwound it. */
2594 #if !defined(PERL_IMPLICIT_CONTEXT)
2596 SvREFCNT_dec(sig_sv);