3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 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));
132 int was_temp = SvTEMP(sv);
133 /* guard against sv having being freed midway by holding a private
136 /* sv_2mortal has this side effect of turning on the TEMP flag, which can
137 cause the SV's buffer to get stolen (and maybe other stuff).
140 sv_2mortal(SvREFCNT_inc(sv));
145 save_magic(mgs_ix, sv);
147 /* We must call svt_get(sv, mg) for each valid entry in the linked
148 list of magic. svt_get() may delete the current entry, add new
149 magic to the head of the list, or upgrade the SV. AMS 20010810 */
151 newmg = cur = head = mg = SvMAGIC(sv);
153 MGVTBL *vtbl = mg->mg_virtual;
155 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
156 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
158 /* guard against magic having been deleted - eg FETCH calling
163 /* Don't restore the flags for this entry if it was deleted. */
164 if (mg->mg_flags & MGf_GSKIP)
165 (SSPTR(mgs_ix, MGS *))->mgs_flags = 0;
168 mg = mg->mg_moremagic;
171 /* Have we finished with the new entries we saw? Start again
172 where we left off (unless there are more new entries). */
180 /* Were any new entries added? */
181 if (!new && (newmg = SvMAGIC(sv)) != head) {
188 restore_magic(aTHX_ INT2PTR(void *, (IV)mgs_ix));
190 if (SvREFCNT(sv) == 1) {
191 /* We hold the last reference to this SV, which implies that the
192 SV was deleted as a side effect of the routines we called. */
201 Do magic after a value is assigned to the SV. See C<sv_magic>.
207 Perl_mg_set(pTHX_ SV *sv)
213 mgs_ix = SSNEW(sizeof(MGS));
214 save_magic(mgs_ix, sv);
216 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
217 MGVTBL* vtbl = mg->mg_virtual;
218 nextmg = mg->mg_moremagic; /* it may delete itself */
219 if (mg->mg_flags & MGf_GSKIP) {
220 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
221 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
223 if (vtbl && vtbl->svt_set)
224 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
227 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
232 =for apidoc mg_length
234 Report on the SV's length. See C<sv_magic>.
240 Perl_mg_length(pTHX_ SV *sv)
245 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
246 MGVTBL* vtbl = mg->mg_virtual;
247 if (vtbl && vtbl->svt_len) {
250 mgs_ix = SSNEW(sizeof(MGS));
251 save_magic(mgs_ix, sv);
252 /* omit MGf_GSKIP -- not changed here */
253 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
254 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
261 U8 *s = (U8*)SvPV(sv, len);
262 len = Perl_utf8_length(aTHX_ s, s + len);
270 Perl_mg_size(pTHX_ SV *sv)
275 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
276 MGVTBL* vtbl = mg->mg_virtual;
277 if (vtbl && vtbl->svt_len) {
280 mgs_ix = SSNEW(sizeof(MGS));
281 save_magic(mgs_ix, sv);
282 /* omit MGf_GSKIP -- not changed here */
283 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
284 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
291 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
296 Perl_croak(aTHX_ "Size magic not implemented");
305 Clear something magical that the SV represents. See C<sv_magic>.
311 Perl_mg_clear(pTHX_ SV *sv)
316 mgs_ix = SSNEW(sizeof(MGS));
317 save_magic(mgs_ix, sv);
319 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
320 MGVTBL* vtbl = mg->mg_virtual;
321 /* omit GSKIP -- never set here */
323 if (vtbl && vtbl->svt_clear)
324 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
327 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
334 Finds the magic pointer for type matching the SV. See C<sv_magic>.
340 Perl_mg_find(pTHX_ SV *sv, int type)
345 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
346 if (mg->mg_type == type)
355 Copies the magic from one SV to another. See C<sv_magic>.
361 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
365 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
366 MGVTBL* vtbl = mg->mg_virtual;
367 if ((mg->mg_flags & MGf_COPY) && vtbl->svt_copy){
368 count += CALL_FPTR(vtbl->svt_copy)(aTHX_ sv, mg, nsv, key, klen);
370 else if (isUPPER(mg->mg_type)) {
372 mg->mg_type == PERL_MAGIC_tied ? SvTIED_obj(sv, mg) :
373 (mg->mg_type == PERL_MAGIC_regdata && mg->mg_obj)
375 toLOWER(mg->mg_type), key, klen);
385 Free any magic storage used by the SV. See C<sv_magic>.
391 Perl_mg_free(pTHX_ SV *sv)
395 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
396 MGVTBL* vtbl = mg->mg_virtual;
397 moremagic = mg->mg_moremagic;
398 if (vtbl && vtbl->svt_free)
399 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
400 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
401 if (mg->mg_len > 0 || mg->mg_type == PERL_MAGIC_utf8)
402 Safefree(mg->mg_ptr);
403 else if (mg->mg_len == HEf_SVKEY)
404 SvREFCNT_dec((SV*)mg->mg_ptr);
406 if (mg->mg_flags & MGf_REFCOUNTED)
407 SvREFCNT_dec(mg->mg_obj);
417 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
421 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
422 if (mg->mg_obj) /* @+ */
425 return rx->lastparen;
432 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
440 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
444 if (paren <= (I32)rx->nparens &&
445 (s = rx->startp[paren]) != -1 &&
446 (t = rx->endp[paren]) != -1)
448 if (mg->mg_obj) /* @+ */
453 if (i > 0 && RX_MATCH_UTF8(rx)) {
454 char *b = rx->subbeg;
456 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
466 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
468 Perl_croak(aTHX_ PL_no_modify);
474 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
481 switch (*mg->mg_ptr) {
482 case '1': case '2': case '3': case '4':
483 case '5': case '6': case '7': case '8': case '9': case '&':
484 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
486 paren = atoi(mg->mg_ptr); /* $& is in [0] */
488 if (paren <= (I32)rx->nparens &&
489 (s1 = rx->startp[paren]) != -1 &&
490 (t1 = rx->endp[paren]) != -1)
494 if (i > 0 && RX_MATCH_UTF8(rx)) {
495 char *s = rx->subbeg + s1;
496 char *send = rx->subbeg + t1;
499 if (is_utf8_string((U8*)s, i))
500 i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send);
503 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
507 if (ckWARN(WARN_UNINITIALIZED))
512 if (ckWARN(WARN_UNINITIALIZED))
517 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
518 paren = rx->lastparen;
523 case '\016': /* ^N */
524 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
525 paren = rx->lastcloseparen;
531 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
532 if (rx->startp[0] != -1) {
543 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
544 if (rx->endp[0] != -1) {
545 i = rx->sublen - rx->endp[0];
556 if (!SvPOK(sv) && SvNIOK(sv)) {
566 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
569 register char *s = NULL;
573 switch (*mg->mg_ptr) {
574 case '\001': /* ^A */
575 sv_setsv(sv, PL_bodytarget);
577 case '\003': /* ^C */
578 sv_setiv(sv, (IV)PL_minus_c);
581 case '\004': /* ^D */
582 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
584 case '\005': /* ^E */
585 if (*(mg->mg_ptr+1) == '\0') {
586 #ifdef MACOS_TRADITIONAL
590 sv_setnv(sv,(double)gMacPerl_OSErr);
591 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
596 # include <descrip.h>
597 # include <starlet.h>
599 $DESCRIPTOR(msgdsc,msg);
600 sv_setnv(sv,(NV) vaxc$errno);
601 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
602 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
608 if (!(_emx_env & 0x200)) { /* Under DOS */
609 sv_setnv(sv, (NV)errno);
610 sv_setpv(sv, errno ? Strerror(errno) : "");
612 if (errno != errno_isOS2) {
613 int tmp = _syserrno();
614 if (tmp) /* 2nd call to _syserrno() makes it 0 */
617 sv_setnv(sv, (NV)Perl_rc);
618 sv_setpv(sv, os2error(Perl_rc));
623 DWORD dwErr = GetLastError();
624 sv_setnv(sv, (NV)dwErr);
627 PerlProc_GetOSError(sv, dwErr);
635 int saveerrno = errno;
636 sv_setnv(sv, (NV)errno);
637 sv_setpv(sv, errno ? Strerror(errno) : "");
644 SvNOK_on(sv); /* what a wonderful hack! */
646 else if (strEQ(mg->mg_ptr+1, "NCODING"))
647 sv_setsv(sv, PL_encoding);
649 case '\006': /* ^F */
650 sv_setiv(sv, (IV)PL_maxsysfd);
652 case '\010': /* ^H */
653 sv_setiv(sv, (IV)PL_hints);
655 case '\011': /* ^I */ /* NOT \t in EBCDIC */
657 sv_setpv(sv, PL_inplace);
659 sv_setsv(sv, &PL_sv_undef);
661 case '\017': /* ^O & ^OPEN */
662 if (*(mg->mg_ptr+1) == '\0') {
663 sv_setpv(sv, PL_osname);
666 else if (strEQ(mg->mg_ptr, "\017PEN")) {
667 if (!PL_compiling.cop_io)
668 sv_setsv(sv, &PL_sv_undef);
670 sv_setsv(sv, PL_compiling.cop_io);
674 case '\020': /* ^P */
675 sv_setiv(sv, (IV)PL_perldb);
677 case '\023': /* ^S */
678 if (*(mg->mg_ptr+1) == '\0') {
679 if (PL_lex_state != LEX_NOTPARSING)
682 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
687 case '\024': /* ^T */
688 if (*(mg->mg_ptr+1) == '\0') {
690 sv_setnv(sv, PL_basetime);
692 sv_setiv(sv, (IV)PL_basetime);
695 else if (strEQ(mg->mg_ptr, "\024AINT"))
696 sv_setiv(sv, PL_tainting
697 ? (PL_taint_warn || PL_unsafe ? -1 : 1)
700 case '\025': /* $^UNICODE */
701 if (strEQ(mg->mg_ptr, "\025NICODE"))
702 sv_setuv(sv, (UV) PL_unicode);
704 case '\027': /* ^W & $^WARNING_BITS */
705 if (*(mg->mg_ptr+1) == '\0')
706 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
707 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
708 if (PL_compiling.cop_warnings == pWARN_NONE ||
709 PL_compiling.cop_warnings == pWARN_STD)
711 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
713 else if (PL_compiling.cop_warnings == pWARN_ALL) {
714 /* Get the bit mask for $warnings::Bits{all}, because
715 * it could have been extended by warnings::register */
717 HV *bits=get_hv("warnings::Bits", FALSE);
718 if (bits && (bits_all=hv_fetch(bits, "all", 3, FALSE))) {
719 sv_setsv(sv, *bits_all);
722 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
726 sv_setsv(sv, PL_compiling.cop_warnings);
731 case '1': case '2': case '3': case '4':
732 case '5': case '6': case '7': case '8': case '9': case '&':
733 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
737 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
738 * XXX Does the new way break anything?
740 paren = atoi(mg->mg_ptr); /* $& is in [0] */
742 if (paren <= (I32)rx->nparens &&
743 (s1 = rx->startp[paren]) != -1 &&
744 (t1 = rx->endp[paren]) != -1)
754 if (RX_MATCH_UTF8(rx) && is_utf8_string((U8*)s, i))
759 if (RX_MATCH_TAINTED(rx)) {
760 MAGIC* mg = SvMAGIC(sv);
763 SvMAGIC(sv) = mg->mg_moremagic;
765 if ((mgt = SvMAGIC(sv))) {
766 mg->mg_moremagic = mgt;
776 sv_setsv(sv,&PL_sv_undef);
779 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
780 paren = rx->lastparen;
784 sv_setsv(sv,&PL_sv_undef);
786 case '\016': /* ^N */
787 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
788 paren = rx->lastcloseparen;
792 sv_setsv(sv,&PL_sv_undef);
795 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
796 if ((s = rx->subbeg) && rx->startp[0] != -1) {
801 sv_setsv(sv,&PL_sv_undef);
804 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
805 if (rx->subbeg && rx->endp[0] != -1) {
806 s = rx->subbeg + rx->endp[0];
807 i = rx->sublen - rx->endp[0];
811 sv_setsv(sv,&PL_sv_undef);
815 if (GvIO(PL_last_in_gv)) {
816 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
822 sv_setiv(sv, (IV)STATUS_CURRENT);
823 #ifdef COMPLEX_STATUS
824 LvTARGOFF(sv) = PL_statusvalue;
825 LvTARGLEN(sv) = PL_statusvalue_vms;
830 if (GvIOp(PL_defoutgv))
831 s = IoTOP_NAME(GvIOp(PL_defoutgv));
835 sv_setpv(sv,GvENAME(PL_defoutgv));
840 if (GvIOp(PL_defoutgv))
841 s = IoFMT_NAME(GvIOp(PL_defoutgv));
843 s = GvENAME(PL_defoutgv);
848 if (GvIOp(PL_defoutgv))
849 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
852 if (GvIOp(PL_defoutgv))
853 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
856 if (GvIOp(PL_defoutgv))
857 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
865 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
868 if (GvIOp(PL_defoutgv))
869 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
875 sv_copypv(sv, PL_ors_sv);
878 sv_setpv(sv,PL_ofmt);
882 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
883 sv_setpv(sv, errno ? Strerror(errno) : "");
886 int saveerrno = errno;
887 sv_setnv(sv, (NV)errno);
889 if (errno == errno_isOS2 || errno == errno_isOS2_set)
890 sv_setpv(sv, os2error(Perl_rc));
893 sv_setpv(sv, errno ? Strerror(errno) : "");
897 SvNOK_on(sv); /* what a wonderful hack! */
900 sv_setiv(sv, (IV)PL_uid);
903 sv_setiv(sv, (IV)PL_euid);
906 sv_setiv(sv, (IV)PL_gid);
908 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
912 sv_setiv(sv, (IV)PL_egid);
914 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
919 Groups_t gary[NGROUPS];
920 i = getgroups(NGROUPS,gary);
922 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
925 (void)SvIOK_on(sv); /* what a wonderful hack! */
927 #ifndef MACOS_TRADITIONAL
936 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
938 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
940 if (uf && uf->uf_val)
941 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
946 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
956 #ifdef DYNAMIC_ENV_FETCH
957 /* We just undefd an environment var. Is a replacement */
958 /* waiting in the wings? */
961 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
962 s = SvPV(*valp, len);
966 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
967 /* And you'll never guess what the dog had */
968 /* in its mouth... */
970 MgTAINTEDDIR_off(mg);
972 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
973 char pathbuf[256], eltbuf[256], *cp, *elt = s;
977 do { /* DCL$PATH may be a search list */
978 while (1) { /* as may dev portion of any element */
979 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
980 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
981 cando_by_name(S_IWUSR,0,elt) ) {
986 if ((cp = strchr(elt, ':')) != Nullch)
988 if (my_trnlnm(elt, eltbuf, j++))
994 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
997 if (s && klen == 4 && strEQ(ptr,"PATH")) {
998 char *strend = s + len;
1000 while (s < strend) {
1004 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
1005 s, strend, ':', &i);
1007 if (i >= sizeof tmpbuf /* too long -- assume the worst */
1009 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
1010 MgTAINTEDDIR_on(mg);
1016 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
1022 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
1025 my_setenv(MgPV(mg,n_a),Nullch);
1030 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
1033 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1035 if (PL_localizing) {
1038 magic_clear_all_env(sv,mg);
1039 hv_iterinit((HV*)sv);
1040 while ((entry = hv_iternext((HV*)sv))) {
1042 my_setenv(hv_iterkey(entry, &keylen),
1043 SvPV(hv_iterval((HV*)sv, entry), n_a));
1051 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
1054 #if defined(VMS) || defined(EPOC)
1055 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1057 # if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
1060 # ifdef USE_ENVIRON_ARRAY
1061 # if defined(USE_ITHREADS)
1062 /* only the parent thread can clobber the process environment */
1063 if (PL_curinterp == aTHX)
1066 # ifndef PERL_USE_SAFE_PUTENV
1069 if (environ == PL_origenviron)
1070 environ = (char**)safesysmalloc(sizeof(char*));
1072 for (i = 0; environ[i]; i++)
1073 safesysfree(environ[i]);
1074 # endif /* PERL_USE_SAFE_PUTENV */
1076 environ[0] = Nullch;
1078 # endif /* USE_ENVIRON_ARRAY */
1079 # endif /* PERL_IMPLICIT_SYS || WIN32 */
1080 #endif /* VMS || EPOC */
1081 #endif /* !PERL_MICRO */
1085 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS)||defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1086 static int sig_handlers_initted = 0;
1088 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1089 static int sig_ignoring[SIG_SIZE]; /* which signals we are ignoring */
1091 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1092 static int sig_defaulting[SIG_SIZE];
1096 #ifdef HAS_SIGPROCMASK
1098 restore_sigmask(pTHX_ SV *save_sv)
1100 sigset_t *ossetp = (sigset_t *) SvPV_nolen( save_sv );
1101 (void)sigprocmask(SIG_SETMASK, ossetp, (sigset_t *)0);
1105 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
1109 /* Are we fetching a signal entry? */
1110 i = whichsig(MgPV(mg,n_a));
1113 sv_setsv(sv,PL_psig_ptr[i]);
1115 Sighandler_t sigstate;
1116 sigstate = rsignal_state(i);
1117 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1118 if (sig_handlers_initted && sig_ignoring[i]) sigstate = SIG_IGN;
1120 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1121 if (sig_handlers_initted && sig_defaulting[i]) sigstate = SIG_DFL;
1123 /* cache state so we don't fetch it again */
1124 if(sigstate == SIG_IGN)
1125 sv_setpv(sv,"IGNORE");
1127 sv_setsv(sv,&PL_sv_undef);
1128 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1135 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
1137 /* XXX Some of this code was copied from Perl_magic_setsig. A little
1138 * refactoring might be in order.
1146 if (strEQ(s,"__DIE__"))
1148 else if (strEQ(s,"__WARN__"))
1151 Perl_croak(aTHX_ "No such hook: %s", s);
1155 SvREFCNT_dec(to_dec);
1160 /* Are we clearing a signal entry? */
1163 #ifdef HAS_SIGPROCMASK
1166 /* Avoid having the signal arrive at a bad time, if possible. */
1169 sigprocmask(SIG_BLOCK, &set, &save);
1171 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1172 SAVEFREESV(save_sv);
1173 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1176 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1177 if (!sig_handlers_initted) Perl_csighandler_init();
1179 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1180 sig_defaulting[i] = 1;
1181 (void)rsignal(i, PL_csighandlerp);
1183 (void)rsignal(i, SIG_DFL);
1185 if(PL_psig_name[i]) {
1186 SvREFCNT_dec(PL_psig_name[i]);
1189 if(PL_psig_ptr[i]) {
1190 to_dec=PL_psig_ptr[i];
1193 SvREFCNT_dec(to_dec);
1203 Perl_raise_signal(pTHX_ int sig)
1205 /* Set a flag to say this signal is pending */
1206 PL_psig_pend[sig]++;
1207 /* And one to say _a_ signal is pending */
1212 Perl_csighandler(int sig)
1214 #ifdef PERL_GET_SIG_CONTEXT
1215 dTHXa(PERL_GET_SIG_CONTEXT);
1219 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1220 (void) rsignal(sig, PL_csighandlerp);
1221 if (sig_ignoring[sig]) return;
1223 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1224 if (sig_defaulting[sig])
1225 #ifdef KILL_BY_SIGPRC
1226 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1231 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
1232 /* Call the perl level handler now--
1233 * with risk we may be in malloc() etc. */
1234 (*PL_sighandlerp)(sig);
1236 Perl_raise_signal(aTHX_ sig);
1239 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1241 Perl_csighandler_init(void)
1244 if (sig_handlers_initted) return;
1246 for (sig = 1; sig < SIG_SIZE; sig++) {
1247 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1249 sig_defaulting[sig] = 1;
1250 (void) rsignal(sig, PL_csighandlerp);
1252 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1253 sig_ignoring[sig] = 0;
1256 sig_handlers_initted = 1;
1261 Perl_despatch_signals(pTHX)
1265 for (sig = 1; sig < SIG_SIZE; sig++) {
1266 if (PL_psig_pend[sig]) {
1267 PERL_BLOCKSIG_ADD(set, sig);
1268 PL_psig_pend[sig] = 0;
1269 PERL_BLOCKSIG_BLOCK(set);
1270 (*PL_sighandlerp)(sig);
1271 PERL_BLOCKSIG_UNBLOCK(set);
1277 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1282 /* Need to be careful with SvREFCNT_dec(), because that can have side
1283 * effects (due to closures). We must make sure that the new disposition
1284 * is in place before it is called.
1288 #ifdef HAS_SIGPROCMASK
1295 if (strEQ(s,"__DIE__"))
1297 else if (strEQ(s,"__WARN__"))
1300 Perl_croak(aTHX_ "No such hook: %s", s);
1308 i = whichsig(s); /* ...no, a brick */
1310 if (ckWARN(WARN_SIGNAL))
1311 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
1314 #ifdef HAS_SIGPROCMASK
1315 /* Avoid having the signal arrive at a bad time, if possible. */
1318 sigprocmask(SIG_BLOCK, &set, &save);
1320 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1321 SAVEFREESV(save_sv);
1322 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1325 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1326 if (!sig_handlers_initted) Perl_csighandler_init();
1328 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1329 sig_ignoring[i] = 0;
1331 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1332 sig_defaulting[i] = 0;
1334 SvREFCNT_dec(PL_psig_name[i]);
1335 to_dec = PL_psig_ptr[i];
1336 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1337 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1338 PL_psig_name[i] = newSVpvn(s, len);
1339 SvREADONLY_on(PL_psig_name[i]);
1341 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1343 (void)rsignal(i, PL_csighandlerp);
1344 #ifdef HAS_SIGPROCMASK
1349 *svp = SvREFCNT_inc(sv);
1351 SvREFCNT_dec(to_dec);
1354 s = SvPV_force(sv,len);
1355 if (strEQ(s,"IGNORE")) {
1357 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1358 sig_ignoring[i] = 1;
1359 (void)rsignal(i, PL_csighandlerp);
1361 (void)rsignal(i, SIG_IGN);
1365 else if (strEQ(s,"DEFAULT") || !*s) {
1367 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1369 sig_defaulting[i] = 1;
1370 (void)rsignal(i, PL_csighandlerp);
1373 (void)rsignal(i, SIG_DFL);
1378 * We should warn if HINT_STRICT_REFS, but without
1379 * access to a known hint bit in a known OP, we can't
1380 * tell whether HINT_STRICT_REFS is in force or not.
1382 if (!strchr(s,':') && !strchr(s,'\''))
1383 sv_insert(sv, 0, 0, "main::", 6);
1385 (void)rsignal(i, PL_csighandlerp);
1387 *svp = SvREFCNT_inc(sv);
1389 #ifdef HAS_SIGPROCMASK
1394 SvREFCNT_dec(to_dec);
1397 #endif /* !PERL_MICRO */
1400 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1402 PL_sub_generation++;
1407 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1409 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1410 PL_amagic_generation++;
1416 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1418 HV *hv = (HV*)LvTARG(sv);
1422 (void) hv_iterinit(hv);
1423 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1426 while (hv_iternext(hv))
1431 sv_setiv(sv, (IV)i);
1436 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1439 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1444 /* caller is responsible for stack switching/cleanup */
1446 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1452 PUSHs(SvTIED_obj(sv, mg));
1455 if (mg->mg_len >= 0)
1456 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1457 else if (mg->mg_len == HEf_SVKEY)
1458 PUSHs((SV*)mg->mg_ptr);
1460 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1461 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1469 return call_method(meth, flags);
1473 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1479 PUSHSTACKi(PERLSI_MAGIC);
1481 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1482 sv_setsv(sv, *PL_stack_sp--);
1492 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1495 mg->mg_flags |= MGf_GSKIP;
1496 magic_methpack(sv,mg,"FETCH");
1501 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1505 PUSHSTACKi(PERLSI_MAGIC);
1506 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1513 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1515 return magic_methpack(sv,mg,"DELETE");
1520 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1527 PUSHSTACKi(PERLSI_MAGIC);
1528 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1529 sv = *PL_stack_sp--;
1530 retval = (U32) SvIV(sv)-1;
1539 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1544 PUSHSTACKi(PERLSI_MAGIC);
1546 XPUSHs(SvTIED_obj(sv, mg));
1548 call_method("CLEAR", G_SCALAR|G_DISCARD);
1556 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1559 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1563 PUSHSTACKi(PERLSI_MAGIC);
1566 PUSHs(SvTIED_obj(sv, mg));
1571 if (call_method(meth, G_SCALAR))
1572 sv_setsv(key, *PL_stack_sp--);
1581 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1583 return magic_methpack(sv,mg,"EXISTS");
1587 Perl_magic_scalarpack(pTHX_ HV *hv, MAGIC *mg)
1590 SV *retval = &PL_sv_undef;
1591 SV *tied = SvTIED_obj((SV*)hv, mg);
1592 HV *pkg = SvSTASH((SV*)SvRV(tied));
1594 if (!gv_fetchmethod_autoload(pkg, "SCALAR", FALSE)) {
1597 /* we are in an iteration so the hash cannot be empty */
1599 /* no xhv_eiter so now use FIRSTKEY */
1600 key = sv_newmortal();
1601 magic_nextpack((SV*)hv, mg, key);
1602 HvEITER(hv) = NULL; /* need to reset iterator */
1603 return SvOK(key) ? &PL_sv_yes : &PL_sv_no;
1606 /* there is a SCALAR method that we can call */
1608 PUSHSTACKi(PERLSI_MAGIC);
1614 if (call_method("SCALAR", G_SCALAR))
1615 retval = *PL_stack_sp--;
1622 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1632 svp = av_fetch(GvAV(gv),
1633 atoi(MgPV(mg,n_a)), FALSE);
1634 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp)))) {
1635 /* set or clear breakpoint in the relevant control op */
1637 o->op_flags |= OPf_SPECIAL;
1639 o->op_flags &= ~OPf_SPECIAL;
1645 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1647 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1652 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1654 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1659 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1661 SV* lsv = LvTARG(sv);
1663 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1664 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1665 if (mg && mg->mg_len >= 0) {
1668 sv_pos_b2u(lsv, &i);
1669 sv_setiv(sv, i + PL_curcop->cop_arybase);
1678 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1680 SV* lsv = LvTARG(sv);
1687 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1688 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1692 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1693 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1695 else if (!SvOK(sv)) {
1699 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1701 pos = SvIV(sv) - PL_curcop->cop_arybase;
1704 ulen = sv_len_utf8(lsv);
1714 else if (pos > (SSize_t)len)
1719 sv_pos_u2b(lsv, &p, 0);
1724 mg->mg_flags &= ~MGf_MINMATCH;
1730 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1732 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1734 gv_efullname3(sv,((GV*)sv), "*");
1738 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1743 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1752 if (*s == '*' && s[1])
1754 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1759 GvGP(sv) = gp_ref(GvGP(gv));
1764 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1767 SV *lsv = LvTARG(sv);
1768 char *tmps = SvPV(lsv,len);
1769 I32 offs = LvTARGOFF(sv);
1770 I32 rem = LvTARGLEN(sv);
1773 sv_pos_u2b(lsv, &offs, &rem);
1774 if (offs > (I32)len)
1776 if (rem + offs > (I32)len)
1778 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1785 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1788 char *tmps = SvPV(sv, len);
1789 SV *lsv = LvTARG(sv);
1790 I32 lvoff = LvTARGOFF(sv);
1791 I32 lvlen = LvTARGLEN(sv);
1794 sv_utf8_upgrade(lsv);
1795 sv_pos_u2b(lsv, &lvoff, &lvlen);
1796 sv_insert(lsv, lvoff, lvlen, tmps, len);
1797 LvTARGLEN(sv) = sv_len_utf8(sv);
1800 else if (lsv && SvUTF8(lsv)) {
1801 sv_pos_u2b(lsv, &lvoff, &lvlen);
1802 LvTARGLEN(sv) = len;
1803 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
1804 sv_insert(lsv, lvoff, lvlen, tmps, len);
1808 sv_insert(lsv, lvoff, lvlen, tmps, len);
1809 LvTARGLEN(sv) = len;
1817 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1819 TAINT_IF((mg->mg_len & 1) ||
1820 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1825 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1827 if (PL_localizing) {
1828 if (PL_localizing == 1)
1833 else if (PL_tainted)
1841 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1843 SV *lsv = LvTARG(sv);
1850 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1855 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1857 do_vecset(sv); /* XXX slurp this routine */
1862 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1865 if (LvTARGLEN(sv)) {
1867 SV *ahv = LvTARG(sv);
1868 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1873 AV* av = (AV*)LvTARG(sv);
1874 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1875 targ = AvARRAY(av)[LvTARGOFF(sv)];
1877 if (targ && targ != &PL_sv_undef) {
1878 /* somebody else defined it for us */
1879 SvREFCNT_dec(LvTARG(sv));
1880 LvTARG(sv) = SvREFCNT_inc(targ);
1882 SvREFCNT_dec(mg->mg_obj);
1883 mg->mg_obj = Nullsv;
1884 mg->mg_flags &= ~MGf_REFCOUNTED;
1889 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1894 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1899 sv_setsv(LvTARG(sv), sv);
1900 SvSETMAGIC(LvTARG(sv));
1906 Perl_vivify_defelem(pTHX_ SV *sv)
1911 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
1914 SV *ahv = LvTARG(sv);
1916 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1919 if (!value || value == &PL_sv_undef)
1920 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1923 AV* av = (AV*)LvTARG(sv);
1924 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1925 LvTARG(sv) = Nullsv; /* array can't be extended */
1927 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1928 if (!svp || (value = *svp) == &PL_sv_undef)
1929 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1932 (void)SvREFCNT_inc(value);
1933 SvREFCNT_dec(LvTARG(sv));
1936 SvREFCNT_dec(mg->mg_obj);
1937 mg->mg_obj = Nullsv;
1938 mg->mg_flags &= ~MGf_REFCOUNTED;
1942 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1944 AV *av = (AV*)mg->mg_obj;
1945 SV **svp = AvARRAY(av);
1946 I32 i = AvFILLp(av);
1949 if (!SvWEAKREF(svp[i]))
1950 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1951 /* XXX Should we check that it hasn't changed? */
1953 (void)SvOK_off(svp[i]);
1954 SvWEAKREF_off(svp[i]);
1959 SvREFCNT_dec(av); /* remove extra count added by sv_add_backref() */
1964 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1972 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1974 sv_unmagic(sv, PERL_MAGIC_bm);
1980 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1982 sv_unmagic(sv, PERL_MAGIC_fm);
1988 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1990 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1992 if (uf && uf->uf_set)
1993 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
1998 Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
2000 sv_unmagic(sv, PERL_MAGIC_qr);
2005 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
2007 regexp *re = (regexp *)mg->mg_obj;
2012 #ifdef USE_LOCALE_COLLATE
2014 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
2017 * RenE<eacute> Descartes said "I think not."
2018 * and vanished with a faint plop.
2021 Safefree(mg->mg_ptr);
2027 #endif /* USE_LOCALE_COLLATE */
2029 /* Just clear the UTF-8 cache data. */
2031 Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
2033 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
2035 mg->mg_len = -1; /* The mg_len holds the len cache. */
2040 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
2045 switch (*mg->mg_ptr) {
2046 case '\001': /* ^A */
2047 sv_setsv(PL_bodytarget, sv);
2049 case '\003': /* ^C */
2050 PL_minus_c = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2053 case '\004': /* ^D */
2056 PL_debug = get_debug_opts(&s) | DEBUG_TOP_FLAG;
2057 DEBUG_x(dump_all());
2059 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
2062 case '\005': /* ^E */
2063 if (*(mg->mg_ptr+1) == '\0') {
2064 #ifdef MACOS_TRADITIONAL
2065 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2068 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2071 SetLastError( SvIV(sv) );
2074 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2076 /* will anyone ever use this? */
2077 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
2083 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
2085 SvREFCNT_dec(PL_encoding);
2086 if (SvOK(sv) || SvGMAGICAL(sv)) {
2087 PL_encoding = newSVsv(sv);
2090 PL_encoding = Nullsv;
2094 case '\006': /* ^F */
2095 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2097 case '\010': /* ^H */
2098 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2100 case '\011': /* ^I */ /* NOT \t in EBCDIC */
2102 Safefree(PL_inplace);
2104 PL_inplace = savepv(SvPV(sv,len));
2106 PL_inplace = Nullch;
2108 case '\017': /* ^O */
2109 if (*(mg->mg_ptr+1) == '\0') {
2111 Safefree(PL_osname);
2115 TAINT_PROPER("assigning to $^O");
2116 PL_osname = savepv(SvPV(sv,len));
2119 else if (strEQ(mg->mg_ptr, "\017PEN")) {
2120 if (!PL_compiling.cop_io)
2121 PL_compiling.cop_io = newSVsv(sv);
2123 sv_setsv(PL_compiling.cop_io,sv);
2126 case '\020': /* ^P */
2127 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2128 if ((PERLDB_SUB || PERLDB_LINE || PERLDB_SUBLINE || PERLDB_ASSERTION)
2132 case '\024': /* ^T */
2134 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
2136 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2139 case '\027': /* ^W & $^WARNING_BITS */
2140 if (*(mg->mg_ptr+1) == '\0') {
2141 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2142 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2143 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
2144 | (i ? G_WARN_ON : G_WARN_OFF) ;
2147 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
2148 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2149 if (!SvPOK(sv) && PL_localizing) {
2150 sv_setpvn(sv, WARN_NONEstring, WARNsize);
2151 PL_compiling.cop_warnings = pWARN_NONE;
2156 int accumulate = 0 ;
2157 int any_fatals = 0 ;
2158 char * ptr = (char*)SvPV(sv, len) ;
2159 for (i = 0 ; i < len ; ++i) {
2160 accumulate |= ptr[i] ;
2161 any_fatals |= (ptr[i] & 0xAA) ;
2164 PL_compiling.cop_warnings = pWARN_NONE;
2165 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
2166 PL_compiling.cop_warnings = pWARN_ALL;
2167 PL_dowarn |= G_WARN_ONCE ;
2170 if (specialWARN(PL_compiling.cop_warnings))
2171 PL_compiling.cop_warnings = newSVsv(sv) ;
2173 sv_setsv(PL_compiling.cop_warnings, sv);
2174 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2175 PL_dowarn |= G_WARN_ONCE ;
2183 if (PL_localizing) {
2184 if (PL_localizing == 1)
2185 SAVESPTR(PL_last_in_gv);
2187 else if (SvOK(sv) && GvIO(PL_last_in_gv))
2188 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
2191 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2192 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
2193 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
2196 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2197 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
2198 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
2201 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2204 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2205 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2206 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
2209 IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2213 IO *io = GvIOp(PL_defoutgv);
2216 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
2217 IoFLAGS(io) &= ~IOf_FLUSH;
2219 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2220 PerlIO *ofp = IoOFP(io);
2222 (void)PerlIO_flush(ofp);
2223 IoFLAGS(io) |= IOf_FLUSH;
2229 SvREFCNT_dec(PL_rs);
2230 PL_rs = newSVsv(sv);
2234 SvREFCNT_dec(PL_ors_sv);
2235 if (SvOK(sv) || SvGMAGICAL(sv)) {
2236 PL_ors_sv = newSVsv(sv);
2244 SvREFCNT_dec(PL_ofs_sv);
2245 if (SvOK(sv) || SvGMAGICAL(sv)) {
2246 PL_ofs_sv = newSVsv(sv);
2255 PL_ofmt = savepv(SvPV(sv,len));
2258 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2261 #ifdef COMPLEX_STATUS
2262 if (PL_localizing == 2) {
2263 PL_statusvalue = LvTARGOFF(sv);
2264 PL_statusvalue_vms = LvTARGLEN(sv);
2268 #ifdef VMSISH_STATUS
2270 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
2273 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2278 # define PERL_VMS_BANG vaxc$errno
2280 # define PERL_VMS_BANG 0
2282 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
2283 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
2287 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2288 if (PL_delaymagic) {
2289 PL_delaymagic |= DM_RUID;
2290 break; /* don't do magic till later */
2293 (void)setruid((Uid_t)PL_uid);
2296 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
2298 #ifdef HAS_SETRESUID
2299 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
2301 if (PL_uid == PL_euid) { /* special case $< = $> */
2303 /* workaround for Darwin's setuid peculiarity, cf [perl #24122] */
2304 if (PL_uid != 0 && PerlProc_getuid() == 0)
2305 (void)PerlProc_setuid(0);
2307 (void)PerlProc_setuid(PL_uid);
2309 PL_uid = PerlProc_getuid();
2310 Perl_croak(aTHX_ "setruid() not implemented");
2315 PL_uid = PerlProc_getuid();
2316 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2319 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2320 if (PL_delaymagic) {
2321 PL_delaymagic |= DM_EUID;
2322 break; /* don't do magic till later */
2325 (void)seteuid((Uid_t)PL_euid);
2328 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
2330 #ifdef HAS_SETRESUID
2331 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
2333 if (PL_euid == PL_uid) /* special case $> = $< */
2334 PerlProc_setuid(PL_euid);
2336 PL_euid = PerlProc_geteuid();
2337 Perl_croak(aTHX_ "seteuid() not implemented");
2342 PL_euid = PerlProc_geteuid();
2343 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2346 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2347 if (PL_delaymagic) {
2348 PL_delaymagic |= DM_RGID;
2349 break; /* don't do magic till later */
2352 (void)setrgid((Gid_t)PL_gid);
2355 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
2357 #ifdef HAS_SETRESGID
2358 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2360 if (PL_gid == PL_egid) /* special case $( = $) */
2361 (void)PerlProc_setgid(PL_gid);
2363 PL_gid = PerlProc_getgid();
2364 Perl_croak(aTHX_ "setrgid() not implemented");
2369 PL_gid = PerlProc_getgid();
2370 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2373 #ifdef HAS_SETGROUPS
2375 char *p = SvPV(sv, len);
2376 Groups_t gary[NGROUPS];
2381 for (i = 0; i < NGROUPS; ++i) {
2382 while (*p && !isSPACE(*p))
2391 (void)setgroups(i, gary);
2393 #else /* HAS_SETGROUPS */
2394 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2395 #endif /* HAS_SETGROUPS */
2396 if (PL_delaymagic) {
2397 PL_delaymagic |= DM_EGID;
2398 break; /* don't do magic till later */
2401 (void)setegid((Gid_t)PL_egid);
2404 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2406 #ifdef HAS_SETRESGID
2407 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2409 if (PL_egid == PL_gid) /* special case $) = $( */
2410 (void)PerlProc_setgid(PL_egid);
2412 PL_egid = PerlProc_getegid();
2413 Perl_croak(aTHX_ "setegid() not implemented");
2418 PL_egid = PerlProc_getegid();
2419 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2422 PL_chopset = SvPV_force(sv,len);
2424 #ifndef MACOS_TRADITIONAL
2426 LOCK_DOLLARZERO_MUTEX;
2427 #ifdef HAS_SETPROCTITLE
2428 /* The BSDs don't show the argv[] in ps(1) output, they
2429 * show a string from the process struct and provide
2430 * the setproctitle() routine to manipulate that. */
2433 # if __FreeBSD_version > 410001
2434 /* The leading "-" removes the "perl: " prefix,
2435 * but not the "(perl) suffix from the ps(1)
2436 * output, because that's what ps(1) shows if the
2437 * argv[] is modified. */
2438 setproctitle("-%s", s);
2439 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2440 /* This doesn't really work if you assume that
2441 * $0 = 'foobar'; will wipe out 'perl' from the $0
2442 * because in ps(1) output the result will be like
2443 * sprintf("perl: %s (perl)", s)
2444 * I guess this is a security feature:
2445 * one (a user process) cannot get rid of the original name.
2447 setproctitle("%s", s);
2451 #if defined(__hpux) && defined(PSTAT_SETCMD)
2456 pstat(PSTAT_SETCMD, un, len, 0, 0);
2459 /* PL_origalen is set in perl_parse(). */
2460 s = SvPV_force(sv,len);
2461 if (len >= (STRLEN)PL_origalen-1) {
2462 /* Longer than original, will be truncated. We assume that
2463 * PL_origalen bytes are available. */
2464 Copy(s, PL_origargv[0], PL_origalen-1, char);
2467 /* Shorter than original, will be padded. */
2468 Copy(s, PL_origargv[0], len, char);
2469 PL_origargv[0][len] = 0;
2470 memset(PL_origargv[0] + len + 1,
2471 /* Is the space counterintuitive? Yes.
2472 * (You were expecting \0?)
2473 * Does it work? Seems to. (In Linux 2.4.20 at least.)
2476 PL_origalen - len - 1);
2478 PL_origargv[0][PL_origalen-1] = 0;
2479 for (i = 1; i < PL_origargc; i++)
2481 UNLOCK_DOLLARZERO_MUTEX;
2489 Perl_whichsig(pTHX_ char *sig)
2491 register char **sigv;
2493 for (sigv = PL_sig_name; *sigv; sigv++)
2494 if (strEQ(sig,*sigv))
2495 return PL_sig_num[sigv - PL_sig_name];
2497 if (strEQ(sig,"CHLD"))
2501 if (strEQ(sig,"CLD"))
2507 #if !defined(PERL_IMPLICIT_CONTEXT)
2512 Perl_sighandler(int sig)
2514 #ifdef PERL_GET_SIG_CONTEXT
2515 dTHXa(PERL_GET_SIG_CONTEXT);
2522 SV *sv = Nullsv, *tSv = PL_Sv;
2528 if (PL_savestack_ix + 15 <= PL_savestack_max)
2530 if (PL_markstack_ptr < PL_markstack_max - 2)
2532 if (PL_retstack_ix < PL_retstack_max - 2)
2534 if (PL_scopestack_ix < PL_scopestack_max - 3)
2537 if (!PL_psig_ptr[sig]) {
2538 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
2543 /* Max number of items pushed there is 3*n or 4. We cannot fix
2544 infinity, so we fix 4 (in fact 5): */
2546 PL_savestack_ix += 5; /* Protect save in progress. */
2547 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2550 PL_markstack_ptr++; /* Protect mark. */
2553 PL_retstack[PL_retstack_ix] = NULL;
2556 PL_scopestack_ix += 1;
2557 /* sv_2cv is too complicated, try a simpler variant first: */
2558 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2559 || SvTYPE(cv) != SVt_PVCV)
2560 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2562 if (!cv || !CvROOT(cv)) {
2563 if (ckWARN(WARN_SIGNAL))
2564 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
2565 PL_sig_name[sig], (gv ? GvENAME(gv)
2572 if(PL_psig_name[sig]) {
2573 sv = SvREFCNT_inc(PL_psig_name[sig]);
2575 #if !defined(PERL_IMPLICIT_CONTEXT)
2579 sv = sv_newmortal();
2580 sv_setpv(sv,PL_sig_name[sig]);
2583 PUSHSTACKi(PERLSI_SIGNAL);
2588 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2591 if (SvTRUE(ERRSV)) {
2593 #ifdef HAS_SIGPROCMASK
2594 /* Handler "died", for example to get out of a restart-able read().
2595 * Before we re-do that on its behalf re-enable the signal which was
2596 * blocked by the system when we entered.
2600 sigaddset(&set,sig);
2601 sigprocmask(SIG_UNBLOCK, &set, NULL);
2603 /* Not clear if this will work */
2604 (void)rsignal(sig, SIG_IGN);
2605 (void)rsignal(sig, PL_csighandlerp);
2607 #endif /* !PERL_MICRO */
2608 Perl_die(aTHX_ Nullformat);
2612 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2618 PL_scopestack_ix -= 1;
2621 PL_op = myop; /* Apparently not needed... */
2623 PL_Sv = tSv; /* Restore global temporaries. */
2630 restore_magic(pTHX_ void *p)
2632 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2633 SV* sv = mgs->mgs_sv;
2638 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2640 #ifdef PERL_COPY_ON_WRITE
2641 /* While magic was saved (and off) sv_setsv may well have seen
2642 this SV as a prime candidate for COW. */
2644 sv_force_normal(sv);
2648 SvFLAGS(sv) |= mgs->mgs_flags;
2652 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2655 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2657 /* If we're still on top of the stack, pop us off. (That condition
2658 * will be satisfied if restore_magic was called explicitly, but *not*
2659 * if it's being called via leave_scope.)
2660 * The reason for doing this is that otherwise, things like sv_2cv()
2661 * may leave alloc gunk on the savestack, and some code
2662 * (e.g. sighandler) doesn't expect that...
2664 if (PL_savestack_ix == mgs->mgs_ss_ix)
2666 I32 popval = SSPOPINT;
2667 assert(popval == SAVEt_DESTRUCTOR_X);
2668 PL_savestack_ix -= 2;
2670 assert(popval == SAVEt_ALLOC);
2672 PL_savestack_ix -= popval;
2678 unwind_handler_stack(pTHX_ void *p)
2680 U32 flags = *(U32*)p;
2683 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2684 /* cxstack_ix-- Not needed, die already unwound it. */
2685 #if !defined(PERL_IMPLICIT_CONTEXT)
2687 SvREFCNT_dec(sig_sv);