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 /* Missing protos on LynxOS */
53 void setruid(uid_t id);
54 void seteuid(uid_t id);
55 void setrgid(uid_t id);
56 void setegid(uid_t id);
60 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
68 /* MGS is typedef'ed to struct magic_state in perl.h */
71 S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
74 assert(SvMAGICAL(sv));
75 #ifdef PERL_COPY_ON_WRITE
76 /* Turning READONLY off for a copy-on-write scalar is a bad idea. */
81 SAVEDESTRUCTOR_X(restore_magic, INT2PTR(void*, (IV)mgs_ix));
83 mgs = SSPTR(mgs_ix, MGS*);
85 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
86 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
90 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
94 =for apidoc mg_magical
96 Turns on the magical status of an SV. See C<sv_magic>.
102 Perl_mg_magical(pTHX_ SV *sv)
105 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
106 MGVTBL* vtbl = mg->mg_virtual;
108 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
112 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
121 Do magic after a value is retrieved from the SV. See C<sv_magic>.
127 Perl_mg_get(pTHX_ SV *sv)
130 MAGIC *newmg, *head, *cur, *mg;
131 I32 mgs_ix = SSNEW(sizeof(MGS));
133 save_magic(mgs_ix, sv);
135 /* We must call svt_get(sv, mg) for each valid entry in the linked
136 list of magic. svt_get() may delete the current entry, add new
137 magic to the head of the list, or upgrade the SV. AMS 20010810 */
139 newmg = cur = head = mg = SvMAGIC(sv);
141 MGVTBL *vtbl = mg->mg_virtual;
143 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
144 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
146 /* guard against sv having been freed */
147 if (SvTYPE(sv) == SVTYPEMASK) {
148 Perl_croak(aTHX_ "Tied variable freed while still in use");
150 /* guard against magic having been deleted - eg FETCH calling
155 /* Don't restore the flags for this entry if it was deleted. */
156 if (mg->mg_flags & MGf_GSKIP)
157 (SSPTR(mgs_ix, MGS *))->mgs_flags = 0;
160 mg = mg->mg_moremagic;
163 /* Have we finished with the new entries we saw? Start again
164 where we left off (unless there are more new entries). */
172 /* Were any new entries added? */
173 if (!new && (newmg = SvMAGIC(sv)) != head) {
180 restore_magic(aTHX_ INT2PTR(void *, (IV)mgs_ix));
187 Do magic after a value is assigned to the SV. See C<sv_magic>.
193 Perl_mg_set(pTHX_ SV *sv)
199 mgs_ix = SSNEW(sizeof(MGS));
200 save_magic(mgs_ix, sv);
202 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
203 MGVTBL* vtbl = mg->mg_virtual;
204 nextmg = mg->mg_moremagic; /* it may delete itself */
205 if (mg->mg_flags & MGf_GSKIP) {
206 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
207 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
209 if (vtbl && vtbl->svt_set)
210 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
213 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
218 =for apidoc mg_length
220 Report on the SV's length. See C<sv_magic>.
226 Perl_mg_length(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 U8 *s = (U8*)SvPV(sv, len);
248 len = Perl_utf8_length(aTHX_ s, s + len);
256 Perl_mg_size(pTHX_ SV *sv)
261 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
262 MGVTBL* vtbl = mg->mg_virtual;
263 if (vtbl && vtbl->svt_len) {
266 mgs_ix = SSNEW(sizeof(MGS));
267 save_magic(mgs_ix, sv);
268 /* omit MGf_GSKIP -- not changed here */
269 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
270 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
277 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
282 Perl_croak(aTHX_ "Size magic not implemented");
291 Clear something magical that the SV represents. See C<sv_magic>.
297 Perl_mg_clear(pTHX_ SV *sv)
302 mgs_ix = SSNEW(sizeof(MGS));
303 save_magic(mgs_ix, sv);
305 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
306 MGVTBL* vtbl = mg->mg_virtual;
307 /* omit GSKIP -- never set here */
309 if (vtbl && vtbl->svt_clear)
310 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
313 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
320 Finds the magic pointer for type matching the SV. See C<sv_magic>.
326 Perl_mg_find(pTHX_ SV *sv, int type)
331 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
332 if (mg->mg_type == type)
341 Copies the magic from one SV to another. See C<sv_magic>.
347 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
351 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
352 MGVTBL* vtbl = mg->mg_virtual;
353 if ((mg->mg_flags & MGf_COPY) && vtbl->svt_copy){
354 count += CALL_FPTR(vtbl->svt_copy)(aTHX_ sv, mg, nsv, key, klen);
356 else if (isUPPER(mg->mg_type)) {
358 mg->mg_type == PERL_MAGIC_tied ? SvTIED_obj(sv, mg) :
359 (mg->mg_type == PERL_MAGIC_regdata && mg->mg_obj)
361 toLOWER(mg->mg_type), key, klen);
371 Free any magic storage used by the SV. See C<sv_magic>.
377 Perl_mg_free(pTHX_ SV *sv)
381 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
382 MGVTBL* vtbl = mg->mg_virtual;
383 moremagic = mg->mg_moremagic;
384 if (vtbl && vtbl->svt_free)
385 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
386 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
387 if (mg->mg_len > 0 || mg->mg_type == PERL_MAGIC_utf8)
388 Safefree(mg->mg_ptr);
389 else if (mg->mg_len == HEf_SVKEY)
390 SvREFCNT_dec((SV*)mg->mg_ptr);
392 if (mg->mg_flags & MGf_REFCOUNTED)
393 SvREFCNT_dec(mg->mg_obj);
403 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
407 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
408 if (mg->mg_obj) /* @+ */
411 return rx->lastparen;
418 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
426 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
430 if (paren <= (I32)rx->nparens &&
431 (s = rx->startp[paren]) != -1 &&
432 (t = rx->endp[paren]) != -1)
434 if (mg->mg_obj) /* @+ */
439 if (i > 0 && RX_MATCH_UTF8(rx)) {
440 char *b = rx->subbeg;
442 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
452 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
454 Perl_croak(aTHX_ PL_no_modify);
460 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
467 switch (*mg->mg_ptr) {
468 case '1': case '2': case '3': case '4':
469 case '5': case '6': case '7': case '8': case '9': case '&':
470 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
472 paren = atoi(mg->mg_ptr); /* $& is in [0] */
474 if (paren <= (I32)rx->nparens &&
475 (s1 = rx->startp[paren]) != -1 &&
476 (t1 = rx->endp[paren]) != -1)
480 if (i > 0 && RX_MATCH_UTF8(rx)) {
481 char *s = rx->subbeg + s1;
482 char *send = rx->subbeg + t1;
485 if (is_utf8_string((U8*)s, i))
486 i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send);
489 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
493 if (ckWARN(WARN_UNINITIALIZED))
498 if (ckWARN(WARN_UNINITIALIZED))
503 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
504 paren = rx->lastparen;
509 case '\016': /* ^N */
510 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
511 paren = rx->lastcloseparen;
517 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
518 if (rx->startp[0] != -1) {
529 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
530 if (rx->endp[0] != -1) {
531 i = rx->sublen - rx->endp[0];
542 if (!SvPOK(sv) && SvNIOK(sv)) {
552 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
555 register char *s = NULL;
559 switch (*mg->mg_ptr) {
560 case '\001': /* ^A */
561 sv_setsv(sv, PL_bodytarget);
563 case '\003': /* ^C */
564 sv_setiv(sv, (IV)PL_minus_c);
567 case '\004': /* ^D */
568 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
569 #if defined(YYDEBUG) && defined(DEBUGGING)
570 PL_yydebug = DEBUG_p_TEST;
573 case '\005': /* ^E */
574 if (*(mg->mg_ptr+1) == '\0') {
575 #ifdef MACOS_TRADITIONAL
579 sv_setnv(sv,(double)gMacPerl_OSErr);
580 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
585 # include <descrip.h>
586 # include <starlet.h>
588 $DESCRIPTOR(msgdsc,msg);
589 sv_setnv(sv,(NV) vaxc$errno);
590 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
591 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
597 if (!(_emx_env & 0x200)) { /* Under DOS */
598 sv_setnv(sv, (NV)errno);
599 sv_setpv(sv, errno ? Strerror(errno) : "");
601 if (errno != errno_isOS2) {
602 int tmp = _syserrno();
603 if (tmp) /* 2nd call to _syserrno() makes it 0 */
606 sv_setnv(sv, (NV)Perl_rc);
607 sv_setpv(sv, os2error(Perl_rc));
612 DWORD dwErr = GetLastError();
613 sv_setnv(sv, (NV)dwErr);
616 PerlProc_GetOSError(sv, dwErr);
624 int saveerrno = errno;
625 sv_setnv(sv, (NV)errno);
626 sv_setpv(sv, errno ? Strerror(errno) : "");
633 SvNOK_on(sv); /* what a wonderful hack! */
635 else if (strEQ(mg->mg_ptr+1, "NCODING"))
636 sv_setsv(sv, PL_encoding);
638 case '\006': /* ^F */
639 sv_setiv(sv, (IV)PL_maxsysfd);
641 case '\010': /* ^H */
642 sv_setiv(sv, (IV)PL_hints);
644 case '\011': /* ^I */ /* NOT \t in EBCDIC */
646 sv_setpv(sv, PL_inplace);
648 sv_setsv(sv, &PL_sv_undef);
650 case '\017': /* ^O & ^OPEN */
651 if (*(mg->mg_ptr+1) == '\0') {
652 sv_setpv(sv, PL_osname);
655 else if (strEQ(mg->mg_ptr, "\017PEN")) {
656 if (!PL_compiling.cop_io)
657 sv_setsv(sv, &PL_sv_undef);
659 sv_setsv(sv, PL_compiling.cop_io);
663 case '\020': /* ^P */
664 sv_setiv(sv, (IV)PL_perldb);
666 case '\023': /* ^S */
667 if (*(mg->mg_ptr+1) == '\0') {
668 if (PL_lex_state != LEX_NOTPARSING)
671 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
676 case '\024': /* ^T */
677 if (*(mg->mg_ptr+1) == '\0') {
679 sv_setnv(sv, PL_basetime);
681 sv_setiv(sv, (IV)PL_basetime);
684 else if (strEQ(mg->mg_ptr, "\024AINT"))
685 sv_setiv(sv, PL_tainting
686 ? (PL_taint_warn || PL_unsafe ? -1 : 1)
689 case '\025': /* $^UNICODE */
690 if (strEQ(mg->mg_ptr, "\025NICODE"))
691 sv_setuv(sv, (UV) PL_unicode);
693 case '\027': /* ^W & $^WARNING_BITS */
694 if (*(mg->mg_ptr+1) == '\0')
695 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
696 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
697 if (PL_compiling.cop_warnings == pWARN_NONE ||
698 PL_compiling.cop_warnings == pWARN_STD)
700 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
702 else if (PL_compiling.cop_warnings == pWARN_ALL) {
703 /* Get the bit mask for $warnings::Bits{all}, because
704 * it could have been extended by warnings::register */
706 HV *bits=get_hv("warnings::Bits", FALSE);
707 if (bits && (bits_all=hv_fetch(bits, "all", 3, FALSE))) {
708 sv_setsv(sv, *bits_all);
711 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
715 sv_setsv(sv, PL_compiling.cop_warnings);
720 case '1': case '2': case '3': case '4':
721 case '5': case '6': case '7': case '8': case '9': case '&':
722 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
726 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
727 * XXX Does the new way break anything?
729 paren = atoi(mg->mg_ptr); /* $& is in [0] */
731 if (paren <= (I32)rx->nparens &&
732 (s1 = rx->startp[paren]) != -1 &&
733 (t1 = rx->endp[paren]) != -1)
743 if (RX_MATCH_UTF8(rx) && is_utf8_string((U8*)s, i))
748 if (RX_MATCH_TAINTED(rx)) {
749 MAGIC* mg = SvMAGIC(sv);
752 SvMAGIC(sv) = mg->mg_moremagic;
754 if ((mgt = SvMAGIC(sv))) {
755 mg->mg_moremagic = mgt;
765 sv_setsv(sv,&PL_sv_undef);
768 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
769 paren = rx->lastparen;
773 sv_setsv(sv,&PL_sv_undef);
775 case '\016': /* ^N */
776 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
777 paren = rx->lastcloseparen;
781 sv_setsv(sv,&PL_sv_undef);
784 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
785 if ((s = rx->subbeg) && rx->startp[0] != -1) {
790 sv_setsv(sv,&PL_sv_undef);
793 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
794 if (rx->subbeg && rx->endp[0] != -1) {
795 s = rx->subbeg + rx->endp[0];
796 i = rx->sublen - rx->endp[0];
800 sv_setsv(sv,&PL_sv_undef);
804 if (GvIO(PL_last_in_gv)) {
805 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
811 sv_setiv(sv, (IV)STATUS_CURRENT);
812 #ifdef COMPLEX_STATUS
813 LvTARGOFF(sv) = PL_statusvalue;
814 LvTARGLEN(sv) = PL_statusvalue_vms;
819 if (GvIOp(PL_defoutgv))
820 s = IoTOP_NAME(GvIOp(PL_defoutgv));
824 sv_setpv(sv,GvENAME(PL_defoutgv));
829 if (GvIOp(PL_defoutgv))
830 s = IoFMT_NAME(GvIOp(PL_defoutgv));
832 s = GvENAME(PL_defoutgv);
837 if (GvIOp(PL_defoutgv))
838 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
841 if (GvIOp(PL_defoutgv))
842 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
845 if (GvIOp(PL_defoutgv))
846 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
854 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
857 if (GvIOp(PL_defoutgv))
858 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
864 sv_copypv(sv, PL_ors_sv);
867 sv_setpv(sv,PL_ofmt);
871 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
872 sv_setpv(sv, errno ? Strerror(errno) : "");
875 int saveerrno = errno;
876 sv_setnv(sv, (NV)errno);
878 if (errno == errno_isOS2 || errno == errno_isOS2_set)
879 sv_setpv(sv, os2error(Perl_rc));
882 sv_setpv(sv, errno ? Strerror(errno) : "");
886 SvNOK_on(sv); /* what a wonderful hack! */
889 sv_setiv(sv, (IV)PL_uid);
892 sv_setiv(sv, (IV)PL_euid);
895 sv_setiv(sv, (IV)PL_gid);
897 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
901 sv_setiv(sv, (IV)PL_egid);
903 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
908 Groups_t gary[NGROUPS];
909 i = getgroups(NGROUPS,gary);
911 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
914 (void)SvIOK_on(sv); /* what a wonderful hack! */
916 #ifndef MACOS_TRADITIONAL
925 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
927 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
929 if (uf && uf->uf_val)
930 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
935 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
945 #ifdef DYNAMIC_ENV_FETCH
946 /* We just undefd an environment var. Is a replacement */
947 /* waiting in the wings? */
950 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
951 s = SvPV(*valp, len);
955 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
956 /* And you'll never guess what the dog had */
957 /* in its mouth... */
959 MgTAINTEDDIR_off(mg);
961 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
962 char pathbuf[256], eltbuf[256], *cp, *elt = s;
966 do { /* DCL$PATH may be a search list */
967 while (1) { /* as may dev portion of any element */
968 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
969 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
970 cando_by_name(S_IWUSR,0,elt) ) {
975 if ((cp = strchr(elt, ':')) != Nullch)
977 if (my_trnlnm(elt, eltbuf, j++))
983 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
986 if (s && klen == 4 && strEQ(ptr,"PATH")) {
987 char *strend = s + len;
993 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
996 if (i >= sizeof tmpbuf /* too long -- assume the worst */
998 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
1005 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
1011 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
1014 my_setenv(MgPV(mg,n_a),Nullch);
1019 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
1022 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1024 if (PL_localizing) {
1027 magic_clear_all_env(sv,mg);
1028 hv_iterinit((HV*)sv);
1029 while ((entry = hv_iternext((HV*)sv))) {
1031 my_setenv(hv_iterkey(entry, &keylen),
1032 SvPV(hv_iterval((HV*)sv, entry), n_a));
1040 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
1043 #if defined(VMS) || defined(EPOC)
1044 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1046 # if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
1049 # ifdef USE_ENVIRON_ARRAY
1050 # if defined(USE_ITHREADS)
1051 /* only the parent thread can clobber the process environment */
1052 if (PL_curinterp == aTHX)
1055 # ifndef PERL_USE_SAFE_PUTENV
1058 if (environ == PL_origenviron)
1059 environ = (char**)safesysmalloc(sizeof(char*));
1061 for (i = 0; environ[i]; i++)
1062 safesysfree(environ[i]);
1063 # endif /* PERL_USE_SAFE_PUTENV */
1065 environ[0] = Nullch;
1067 # endif /* USE_ENVIRON_ARRAY */
1068 # endif /* PERL_IMPLICIT_SYS || WIN32 */
1069 #endif /* VMS || EPOC */
1070 #endif /* !PERL_MICRO */
1074 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS)||defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1075 static int sig_handlers_initted = 0;
1077 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1078 static int sig_ignoring[SIG_SIZE]; /* which signals we are ignoring */
1080 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1081 static int sig_defaulting[SIG_SIZE];
1085 #ifdef HAS_SIGPROCMASK
1087 restore_sigmask(pTHX_ SV *save_sv)
1089 sigset_t *ossetp = (sigset_t *) SvPV_nolen( save_sv );
1090 (void)sigprocmask(SIG_SETMASK, ossetp, (sigset_t *)0);
1094 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
1098 /* Are we fetching a signal entry? */
1099 i = whichsig(MgPV(mg,n_a));
1102 sv_setsv(sv,PL_psig_ptr[i]);
1104 Sighandler_t sigstate;
1105 sigstate = rsignal_state(i);
1106 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1107 if (sig_handlers_initted && sig_ignoring[i]) sigstate = SIG_IGN;
1109 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1110 if (sig_handlers_initted && sig_defaulting[i]) sigstate = SIG_DFL;
1112 /* cache state so we don't fetch it again */
1113 if(sigstate == SIG_IGN)
1114 sv_setpv(sv,"IGNORE");
1116 sv_setsv(sv,&PL_sv_undef);
1117 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1124 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
1126 /* XXX Some of this code was copied from Perl_magic_setsig. A little
1127 * refactoring might be in order.
1135 if (strEQ(s,"__DIE__"))
1137 else if (strEQ(s,"__WARN__"))
1140 Perl_croak(aTHX_ "No such hook: %s", s);
1144 SvREFCNT_dec(to_dec);
1149 /* Are we clearing a signal entry? */
1152 #ifdef HAS_SIGPROCMASK
1155 /* Avoid having the signal arrive at a bad time, if possible. */
1158 sigprocmask(SIG_BLOCK, &set, &save);
1160 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1161 SAVEFREESV(save_sv);
1162 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1165 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1166 if (!sig_handlers_initted) Perl_csighandler_init();
1168 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1169 sig_defaulting[i] = 1;
1170 (void)rsignal(i, PL_csighandlerp);
1172 (void)rsignal(i, SIG_DFL);
1174 if(PL_psig_name[i]) {
1175 SvREFCNT_dec(PL_psig_name[i]);
1178 if(PL_psig_ptr[i]) {
1179 to_dec=PL_psig_ptr[i];
1182 SvREFCNT_dec(to_dec);
1192 Perl_raise_signal(pTHX_ int sig)
1194 /* Set a flag to say this signal is pending */
1195 PL_psig_pend[sig]++;
1196 /* And one to say _a_ signal is pending */
1201 Perl_csighandler(int sig)
1203 #ifdef PERL_GET_SIG_CONTEXT
1204 dTHXa(PERL_GET_SIG_CONTEXT);
1208 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1209 (void) rsignal(sig, PL_csighandlerp);
1210 if (sig_ignoring[sig]) return;
1212 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1213 if (sig_defaulting[sig])
1214 #ifdef KILL_BY_SIGPRC
1215 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1220 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
1221 /* Call the perl level handler now--
1222 * with risk we may be in malloc() etc. */
1223 (*PL_sighandlerp)(sig);
1225 Perl_raise_signal(aTHX_ sig);
1228 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1230 Perl_csighandler_init(void)
1233 if (sig_handlers_initted) return;
1235 for (sig = 1; sig < SIG_SIZE; sig++) {
1236 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1238 sig_defaulting[sig] = 1;
1239 (void) rsignal(sig, PL_csighandlerp);
1241 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1242 sig_ignoring[sig] = 0;
1245 sig_handlers_initted = 1;
1250 Perl_despatch_signals(pTHX)
1254 for (sig = 1; sig < SIG_SIZE; sig++) {
1255 if (PL_psig_pend[sig]) {
1256 PERL_BLOCKSIG_ADD(set, sig);
1257 PL_psig_pend[sig] = 0;
1258 PERL_BLOCKSIG_BLOCK(set);
1259 (*PL_sighandlerp)(sig);
1260 PERL_BLOCKSIG_UNBLOCK(set);
1266 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1271 /* Need to be careful with SvREFCNT_dec(), because that can have side
1272 * effects (due to closures). We must make sure that the new disposition
1273 * is in place before it is called.
1277 #ifdef HAS_SIGPROCMASK
1284 if (strEQ(s,"__DIE__"))
1286 else if (strEQ(s,"__WARN__"))
1289 Perl_croak(aTHX_ "No such hook: %s", s);
1297 i = whichsig(s); /* ...no, a brick */
1299 if (ckWARN(WARN_SIGNAL))
1300 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
1303 #ifdef HAS_SIGPROCMASK
1304 /* Avoid having the signal arrive at a bad time, if possible. */
1307 sigprocmask(SIG_BLOCK, &set, &save);
1309 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1310 SAVEFREESV(save_sv);
1311 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1314 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1315 if (!sig_handlers_initted) Perl_csighandler_init();
1317 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1318 sig_ignoring[i] = 0;
1320 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1321 sig_defaulting[i] = 0;
1323 SvREFCNT_dec(PL_psig_name[i]);
1324 to_dec = PL_psig_ptr[i];
1325 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1326 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1327 PL_psig_name[i] = newSVpvn(s, len);
1328 SvREADONLY_on(PL_psig_name[i]);
1330 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1332 (void)rsignal(i, PL_csighandlerp);
1333 #ifdef HAS_SIGPROCMASK
1338 *svp = SvREFCNT_inc(sv);
1340 SvREFCNT_dec(to_dec);
1343 s = SvPV_force(sv,len);
1344 if (strEQ(s,"IGNORE")) {
1346 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1347 sig_ignoring[i] = 1;
1348 (void)rsignal(i, PL_csighandlerp);
1350 (void)rsignal(i, SIG_IGN);
1354 else if (strEQ(s,"DEFAULT") || !*s) {
1356 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1358 sig_defaulting[i] = 1;
1359 (void)rsignal(i, PL_csighandlerp);
1362 (void)rsignal(i, SIG_DFL);
1367 * We should warn if HINT_STRICT_REFS, but without
1368 * access to a known hint bit in a known OP, we can't
1369 * tell whether HINT_STRICT_REFS is in force or not.
1371 if (!strchr(s,':') && !strchr(s,'\''))
1372 sv_insert(sv, 0, 0, "main::", 6);
1374 (void)rsignal(i, PL_csighandlerp);
1376 *svp = SvREFCNT_inc(sv);
1378 #ifdef HAS_SIGPROCMASK
1383 SvREFCNT_dec(to_dec);
1386 #endif /* !PERL_MICRO */
1389 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1391 PL_sub_generation++;
1396 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1398 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1399 PL_amagic_generation++;
1405 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1407 HV *hv = (HV*)LvTARG(sv);
1411 (void) hv_iterinit(hv);
1412 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1415 while (hv_iternext(hv))
1420 sv_setiv(sv, (IV)i);
1425 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1428 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1433 /* caller is responsible for stack switching/cleanup */
1435 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1441 PUSHs(SvTIED_obj(sv, mg));
1444 if (mg->mg_len >= 0)
1445 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1446 else if (mg->mg_len == HEf_SVKEY)
1447 PUSHs((SV*)mg->mg_ptr);
1449 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1450 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1458 return call_method(meth, flags);
1462 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1468 PUSHSTACKi(PERLSI_MAGIC);
1470 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1471 sv_setsv(sv, *PL_stack_sp--);
1481 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1484 mg->mg_flags |= MGf_GSKIP;
1485 magic_methpack(sv,mg,"FETCH");
1490 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1494 PUSHSTACKi(PERLSI_MAGIC);
1495 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1502 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1504 return magic_methpack(sv,mg,"DELETE");
1509 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1516 PUSHSTACKi(PERLSI_MAGIC);
1517 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1518 sv = *PL_stack_sp--;
1519 retval = (U32) SvIV(sv)-1;
1528 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1533 PUSHSTACKi(PERLSI_MAGIC);
1535 XPUSHs(SvTIED_obj(sv, mg));
1537 call_method("CLEAR", G_SCALAR|G_DISCARD);
1545 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1548 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1552 PUSHSTACKi(PERLSI_MAGIC);
1555 PUSHs(SvTIED_obj(sv, mg));
1560 if (call_method(meth, G_SCALAR))
1561 sv_setsv(key, *PL_stack_sp--);
1570 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1572 return magic_methpack(sv,mg,"EXISTS");
1576 Perl_magic_scalarpack(pTHX_ HV *hv, MAGIC *mg)
1579 SV *retval = &PL_sv_undef;
1580 SV *tied = SvTIED_obj((SV*)hv, mg);
1581 HV *pkg = SvSTASH((SV*)SvRV(tied));
1583 if (!gv_fetchmethod_autoload(pkg, "SCALAR", FALSE)) {
1586 /* we are in an iteration so the hash cannot be empty */
1588 /* no xhv_eiter so now use FIRSTKEY */
1589 key = sv_newmortal();
1590 magic_nextpack((SV*)hv, mg, key);
1591 HvEITER(hv) = NULL; /* need to reset iterator */
1592 return SvOK(key) ? &PL_sv_yes : &PL_sv_no;
1595 /* there is a SCALAR method that we can call */
1597 PUSHSTACKi(PERLSI_MAGIC);
1603 if (call_method("SCALAR", G_SCALAR))
1604 retval = *PL_stack_sp--;
1611 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1621 svp = av_fetch(GvAV(gv),
1622 atoi(MgPV(mg,n_a)), FALSE);
1623 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp)))) {
1624 /* set or clear breakpoint in the relevant control op */
1626 o->op_flags |= OPf_SPECIAL;
1628 o->op_flags &= ~OPf_SPECIAL;
1634 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1636 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1641 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1643 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1648 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1650 SV* lsv = LvTARG(sv);
1652 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1653 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1654 if (mg && mg->mg_len >= 0) {
1657 sv_pos_b2u(lsv, &i);
1658 sv_setiv(sv, i + PL_curcop->cop_arybase);
1667 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1669 SV* lsv = LvTARG(sv);
1676 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1677 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1681 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1682 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1684 else if (!SvOK(sv)) {
1688 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1690 pos = SvIV(sv) - PL_curcop->cop_arybase;
1693 ulen = sv_len_utf8(lsv);
1703 else if (pos > (SSize_t)len)
1708 sv_pos_u2b(lsv, &p, 0);
1713 mg->mg_flags &= ~MGf_MINMATCH;
1719 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1721 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1723 gv_efullname3(sv,((GV*)sv), "*");
1727 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1732 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1741 if (*s == '*' && s[1])
1743 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1748 GvGP(sv) = gp_ref(GvGP(gv));
1753 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1756 SV *lsv = LvTARG(sv);
1757 char *tmps = SvPV(lsv,len);
1758 I32 offs = LvTARGOFF(sv);
1759 I32 rem = LvTARGLEN(sv);
1762 sv_pos_u2b(lsv, &offs, &rem);
1763 if (offs > (I32)len)
1765 if (rem + offs > (I32)len)
1767 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1774 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1777 char *tmps = SvPV(sv, len);
1778 SV *lsv = LvTARG(sv);
1779 I32 lvoff = LvTARGOFF(sv);
1780 I32 lvlen = LvTARGLEN(sv);
1783 sv_utf8_upgrade(lsv);
1784 sv_pos_u2b(lsv, &lvoff, &lvlen);
1785 sv_insert(lsv, lvoff, lvlen, tmps, len);
1788 else if (lsv && SvUTF8(lsv)) {
1789 sv_pos_u2b(lsv, &lvoff, &lvlen);
1790 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
1791 sv_insert(lsv, lvoff, lvlen, tmps, len);
1795 sv_insert(lsv, lvoff, lvlen, tmps, len);
1801 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1803 TAINT_IF((mg->mg_len & 1) ||
1804 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1809 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1811 if (PL_localizing) {
1812 if (PL_localizing == 1)
1817 else if (PL_tainted)
1825 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1827 SV *lsv = LvTARG(sv);
1834 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1839 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1841 do_vecset(sv); /* XXX slurp this routine */
1846 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1849 if (LvTARGLEN(sv)) {
1851 SV *ahv = LvTARG(sv);
1852 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1857 AV* av = (AV*)LvTARG(sv);
1858 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1859 targ = AvARRAY(av)[LvTARGOFF(sv)];
1861 if (targ && targ != &PL_sv_undef) {
1862 /* somebody else defined it for us */
1863 SvREFCNT_dec(LvTARG(sv));
1864 LvTARG(sv) = SvREFCNT_inc(targ);
1866 SvREFCNT_dec(mg->mg_obj);
1867 mg->mg_obj = Nullsv;
1868 mg->mg_flags &= ~MGf_REFCOUNTED;
1873 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1878 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1883 sv_setsv(LvTARG(sv), sv);
1884 SvSETMAGIC(LvTARG(sv));
1890 Perl_vivify_defelem(pTHX_ SV *sv)
1895 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
1898 SV *ahv = LvTARG(sv);
1900 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1903 if (!value || value == &PL_sv_undef)
1904 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1907 AV* av = (AV*)LvTARG(sv);
1908 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1909 LvTARG(sv) = Nullsv; /* array can't be extended */
1911 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1912 if (!svp || (value = *svp) == &PL_sv_undef)
1913 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1916 (void)SvREFCNT_inc(value);
1917 SvREFCNT_dec(LvTARG(sv));
1920 SvREFCNT_dec(mg->mg_obj);
1921 mg->mg_obj = Nullsv;
1922 mg->mg_flags &= ~MGf_REFCOUNTED;
1926 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1928 AV *av = (AV*)mg->mg_obj;
1929 SV **svp = AvARRAY(av);
1930 I32 i = AvFILLp(av);
1933 if (!SvWEAKREF(svp[i]))
1934 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1935 /* XXX Should we check that it hasn't changed? */
1937 (void)SvOK_off(svp[i]);
1938 SvWEAKREF_off(svp[i]);
1943 SvREFCNT_dec(av); /* remove extra count added by sv_add_backref() */
1948 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1956 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1958 sv_unmagic(sv, PERL_MAGIC_bm);
1964 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1966 sv_unmagic(sv, PERL_MAGIC_fm);
1972 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1974 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1976 if (uf && uf->uf_set)
1977 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
1982 Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
1984 sv_unmagic(sv, PERL_MAGIC_qr);
1989 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1991 regexp *re = (regexp *)mg->mg_obj;
1996 #ifdef USE_LOCALE_COLLATE
1998 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
2001 * RenE<eacute> Descartes said "I think not."
2002 * and vanished with a faint plop.
2005 Safefree(mg->mg_ptr);
2011 #endif /* USE_LOCALE_COLLATE */
2013 /* Just clear the UTF-8 cache data. */
2015 Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
2017 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
2019 mg->mg_len = -1; /* The mg_len holds the len cache. */
2024 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
2029 switch (*mg->mg_ptr) {
2030 case '\001': /* ^A */
2031 sv_setsv(PL_bodytarget, sv);
2033 case '\003': /* ^C */
2034 PL_minus_c = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2037 case '\004': /* ^D */
2040 PL_debug = get_debug_opts(&s) | DEBUG_TOP_FLAG;
2041 DEBUG_x(dump_all());
2043 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
2046 case '\005': /* ^E */
2047 if (*(mg->mg_ptr+1) == '\0') {
2048 #ifdef MACOS_TRADITIONAL
2049 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2052 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2055 SetLastError( SvIV(sv) );
2058 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2060 /* will anyone ever use this? */
2061 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
2067 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
2069 SvREFCNT_dec(PL_encoding);
2070 if (SvOK(sv) || SvGMAGICAL(sv)) {
2071 PL_encoding = newSVsv(sv);
2074 PL_encoding = Nullsv;
2078 case '\006': /* ^F */
2079 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2081 case '\010': /* ^H */
2082 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2084 case '\011': /* ^I */ /* NOT \t in EBCDIC */
2086 Safefree(PL_inplace);
2088 PL_inplace = savepv(SvPV(sv,len));
2090 PL_inplace = Nullch;
2092 case '\017': /* ^O */
2093 if (*(mg->mg_ptr+1) == '\0') {
2095 Safefree(PL_osname);
2097 TAINT_PROPER("assigning to $^O");
2098 PL_osname = savepv(SvPV(sv,len));
2103 else if (strEQ(mg->mg_ptr, "\017PEN")) {
2104 if (!PL_compiling.cop_io)
2105 PL_compiling.cop_io = newSVsv(sv);
2107 sv_setsv(PL_compiling.cop_io,sv);
2110 case '\020': /* ^P */
2111 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2112 if ((PERLDB_SUB || PERLDB_LINE || PERLDB_SUBLINE || PERLDB_ASSERTION)
2116 case '\024': /* ^T */
2118 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
2120 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2123 case '\027': /* ^W & $^WARNING_BITS */
2124 if (*(mg->mg_ptr+1) == '\0') {
2125 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2126 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2127 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
2128 | (i ? G_WARN_ON : G_WARN_OFF) ;
2131 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
2132 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2133 if (!SvPOK(sv) && PL_localizing) {
2134 sv_setpvn(sv, WARN_NONEstring, WARNsize);
2135 PL_compiling.cop_warnings = pWARN_NONE;
2140 int accumulate = 0 ;
2141 int any_fatals = 0 ;
2142 char * ptr = (char*)SvPV(sv, len) ;
2143 for (i = 0 ; i < len ; ++i) {
2144 accumulate |= ptr[i] ;
2145 any_fatals |= (ptr[i] & 0xAA) ;
2148 PL_compiling.cop_warnings = pWARN_NONE;
2149 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
2150 PL_compiling.cop_warnings = pWARN_ALL;
2151 PL_dowarn |= G_WARN_ONCE ;
2154 if (specialWARN(PL_compiling.cop_warnings))
2155 PL_compiling.cop_warnings = newSVsv(sv) ;
2157 sv_setsv(PL_compiling.cop_warnings, sv);
2158 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2159 PL_dowarn |= G_WARN_ONCE ;
2167 if (PL_localizing) {
2168 if (PL_localizing == 1)
2169 SAVESPTR(PL_last_in_gv);
2171 else if (SvOK(sv) && GvIO(PL_last_in_gv))
2172 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
2175 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2176 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
2177 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
2180 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2181 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
2182 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
2185 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2188 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2189 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2190 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
2193 IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2197 IO *io = GvIOp(PL_defoutgv);
2200 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
2201 IoFLAGS(io) &= ~IOf_FLUSH;
2203 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2204 PerlIO *ofp = IoOFP(io);
2206 (void)PerlIO_flush(ofp);
2207 IoFLAGS(io) |= IOf_FLUSH;
2213 SvREFCNT_dec(PL_rs);
2214 PL_rs = newSVsv(sv);
2218 SvREFCNT_dec(PL_ors_sv);
2219 if (SvOK(sv) || SvGMAGICAL(sv)) {
2220 PL_ors_sv = newSVsv(sv);
2228 SvREFCNT_dec(PL_ofs_sv);
2229 if (SvOK(sv) || SvGMAGICAL(sv)) {
2230 PL_ofs_sv = newSVsv(sv);
2239 PL_ofmt = savepv(SvPV(sv,len));
2242 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2245 #ifdef COMPLEX_STATUS
2246 if (PL_localizing == 2) {
2247 PL_statusvalue = LvTARGOFF(sv);
2248 PL_statusvalue_vms = LvTARGLEN(sv);
2252 #ifdef VMSISH_STATUS
2254 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
2257 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2262 # define PERL_VMS_BANG vaxc$errno
2264 # define PERL_VMS_BANG 0
2266 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
2267 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
2271 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2272 if (PL_delaymagic) {
2273 PL_delaymagic |= DM_RUID;
2274 break; /* don't do magic till later */
2277 (void)setruid((Uid_t)PL_uid);
2280 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
2282 #ifdef HAS_SETRESUID
2283 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
2285 if (PL_uid == PL_euid) { /* special case $< = $> */
2287 /* workaround for Darwin's setuid peculiarity, cf [perl #24122] */
2288 if (PL_uid != 0 && PerlProc_getuid() == 0)
2289 (void)PerlProc_setuid(0);
2291 (void)PerlProc_setuid(PL_uid);
2293 PL_uid = PerlProc_getuid();
2294 Perl_croak(aTHX_ "setruid() not implemented");
2299 PL_uid = PerlProc_getuid();
2300 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2303 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2304 if (PL_delaymagic) {
2305 PL_delaymagic |= DM_EUID;
2306 break; /* don't do magic till later */
2309 (void)seteuid((Uid_t)PL_euid);
2312 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
2314 #ifdef HAS_SETRESUID
2315 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
2317 if (PL_euid == PL_uid) /* special case $> = $< */
2318 PerlProc_setuid(PL_euid);
2320 PL_euid = PerlProc_geteuid();
2321 Perl_croak(aTHX_ "seteuid() not implemented");
2326 PL_euid = PerlProc_geteuid();
2327 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2330 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2331 if (PL_delaymagic) {
2332 PL_delaymagic |= DM_RGID;
2333 break; /* don't do magic till later */
2336 (void)setrgid((Gid_t)PL_gid);
2339 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
2341 #ifdef HAS_SETRESGID
2342 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2344 if (PL_gid == PL_egid) /* special case $( = $) */
2345 (void)PerlProc_setgid(PL_gid);
2347 PL_gid = PerlProc_getgid();
2348 Perl_croak(aTHX_ "setrgid() not implemented");
2353 PL_gid = PerlProc_getgid();
2354 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2357 #ifdef HAS_SETGROUPS
2359 char *p = SvPV(sv, len);
2360 Groups_t gary[NGROUPS];
2365 for (i = 0; i < NGROUPS; ++i) {
2366 while (*p && !isSPACE(*p))
2375 (void)setgroups(i, gary);
2377 #else /* HAS_SETGROUPS */
2378 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2379 #endif /* HAS_SETGROUPS */
2380 if (PL_delaymagic) {
2381 PL_delaymagic |= DM_EGID;
2382 break; /* don't do magic till later */
2385 (void)setegid((Gid_t)PL_egid);
2388 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2390 #ifdef HAS_SETRESGID
2391 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2393 if (PL_egid == PL_gid) /* special case $) = $( */
2394 (void)PerlProc_setgid(PL_egid);
2396 PL_egid = PerlProc_getegid();
2397 Perl_croak(aTHX_ "setegid() not implemented");
2402 PL_egid = PerlProc_getegid();
2403 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2406 PL_chopset = SvPV_force(sv,len);
2408 #ifndef MACOS_TRADITIONAL
2410 LOCK_DOLLARZERO_MUTEX;
2411 #ifdef HAS_SETPROCTITLE
2412 /* The BSDs don't show the argv[] in ps(1) output, they
2413 * show a string from the process struct and provide
2414 * the setproctitle() routine to manipulate that. */
2417 # if __FreeBSD_version > 410001
2418 /* The leading "-" removes the "perl: " prefix,
2419 * but not the "(perl) suffix from the ps(1)
2420 * output, because that's what ps(1) shows if the
2421 * argv[] is modified. */
2422 setproctitle("-%s", s);
2423 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2424 /* This doesn't really work if you assume that
2425 * $0 = 'foobar'; will wipe out 'perl' from the $0
2426 * because in ps(1) output the result will be like
2427 * sprintf("perl: %s (perl)", s)
2428 * I guess this is a security feature:
2429 * one (a user process) cannot get rid of the original name.
2431 setproctitle("%s", s);
2435 #if defined(__hpux) && defined(PSTAT_SETCMD)
2440 pstat(PSTAT_SETCMD, un, len, 0, 0);
2443 /* PL_origalen is set in perl_parse(). */
2444 s = SvPV_force(sv,len);
2445 if (len >= (STRLEN)PL_origalen-1) {
2446 /* Longer than original, will be truncated. We assume that
2447 * PL_origalen bytes are available. */
2448 Copy(s, PL_origargv[0], PL_origalen-1, char);
2451 /* Shorter than original, will be padded. */
2452 Copy(s, PL_origargv[0], len, char);
2453 PL_origargv[0][len] = 0;
2454 memset(PL_origargv[0] + len + 1,
2455 /* Is the space counterintuitive? Yes.
2456 * (You were expecting \0?)
2457 * Does it work? Seems to. (In Linux 2.4.20 at least.)
2460 PL_origalen - len - 1);
2462 PL_origargv[0][PL_origalen-1] = 0;
2463 for (i = 1; i < PL_origargc; i++)
2465 UNLOCK_DOLLARZERO_MUTEX;
2473 Perl_whichsig(pTHX_ char *sig)
2475 register char **sigv;
2477 for (sigv = PL_sig_name; *sigv; sigv++)
2478 if (strEQ(sig,*sigv))
2479 return PL_sig_num[sigv - PL_sig_name];
2481 if (strEQ(sig,"CHLD"))
2485 if (strEQ(sig,"CLD"))
2491 #if !defined(PERL_IMPLICIT_CONTEXT)
2496 Perl_sighandler(int sig)
2498 #ifdef PERL_GET_SIG_CONTEXT
2499 dTHXa(PERL_GET_SIG_CONTEXT);
2506 SV *sv = Nullsv, *tSv = PL_Sv;
2512 if (PL_savestack_ix + 15 <= PL_savestack_max)
2514 if (PL_markstack_ptr < PL_markstack_max - 2)
2516 if (PL_retstack_ix < PL_retstack_max - 2)
2518 if (PL_scopestack_ix < PL_scopestack_max - 3)
2521 if (!PL_psig_ptr[sig]) {
2522 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
2527 /* Max number of items pushed there is 3*n or 4. We cannot fix
2528 infinity, so we fix 4 (in fact 5): */
2530 PL_savestack_ix += 5; /* Protect save in progress. */
2531 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2534 PL_markstack_ptr++; /* Protect mark. */
2537 PL_retstack[PL_retstack_ix] = NULL;
2540 PL_scopestack_ix += 1;
2541 /* sv_2cv is too complicated, try a simpler variant first: */
2542 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2543 || SvTYPE(cv) != SVt_PVCV)
2544 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2546 if (!cv || !CvROOT(cv)) {
2547 if (ckWARN(WARN_SIGNAL))
2548 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
2549 PL_sig_name[sig], (gv ? GvENAME(gv)
2556 if(PL_psig_name[sig]) {
2557 sv = SvREFCNT_inc(PL_psig_name[sig]);
2559 #if !defined(PERL_IMPLICIT_CONTEXT)
2563 sv = sv_newmortal();
2564 sv_setpv(sv,PL_sig_name[sig]);
2567 PUSHSTACKi(PERLSI_SIGNAL);
2572 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2575 if (SvTRUE(ERRSV)) {
2577 #ifdef HAS_SIGPROCMASK
2578 /* Handler "died", for example to get out of a restart-able read().
2579 * Before we re-do that on its behalf re-enable the signal which was
2580 * blocked by the system when we entered.
2584 sigaddset(&set,sig);
2585 sigprocmask(SIG_UNBLOCK, &set, NULL);
2587 /* Not clear if this will work */
2588 (void)rsignal(sig, SIG_IGN);
2589 (void)rsignal(sig, PL_csighandlerp);
2591 #endif /* !PERL_MICRO */
2592 Perl_die(aTHX_ Nullformat);
2596 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2602 PL_scopestack_ix -= 1;
2605 PL_op = myop; /* Apparently not needed... */
2607 PL_Sv = tSv; /* Restore global temporaries. */
2614 restore_magic(pTHX_ void *p)
2616 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2617 SV* sv = mgs->mgs_sv;
2622 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2624 #ifdef PERL_COPY_ON_WRITE
2625 /* While magic was saved (and off) sv_setsv may well have seen
2626 this SV as a prime candidate for COW. */
2628 sv_force_normal(sv);
2632 SvFLAGS(sv) |= mgs->mgs_flags;
2636 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2639 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2641 /* If we're still on top of the stack, pop us off. (That condition
2642 * will be satisfied if restore_magic was called explicitly, but *not*
2643 * if it's being called via leave_scope.)
2644 * The reason for doing this is that otherwise, things like sv_2cv()
2645 * may leave alloc gunk on the savestack, and some code
2646 * (e.g. sighandler) doesn't expect that...
2648 if (PL_savestack_ix == mgs->mgs_ss_ix)
2650 I32 popval = SSPOPINT;
2651 assert(popval == SAVEt_DESTRUCTOR_X);
2652 PL_savestack_ix -= 2;
2654 assert(popval == SAVEt_ALLOC);
2656 PL_savestack_ix -= popval;
2662 unwind_handler_stack(pTHX_ void *p)
2664 U32 flags = *(U32*)p;
2667 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2668 /* cxstack_ix-- Not needed, die already unwound it. */
2669 #if !defined(PERL_IMPLICIT_CONTEXT)
2671 SvREFCNT_dec(sig_sv);