3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005 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
19 "Magic" is special data attached to SV structures in order to give them
20 "magical" properties. When any Perl code tries to read from, or assign to,
21 an SV marked as magical, it calls the 'get' or 'set' function associated
22 with that SV's magic. A get is called prior to reading an SV, in order to
23 give it a chance to update its internal value (get on $. writes the line
24 number of the last read filehandle into to the SV's IV slot), while
25 set is called after an SV has been written to, in order to allow it to make
26 use of its changed value (set on $/ copies the SV's new value to the
27 PL_rs global variable).
29 Magic is implemented as a linked list of MAGIC structures attached to the
30 SV. Each MAGIC struct holds the type of the magic, a pointer to an array
31 of functions that implement the get(), set(), length() etc functions,
32 plus space for some flags and pointers. For example, a tied variable has
33 a MAGIC structure that contains a pointer to the object associated with the
42 #if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
52 # include <sys/pstat.h>
55 Signal_t Perl_csighandler(int sig);
57 /* if you only have signal() and it resets on each signal, FAKE_PERSISTENT_SIGNAL_HANDLERS fixes */
58 #if !defined(HAS_SIGACTION) && defined(VMS)
59 # define FAKE_PERSISTENT_SIGNAL_HANDLERS
61 /* if we're doing kill() with sys$sigprc on VMS, FAKE_DEFAULT_SIGNAL_HANDLERS */
62 #if defined(KILL_BY_SIGPRC)
63 # define FAKE_DEFAULT_SIGNAL_HANDLERS
66 static void restore_magic(pTHX_ void *p);
67 static void unwind_handler_stack(pTHX_ void *p);
70 /* Missing protos on LynxOS */
71 void setruid(uid_t id);
72 void seteuid(uid_t id);
73 void setrgid(uid_t id);
74 void setegid(uid_t id);
78 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
86 /* MGS is typedef'ed to struct magic_state in perl.h */
89 S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
92 assert(SvMAGICAL(sv));
93 #ifdef PERL_COPY_ON_WRITE
94 /* Turning READONLY off for a copy-on-write scalar is a bad idea. */
99 SAVEDESTRUCTOR_X(restore_magic, INT2PTR(void*, (IV)mgs_ix));
101 mgs = SSPTR(mgs_ix, MGS*);
103 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
104 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
108 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
112 =for apidoc mg_magical
114 Turns on the magical status of an SV. See C<sv_magic>.
120 Perl_mg_magical(pTHX_ SV *sv)
123 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
124 MGVTBL* vtbl = mg->mg_virtual;
126 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
130 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
139 Do magic after a value is retrieved from the SV. See C<sv_magic>.
145 Perl_mg_get(pTHX_ SV *sv)
148 MAGIC *newmg, *head, *cur, *mg;
149 I32 mgs_ix = SSNEW(sizeof(MGS));
150 int was_temp = SvTEMP(sv);
151 /* guard against sv having being freed midway by holding a private
154 /* sv_2mortal has this side effect of turning on the TEMP flag, which can
155 cause the SV's buffer to get stolen (and maybe other stuff).
158 sv_2mortal(SvREFCNT_inc(sv));
163 save_magic(mgs_ix, sv);
165 /* We must call svt_get(sv, mg) for each valid entry in the linked
166 list of magic. svt_get() may delete the current entry, add new
167 magic to the head of the list, or upgrade the SV. AMS 20010810 */
169 newmg = cur = head = mg = SvMAGIC(sv);
171 MGVTBL *vtbl = mg->mg_virtual;
173 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
174 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
176 /* guard against magic having been deleted - eg FETCH calling
181 /* Don't restore the flags for this entry if it was deleted. */
182 if (mg->mg_flags & MGf_GSKIP)
183 (SSPTR(mgs_ix, MGS *))->mgs_flags = 0;
186 mg = mg->mg_moremagic;
189 /* Have we finished with the new entries we saw? Start again
190 where we left off (unless there are more new entries). */
198 /* Were any new entries added? */
199 if (!new && (newmg = SvMAGIC(sv)) != head) {
206 restore_magic(aTHX_ INT2PTR(void *, (IV)mgs_ix));
208 if (SvREFCNT(sv) == 1) {
209 /* We hold the last reference to this SV, which implies that the
210 SV was deleted as a side effect of the routines we called. */
219 Do magic after a value is assigned to the SV. See C<sv_magic>.
225 Perl_mg_set(pTHX_ SV *sv)
231 mgs_ix = SSNEW(sizeof(MGS));
232 save_magic(mgs_ix, sv);
234 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
235 MGVTBL* vtbl = mg->mg_virtual;
236 nextmg = mg->mg_moremagic; /* it may delete itself */
237 if (mg->mg_flags & MGf_GSKIP) {
238 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
239 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
241 if (vtbl && vtbl->svt_set)
242 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
245 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
250 =for apidoc mg_length
252 Report on the SV's length. See C<sv_magic>.
258 Perl_mg_length(pTHX_ SV *sv)
263 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
264 MGVTBL* vtbl = mg->mg_virtual;
265 if (vtbl && vtbl->svt_len) {
268 mgs_ix = SSNEW(sizeof(MGS));
269 save_magic(mgs_ix, sv);
270 /* omit MGf_GSKIP -- not changed here */
271 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
272 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
279 U8 *s = (U8*)SvPV(sv, len);
280 len = Perl_utf8_length(aTHX_ s, s + len);
288 Perl_mg_size(pTHX_ SV *sv)
293 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
294 MGVTBL* vtbl = mg->mg_virtual;
295 if (vtbl && vtbl->svt_len) {
298 mgs_ix = SSNEW(sizeof(MGS));
299 save_magic(mgs_ix, sv);
300 /* omit MGf_GSKIP -- not changed here */
301 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
302 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
309 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
314 Perl_croak(aTHX_ "Size magic not implemented");
323 Clear something magical that the SV represents. See C<sv_magic>.
329 Perl_mg_clear(pTHX_ SV *sv)
334 mgs_ix = SSNEW(sizeof(MGS));
335 save_magic(mgs_ix, sv);
337 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
338 MGVTBL* vtbl = mg->mg_virtual;
339 /* omit GSKIP -- never set here */
341 if (vtbl && vtbl->svt_clear)
342 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
345 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
352 Finds the magic pointer for type matching the SV. See C<sv_magic>.
358 Perl_mg_find(pTHX_ SV *sv, int type)
363 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
364 if (mg->mg_type == type)
373 Copies the magic from one SV to another. See C<sv_magic>.
379 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
383 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
384 MGVTBL* vtbl = mg->mg_virtual;
385 if ((mg->mg_flags & MGf_COPY) && vtbl->svt_copy){
386 count += CALL_FPTR(vtbl->svt_copy)(aTHX_ sv, mg, nsv, key, klen);
388 else if (isUPPER(mg->mg_type)) {
390 mg->mg_type == PERL_MAGIC_tied ? SvTIED_obj(sv, mg) :
391 (mg->mg_type == PERL_MAGIC_regdata && mg->mg_obj)
393 toLOWER(mg->mg_type), key, klen);
403 Free any magic storage used by the SV. See C<sv_magic>.
409 Perl_mg_free(pTHX_ SV *sv)
413 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
414 MGVTBL* vtbl = mg->mg_virtual;
415 moremagic = mg->mg_moremagic;
416 if (vtbl && vtbl->svt_free)
417 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
418 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
419 if (mg->mg_len > 0 || mg->mg_type == PERL_MAGIC_utf8)
420 Safefree(mg->mg_ptr);
421 else if (mg->mg_len == HEf_SVKEY)
422 SvREFCNT_dec((SV*)mg->mg_ptr);
424 if (mg->mg_flags & MGf_REFCOUNTED)
425 SvREFCNT_dec(mg->mg_obj);
435 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
439 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
440 if (mg->mg_obj) /* @+ */
443 return rx->lastparen;
450 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
458 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
462 if (paren <= (I32)rx->nparens &&
463 (s = rx->startp[paren]) != -1 &&
464 (t = rx->endp[paren]) != -1)
466 if (mg->mg_obj) /* @+ */
471 if (i > 0 && RX_MATCH_UTF8(rx)) {
472 char *b = rx->subbeg;
474 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
484 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
486 Perl_croak(aTHX_ PL_no_modify);
492 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
499 switch (*mg->mg_ptr) {
500 case '1': case '2': case '3': case '4':
501 case '5': case '6': case '7': case '8': case '9': case '&':
502 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
504 paren = atoi(mg->mg_ptr); /* $& is in [0] */
506 if (paren <= (I32)rx->nparens &&
507 (s1 = rx->startp[paren]) != -1 &&
508 (t1 = rx->endp[paren]) != -1)
512 if (i > 0 && RX_MATCH_UTF8(rx)) {
513 char *s = rx->subbeg + s1;
514 char *send = rx->subbeg + t1;
517 if (is_utf8_string((U8*)s, i))
518 i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send);
521 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
525 if (ckWARN(WARN_UNINITIALIZED))
530 if (ckWARN(WARN_UNINITIALIZED))
535 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
536 paren = rx->lastparen;
541 case '\016': /* ^N */
542 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
543 paren = rx->lastcloseparen;
549 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
550 if (rx->startp[0] != -1) {
561 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
562 if (rx->endp[0] != -1) {
563 i = rx->sublen - rx->endp[0];
574 if (!SvPOK(sv) && SvNIOK(sv)) {
584 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
587 register char *s = NULL;
591 switch (*mg->mg_ptr) {
592 case '\001': /* ^A */
593 sv_setsv(sv, PL_bodytarget);
595 case '\003': /* ^C */
596 sv_setiv(sv, (IV)PL_minus_c);
599 case '\004': /* ^D */
600 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
602 case '\005': /* ^E */
603 if (*(mg->mg_ptr+1) == '\0') {
604 #ifdef MACOS_TRADITIONAL
608 sv_setnv(sv,(double)gMacPerl_OSErr);
609 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
614 # include <descrip.h>
615 # include <starlet.h>
617 $DESCRIPTOR(msgdsc,msg);
618 sv_setnv(sv,(NV) vaxc$errno);
619 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
620 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
626 if (!(_emx_env & 0x200)) { /* Under DOS */
627 sv_setnv(sv, (NV)errno);
628 sv_setpv(sv, errno ? Strerror(errno) : "");
630 if (errno != errno_isOS2) {
631 int tmp = _syserrno();
632 if (tmp) /* 2nd call to _syserrno() makes it 0 */
635 sv_setnv(sv, (NV)Perl_rc);
636 sv_setpv(sv, os2error(Perl_rc));
641 DWORD dwErr = GetLastError();
642 sv_setnv(sv, (NV)dwErr);
645 PerlProc_GetOSError(sv, dwErr);
653 int saveerrno = errno;
654 sv_setnv(sv, (NV)errno);
655 sv_setpv(sv, errno ? Strerror(errno) : "");
662 SvNOK_on(sv); /* what a wonderful hack! */
664 else if (strEQ(mg->mg_ptr+1, "NCODING"))
665 sv_setsv(sv, PL_encoding);
667 case '\006': /* ^F */
668 sv_setiv(sv, (IV)PL_maxsysfd);
670 case '\010': /* ^H */
671 sv_setiv(sv, (IV)PL_hints);
673 case '\011': /* ^I */ /* NOT \t in EBCDIC */
675 sv_setpv(sv, PL_inplace);
677 sv_setsv(sv, &PL_sv_undef);
679 case '\017': /* ^O & ^OPEN */
680 if (*(mg->mg_ptr+1) == '\0') {
681 sv_setpv(sv, PL_osname);
684 else if (strEQ(mg->mg_ptr, "\017PEN")) {
685 if (!PL_compiling.cop_io)
686 sv_setsv(sv, &PL_sv_undef);
688 sv_setsv(sv, PL_compiling.cop_io);
692 case '\020': /* ^P */
693 sv_setiv(sv, (IV)PL_perldb);
695 case '\023': /* ^S */
696 if (*(mg->mg_ptr+1) == '\0') {
697 if (PL_lex_state != LEX_NOTPARSING)
700 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
705 case '\024': /* ^T */
706 if (*(mg->mg_ptr+1) == '\0') {
708 sv_setnv(sv, PL_basetime);
710 sv_setiv(sv, (IV)PL_basetime);
713 else if (strEQ(mg->mg_ptr, "\024AINT"))
714 sv_setiv(sv, PL_tainting
715 ? (PL_taint_warn || PL_unsafe ? -1 : 1)
718 case '\025': /* $^UNICODE, $^UTF8LOCALE */
719 if (strEQ(mg->mg_ptr, "\025NICODE"))
720 sv_setuv(sv, (UV) PL_unicode);
721 else if (strEQ(mg->mg_ptr, "\025TF8LOCALE"))
722 sv_setuv(sv, (UV) PL_utf8locale);
724 case '\027': /* ^W & $^WARNING_BITS */
725 if (*(mg->mg_ptr+1) == '\0')
726 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
727 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
728 if (PL_compiling.cop_warnings == pWARN_NONE ||
729 PL_compiling.cop_warnings == pWARN_STD)
731 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
733 else if (PL_compiling.cop_warnings == pWARN_ALL) {
734 /* Get the bit mask for $warnings::Bits{all}, because
735 * it could have been extended by warnings::register */
737 HV *bits=get_hv("warnings::Bits", FALSE);
738 if (bits && (bits_all=hv_fetch(bits, "all", 3, FALSE))) {
739 sv_setsv(sv, *bits_all);
742 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
746 sv_setsv(sv, PL_compiling.cop_warnings);
751 case '1': case '2': case '3': case '4':
752 case '5': case '6': case '7': case '8': case '9': case '&':
753 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
757 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
758 * XXX Does the new way break anything?
760 paren = atoi(mg->mg_ptr); /* $& is in [0] */
762 if (paren <= (I32)rx->nparens &&
763 (s1 = rx->startp[paren]) != -1 &&
764 (t1 = rx->endp[paren]) != -1)
774 if (RX_MATCH_UTF8(rx) && is_utf8_string((U8*)s, i))
779 if (RX_MATCH_TAINTED(rx)) {
780 MAGIC* mg = SvMAGIC(sv);
783 SvMAGIC(sv) = mg->mg_moremagic;
785 if ((mgt = SvMAGIC(sv))) {
786 mg->mg_moremagic = mgt;
796 sv_setsv(sv,&PL_sv_undef);
799 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
800 paren = rx->lastparen;
804 sv_setsv(sv,&PL_sv_undef);
806 case '\016': /* ^N */
807 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
808 paren = rx->lastcloseparen;
812 sv_setsv(sv,&PL_sv_undef);
815 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
816 if ((s = rx->subbeg) && rx->startp[0] != -1) {
821 sv_setsv(sv,&PL_sv_undef);
824 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
825 if (rx->subbeg && rx->endp[0] != -1) {
826 s = rx->subbeg + rx->endp[0];
827 i = rx->sublen - rx->endp[0];
831 sv_setsv(sv,&PL_sv_undef);
835 if (GvIO(PL_last_in_gv)) {
836 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
842 sv_setiv(sv, (IV)STATUS_CURRENT);
843 #ifdef COMPLEX_STATUS
844 LvTARGOFF(sv) = PL_statusvalue;
845 LvTARGLEN(sv) = PL_statusvalue_vms;
850 if (GvIOp(PL_defoutgv))
851 s = IoTOP_NAME(GvIOp(PL_defoutgv));
855 sv_setpv(sv,GvENAME(PL_defoutgv));
860 if (GvIOp(PL_defoutgv))
861 s = IoFMT_NAME(GvIOp(PL_defoutgv));
863 s = GvENAME(PL_defoutgv);
868 if (GvIOp(PL_defoutgv))
869 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
872 if (GvIOp(PL_defoutgv))
873 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
876 if (GvIOp(PL_defoutgv))
877 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
885 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
888 if (GvIOp(PL_defoutgv))
889 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
895 sv_copypv(sv, PL_ors_sv);
898 sv_setpv(sv,PL_ofmt);
902 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
903 sv_setpv(sv, errno ? Strerror(errno) : "");
906 int saveerrno = errno;
907 sv_setnv(sv, (NV)errno);
909 if (errno == errno_isOS2 || errno == errno_isOS2_set)
910 sv_setpv(sv, os2error(Perl_rc));
913 sv_setpv(sv, errno ? Strerror(errno) : "");
917 SvNOK_on(sv); /* what a wonderful hack! */
920 sv_setiv(sv, (IV)PL_uid);
923 sv_setiv(sv, (IV)PL_euid);
926 sv_setiv(sv, (IV)PL_gid);
928 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
932 sv_setiv(sv, (IV)PL_egid);
934 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
939 Groups_t gary[NGROUPS];
940 i = getgroups(NGROUPS,gary);
942 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
945 (void)SvIOK_on(sv); /* what a wonderful hack! */
947 #ifndef MACOS_TRADITIONAL
956 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
958 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
960 if (uf && uf->uf_val)
961 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
966 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
976 #ifdef DYNAMIC_ENV_FETCH
977 /* We just undefd an environment var. Is a replacement */
978 /* waiting in the wings? */
981 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
982 s = SvPV(*valp, len);
986 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
987 /* And you'll never guess what the dog had */
988 /* in its mouth... */
990 MgTAINTEDDIR_off(mg);
992 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
993 char pathbuf[256], eltbuf[256], *cp, *elt = s;
997 do { /* DCL$PATH may be a search list */
998 while (1) { /* as may dev portion of any element */
999 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
1000 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
1001 cando_by_name(S_IWUSR,0,elt) ) {
1002 MgTAINTEDDIR_on(mg);
1006 if ((cp = strchr(elt, ':')) != Nullch)
1008 if (my_trnlnm(elt, eltbuf, j++))
1014 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
1017 if (s && klen == 4 && strEQ(ptr,"PATH")) {
1018 char *strend = s + len;
1020 while (s < strend) {
1024 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
1025 s, strend, ':', &i);
1027 if (i >= sizeof tmpbuf /* too long -- assume the worst */
1029 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
1030 MgTAINTEDDIR_on(mg);
1036 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
1042 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
1045 my_setenv(MgPV(mg,n_a),Nullch);
1050 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
1053 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1055 if (PL_localizing) {
1058 magic_clear_all_env(sv,mg);
1059 hv_iterinit((HV*)sv);
1060 while ((entry = hv_iternext((HV*)sv))) {
1062 my_setenv(hv_iterkey(entry, &keylen),
1063 SvPV(hv_iterval((HV*)sv, entry), n_a));
1071 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
1074 #if defined(VMS) || defined(EPOC)
1075 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1077 # if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
1080 # ifdef USE_ENVIRON_ARRAY
1081 # if defined(USE_ITHREADS)
1082 /* only the parent thread can clobber the process environment */
1083 if (PL_curinterp == aTHX)
1086 # ifndef PERL_USE_SAFE_PUTENV
1087 if (!PL_use_safe_putenv) {
1090 if (environ == PL_origenviron)
1091 environ = (char**)safesysmalloc(sizeof(char*));
1093 for (i = 0; environ[i]; i++)
1094 safesysfree(environ[i]);
1096 # endif /* PERL_USE_SAFE_PUTENV */
1098 environ[0] = Nullch;
1100 # endif /* USE_ENVIRON_ARRAY */
1101 # endif /* PERL_IMPLICIT_SYS || WIN32 */
1102 #endif /* VMS || EPOC */
1103 #endif /* !PERL_MICRO */
1107 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS)||defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1108 static int sig_handlers_initted = 0;
1110 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1111 static int sig_ignoring[SIG_SIZE]; /* which signals we are ignoring */
1113 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1114 static int sig_defaulting[SIG_SIZE];
1118 #ifdef HAS_SIGPROCMASK
1120 restore_sigmask(pTHX_ SV *save_sv)
1122 sigset_t *ossetp = (sigset_t *) SvPV_nolen( save_sv );
1123 (void)sigprocmask(SIG_SETMASK, ossetp, (sigset_t *)0);
1127 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
1131 /* Are we fetching a signal entry? */
1132 i = whichsig(MgPV(mg,n_a));
1135 sv_setsv(sv,PL_psig_ptr[i]);
1137 Sighandler_t sigstate;
1138 sigstate = rsignal_state(i);
1139 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1140 if (sig_handlers_initted && sig_ignoring[i]) sigstate = SIG_IGN;
1142 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1143 if (sig_handlers_initted && sig_defaulting[i]) sigstate = SIG_DFL;
1145 /* cache state so we don't fetch it again */
1146 if(sigstate == SIG_IGN)
1147 sv_setpv(sv,"IGNORE");
1149 sv_setsv(sv,&PL_sv_undef);
1150 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1157 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
1159 /* XXX Some of this code was copied from Perl_magic_setsig. A little
1160 * refactoring might be in order.
1168 if (strEQ(s,"__DIE__"))
1170 else if (strEQ(s,"__WARN__"))
1173 Perl_croak(aTHX_ "No such hook: %s", s);
1177 SvREFCNT_dec(to_dec);
1182 /* Are we clearing a signal entry? */
1185 #ifdef HAS_SIGPROCMASK
1188 /* Avoid having the signal arrive at a bad time, if possible. */
1191 sigprocmask(SIG_BLOCK, &set, &save);
1193 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1194 SAVEFREESV(save_sv);
1195 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1198 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1199 if (!sig_handlers_initted) Perl_csighandler_init();
1201 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1202 sig_defaulting[i] = 1;
1203 (void)rsignal(i, PL_csighandlerp);
1205 (void)rsignal(i, SIG_DFL);
1207 if(PL_psig_name[i]) {
1208 SvREFCNT_dec(PL_psig_name[i]);
1211 if(PL_psig_ptr[i]) {
1212 to_dec=PL_psig_ptr[i];
1215 SvREFCNT_dec(to_dec);
1225 Perl_raise_signal(pTHX_ int sig)
1227 /* Set a flag to say this signal is pending */
1228 PL_psig_pend[sig]++;
1229 /* And one to say _a_ signal is pending */
1234 Perl_csighandler(int sig)
1236 #ifdef PERL_GET_SIG_CONTEXT
1237 dTHXa(PERL_GET_SIG_CONTEXT);
1241 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1242 (void) rsignal(sig, PL_csighandlerp);
1243 if (sig_ignoring[sig]) return;
1245 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1246 if (sig_defaulting[sig])
1247 #ifdef KILL_BY_SIGPRC
1248 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1253 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
1254 /* Call the perl level handler now--
1255 * with risk we may be in malloc() etc. */
1256 (*PL_sighandlerp)(sig);
1258 Perl_raise_signal(aTHX_ sig);
1261 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1263 Perl_csighandler_init(void)
1266 if (sig_handlers_initted) return;
1268 for (sig = 1; sig < SIG_SIZE; sig++) {
1269 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1271 sig_defaulting[sig] = 1;
1272 (void) rsignal(sig, PL_csighandlerp);
1274 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1275 sig_ignoring[sig] = 0;
1278 sig_handlers_initted = 1;
1283 Perl_despatch_signals(pTHX)
1287 for (sig = 1; sig < SIG_SIZE; sig++) {
1288 if (PL_psig_pend[sig]) {
1289 PERL_BLOCKSIG_ADD(set, sig);
1290 PL_psig_pend[sig] = 0;
1291 PERL_BLOCKSIG_BLOCK(set);
1292 (*PL_sighandlerp)(sig);
1293 PERL_BLOCKSIG_UNBLOCK(set);
1299 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1304 /* Need to be careful with SvREFCNT_dec(), because that can have side
1305 * effects (due to closures). We must make sure that the new disposition
1306 * is in place before it is called.
1310 #ifdef HAS_SIGPROCMASK
1317 if (strEQ(s,"__DIE__"))
1319 else if (strEQ(s,"__WARN__"))
1322 Perl_croak(aTHX_ "No such hook: %s", s);
1330 i = whichsig(s); /* ...no, a brick */
1332 if (ckWARN(WARN_SIGNAL))
1333 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
1336 #ifdef HAS_SIGPROCMASK
1337 /* Avoid having the signal arrive at a bad time, if possible. */
1340 sigprocmask(SIG_BLOCK, &set, &save);
1342 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1343 SAVEFREESV(save_sv);
1344 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1347 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1348 if (!sig_handlers_initted) Perl_csighandler_init();
1350 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1351 sig_ignoring[i] = 0;
1353 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1354 sig_defaulting[i] = 0;
1356 SvREFCNT_dec(PL_psig_name[i]);
1357 to_dec = PL_psig_ptr[i];
1358 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1359 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1360 PL_psig_name[i] = newSVpvn(s, len);
1361 SvREADONLY_on(PL_psig_name[i]);
1363 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1365 (void)rsignal(i, PL_csighandlerp);
1366 #ifdef HAS_SIGPROCMASK
1371 *svp = SvREFCNT_inc(sv);
1373 SvREFCNT_dec(to_dec);
1376 s = SvPV_force(sv,len);
1377 if (strEQ(s,"IGNORE")) {
1379 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1380 sig_ignoring[i] = 1;
1381 (void)rsignal(i, PL_csighandlerp);
1383 (void)rsignal(i, SIG_IGN);
1387 else if (strEQ(s,"DEFAULT") || !*s) {
1389 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1391 sig_defaulting[i] = 1;
1392 (void)rsignal(i, PL_csighandlerp);
1395 (void)rsignal(i, SIG_DFL);
1400 * We should warn if HINT_STRICT_REFS, but without
1401 * access to a known hint bit in a known OP, we can't
1402 * tell whether HINT_STRICT_REFS is in force or not.
1404 if (!strchr(s,':') && !strchr(s,'\''))
1405 sv_insert(sv, 0, 0, "main::", 6);
1407 (void)rsignal(i, PL_csighandlerp);
1409 *svp = SvREFCNT_inc(sv);
1411 #ifdef HAS_SIGPROCMASK
1416 SvREFCNT_dec(to_dec);
1419 #endif /* !PERL_MICRO */
1422 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1424 PL_sub_generation++;
1429 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1431 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1432 PL_amagic_generation++;
1438 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1440 HV *hv = (HV*)LvTARG(sv);
1444 (void) hv_iterinit(hv);
1445 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1448 while (hv_iternext(hv))
1453 sv_setiv(sv, (IV)i);
1458 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1461 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1466 /* caller is responsible for stack switching/cleanup */
1468 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1474 PUSHs(SvTIED_obj(sv, mg));
1477 if (mg->mg_len >= 0)
1478 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1479 else if (mg->mg_len == HEf_SVKEY)
1480 PUSHs((SV*)mg->mg_ptr);
1482 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1483 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1491 return call_method(meth, flags);
1495 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1501 PUSHSTACKi(PERLSI_MAGIC);
1503 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1504 sv_setsv(sv, *PL_stack_sp--);
1514 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1517 mg->mg_flags |= MGf_GSKIP;
1518 magic_methpack(sv,mg,"FETCH");
1523 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1527 PUSHSTACKi(PERLSI_MAGIC);
1528 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1535 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1537 return magic_methpack(sv,mg,"DELETE");
1542 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1549 PUSHSTACKi(PERLSI_MAGIC);
1550 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1551 sv = *PL_stack_sp--;
1552 retval = (U32) SvIV(sv)-1;
1561 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1566 PUSHSTACKi(PERLSI_MAGIC);
1568 XPUSHs(SvTIED_obj(sv, mg));
1570 call_method("CLEAR", G_SCALAR|G_DISCARD);
1578 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1581 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1585 PUSHSTACKi(PERLSI_MAGIC);
1588 PUSHs(SvTIED_obj(sv, mg));
1593 if (call_method(meth, G_SCALAR))
1594 sv_setsv(key, *PL_stack_sp--);
1603 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1605 return magic_methpack(sv,mg,"EXISTS");
1609 Perl_magic_scalarpack(pTHX_ HV *hv, MAGIC *mg)
1612 SV *retval = &PL_sv_undef;
1613 SV *tied = SvTIED_obj((SV*)hv, mg);
1614 HV *pkg = SvSTASH((SV*)SvRV(tied));
1616 if (!gv_fetchmethod_autoload(pkg, "SCALAR", FALSE)) {
1619 /* we are in an iteration so the hash cannot be empty */
1621 /* no xhv_eiter so now use FIRSTKEY */
1622 key = sv_newmortal();
1623 magic_nextpack((SV*)hv, mg, key);
1624 HvEITER(hv) = NULL; /* need to reset iterator */
1625 return SvOK(key) ? &PL_sv_yes : &PL_sv_no;
1628 /* there is a SCALAR method that we can call */
1630 PUSHSTACKi(PERLSI_MAGIC);
1636 if (call_method("SCALAR", G_SCALAR))
1637 retval = *PL_stack_sp--;
1644 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1654 svp = av_fetch(GvAV(gv),
1655 atoi(MgPV(mg,n_a)), FALSE);
1656 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp)))) {
1657 /* set or clear breakpoint in the relevant control op */
1659 o->op_flags |= OPf_SPECIAL;
1661 o->op_flags &= ~OPf_SPECIAL;
1667 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1669 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1674 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1676 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1681 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1683 SV* lsv = LvTARG(sv);
1685 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1686 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1687 if (mg && mg->mg_len >= 0) {
1690 sv_pos_b2u(lsv, &i);
1691 sv_setiv(sv, i + PL_curcop->cop_arybase);
1700 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1702 SV* lsv = LvTARG(sv);
1709 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1710 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1714 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1715 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1717 else if (!SvOK(sv)) {
1721 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1723 pos = SvIV(sv) - PL_curcop->cop_arybase;
1726 ulen = sv_len_utf8(lsv);
1736 else if (pos > (SSize_t)len)
1741 sv_pos_u2b(lsv, &p, 0);
1746 mg->mg_flags &= ~MGf_MINMATCH;
1752 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1754 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1756 gv_efullname3(sv,((GV*)sv), "*");
1760 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1765 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1771 gv = gv_fetchsv(sv,TRUE, SVt_PVGV);
1776 GvGP(sv) = gp_ref(GvGP(gv));
1781 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1784 SV *lsv = LvTARG(sv);
1785 char *tmps = SvPV(lsv,len);
1786 I32 offs = LvTARGOFF(sv);
1787 I32 rem = LvTARGLEN(sv);
1790 sv_pos_u2b(lsv, &offs, &rem);
1791 if (offs > (I32)len)
1793 if (rem + offs > (I32)len)
1795 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1802 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1805 char *tmps = SvPV(sv, len);
1806 SV *lsv = LvTARG(sv);
1807 I32 lvoff = LvTARGOFF(sv);
1808 I32 lvlen = LvTARGLEN(sv);
1811 sv_utf8_upgrade(lsv);
1812 sv_pos_u2b(lsv, &lvoff, &lvlen);
1813 sv_insert(lsv, lvoff, lvlen, tmps, len);
1814 LvTARGLEN(sv) = sv_len_utf8(sv);
1817 else if (lsv && SvUTF8(lsv)) {
1818 sv_pos_u2b(lsv, &lvoff, &lvlen);
1819 LvTARGLEN(sv) = len;
1820 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
1821 sv_insert(lsv, lvoff, lvlen, tmps, len);
1825 sv_insert(lsv, lvoff, lvlen, tmps, len);
1826 LvTARGLEN(sv) = len;
1834 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1836 TAINT_IF((mg->mg_len & 1) ||
1837 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1842 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1844 if (PL_localizing) {
1845 if (PL_localizing == 1)
1850 else if (PL_tainted)
1858 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1860 SV *lsv = LvTARG(sv);
1867 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1872 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1874 do_vecset(sv); /* XXX slurp this routine */
1879 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1882 if (LvTARGLEN(sv)) {
1884 SV *ahv = LvTARG(sv);
1885 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1890 AV* av = (AV*)LvTARG(sv);
1891 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1892 targ = AvARRAY(av)[LvTARGOFF(sv)];
1894 if (targ && targ != &PL_sv_undef) {
1895 /* somebody else defined it for us */
1896 SvREFCNT_dec(LvTARG(sv));
1897 LvTARG(sv) = SvREFCNT_inc(targ);
1899 SvREFCNT_dec(mg->mg_obj);
1900 mg->mg_obj = Nullsv;
1901 mg->mg_flags &= ~MGf_REFCOUNTED;
1906 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1911 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1916 sv_setsv(LvTARG(sv), sv);
1917 SvSETMAGIC(LvTARG(sv));
1923 Perl_vivify_defelem(pTHX_ SV *sv)
1928 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
1931 SV *ahv = LvTARG(sv);
1933 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1936 if (!value || value == &PL_sv_undef)
1937 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1940 AV* av = (AV*)LvTARG(sv);
1941 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1942 LvTARG(sv) = Nullsv; /* array can't be extended */
1944 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1945 if (!svp || (value = *svp) == &PL_sv_undef)
1946 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1949 (void)SvREFCNT_inc(value);
1950 SvREFCNT_dec(LvTARG(sv));
1953 SvREFCNT_dec(mg->mg_obj);
1954 mg->mg_obj = Nullsv;
1955 mg->mg_flags &= ~MGf_REFCOUNTED;
1959 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1961 AV *av = (AV*)mg->mg_obj;
1962 SV **svp = AvARRAY(av);
1963 I32 i = AvFILLp(av);
1966 if (!SvWEAKREF(svp[i]))
1967 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1968 /* XXX Should we check that it hasn't changed? */
1971 SvWEAKREF_off(svp[i]);
1976 SvREFCNT_dec(av); /* remove extra count added by sv_add_backref() */
1981 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1989 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1991 sv_unmagic(sv, PERL_MAGIC_bm);
1997 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1999 sv_unmagic(sv, PERL_MAGIC_fm);
2005 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
2007 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
2009 if (uf && uf->uf_set)
2010 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
2015 Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
2017 sv_unmagic(sv, PERL_MAGIC_qr);
2022 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
2024 regexp *re = (regexp *)mg->mg_obj;
2029 #ifdef USE_LOCALE_COLLATE
2031 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
2034 * RenE<eacute> Descartes said "I think not."
2035 * and vanished with a faint plop.
2038 Safefree(mg->mg_ptr);
2044 #endif /* USE_LOCALE_COLLATE */
2046 /* Just clear the UTF-8 cache data. */
2048 Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
2050 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
2052 mg->mg_len = -1; /* The mg_len holds the len cache. */
2057 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
2062 switch (*mg->mg_ptr) {
2063 case '\001': /* ^A */
2064 sv_setsv(PL_bodytarget, sv);
2066 case '\003': /* ^C */
2067 PL_minus_c = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2070 case '\004': /* ^D */
2073 PL_debug = get_debug_opts(&s, 0) | DEBUG_TOP_FLAG;
2074 DEBUG_x(dump_all());
2076 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
2079 case '\005': /* ^E */
2080 if (*(mg->mg_ptr+1) == '\0') {
2081 #ifdef MACOS_TRADITIONAL
2082 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2085 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2088 SetLastError( SvIV(sv) );
2091 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2093 /* will anyone ever use this? */
2094 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
2100 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
2102 SvREFCNT_dec(PL_encoding);
2103 if (SvOK(sv) || SvGMAGICAL(sv)) {
2104 PL_encoding = newSVsv(sv);
2107 PL_encoding = Nullsv;
2111 case '\006': /* ^F */
2112 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2114 case '\010': /* ^H */
2115 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2117 case '\011': /* ^I */ /* NOT \t in EBCDIC */
2119 Safefree(PL_inplace);
2121 PL_inplace = savesvpv(sv);
2123 PL_inplace = Nullch;
2125 case '\017': /* ^O */
2126 if (*(mg->mg_ptr+1) == '\0') {
2128 Safefree(PL_osname);
2132 TAINT_PROPER("assigning to $^O");
2133 PL_osname = savesvpv(sv);
2136 else if (strEQ(mg->mg_ptr, "\017PEN")) {
2137 if (!PL_compiling.cop_io)
2138 PL_compiling.cop_io = newSVsv(sv);
2140 sv_setsv(PL_compiling.cop_io,sv);
2143 case '\020': /* ^P */
2144 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2145 if ((PERLDB_SUB || PERLDB_LINE || PERLDB_SUBLINE || PERLDB_ASSERTION)
2149 case '\024': /* ^T */
2151 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
2153 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2156 case '\027': /* ^W & $^WARNING_BITS */
2157 if (*(mg->mg_ptr+1) == '\0') {
2158 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2159 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2160 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
2161 | (i ? G_WARN_ON : G_WARN_OFF) ;
2164 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
2165 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2166 if (!SvPOK(sv) && PL_localizing) {
2167 sv_setpvn(sv, WARN_NONEstring, WARNsize);
2168 PL_compiling.cop_warnings = pWARN_NONE;
2173 int accumulate = 0 ;
2174 int any_fatals = 0 ;
2175 char * ptr = (char*)SvPV(sv, len) ;
2176 for (i = 0 ; i < len ; ++i) {
2177 accumulate |= ptr[i] ;
2178 any_fatals |= (ptr[i] & 0xAA) ;
2181 PL_compiling.cop_warnings = pWARN_NONE;
2182 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
2183 PL_compiling.cop_warnings = pWARN_ALL;
2184 PL_dowarn |= G_WARN_ONCE ;
2187 if (specialWARN(PL_compiling.cop_warnings))
2188 PL_compiling.cop_warnings = newSVsv(sv) ;
2190 sv_setsv(PL_compiling.cop_warnings, sv);
2191 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2192 PL_dowarn |= G_WARN_ONCE ;
2200 if (PL_localizing) {
2201 if (PL_localizing == 1)
2202 SAVESPTR(PL_last_in_gv);
2204 else if (SvOK(sv) && GvIO(PL_last_in_gv))
2205 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
2208 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2209 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savesvpv(sv);
2210 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv,TRUE, SVt_PVIO);
2213 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2214 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savesvpv(sv);
2215 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv,TRUE, SVt_PVIO);
2218 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2221 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2222 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2223 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
2226 IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2230 IO *io = GvIOp(PL_defoutgv);
2233 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
2234 IoFLAGS(io) &= ~IOf_FLUSH;
2236 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2237 PerlIO *ofp = IoOFP(io);
2239 (void)PerlIO_flush(ofp);
2240 IoFLAGS(io) |= IOf_FLUSH;
2246 SvREFCNT_dec(PL_rs);
2247 PL_rs = newSVsv(sv);
2251 SvREFCNT_dec(PL_ors_sv);
2252 if (SvOK(sv) || SvGMAGICAL(sv)) {
2253 PL_ors_sv = newSVsv(sv);
2261 SvREFCNT_dec(PL_ofs_sv);
2262 if (SvOK(sv) || SvGMAGICAL(sv)) {
2263 PL_ofs_sv = newSVsv(sv);
2272 PL_ofmt = savesvpv(sv);
2275 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2278 #ifdef COMPLEX_STATUS
2279 if (PL_localizing == 2) {
2280 PL_statusvalue = LvTARGOFF(sv);
2281 PL_statusvalue_vms = LvTARGLEN(sv);
2285 #ifdef VMSISH_STATUS
2287 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
2290 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2295 # define PERL_VMS_BANG vaxc$errno
2297 # define PERL_VMS_BANG 0
2299 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
2300 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
2304 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2305 if (PL_delaymagic) {
2306 PL_delaymagic |= DM_RUID;
2307 break; /* don't do magic till later */
2310 (void)setruid((Uid_t)PL_uid);
2313 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
2315 #ifdef HAS_SETRESUID
2316 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
2318 if (PL_uid == PL_euid) { /* special case $< = $> */
2320 /* workaround for Darwin's setuid peculiarity, cf [perl #24122] */
2321 if (PL_uid != 0 && PerlProc_getuid() == 0)
2322 (void)PerlProc_setuid(0);
2324 (void)PerlProc_setuid(PL_uid);
2326 PL_uid = PerlProc_getuid();
2327 Perl_croak(aTHX_ "setruid() not implemented");
2332 PL_uid = PerlProc_getuid();
2333 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2336 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2337 if (PL_delaymagic) {
2338 PL_delaymagic |= DM_EUID;
2339 break; /* don't do magic till later */
2342 (void)seteuid((Uid_t)PL_euid);
2345 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
2347 #ifdef HAS_SETRESUID
2348 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
2350 if (PL_euid == PL_uid) /* special case $> = $< */
2351 PerlProc_setuid(PL_euid);
2353 PL_euid = PerlProc_geteuid();
2354 Perl_croak(aTHX_ "seteuid() not implemented");
2359 PL_euid = PerlProc_geteuid();
2360 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2363 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2364 if (PL_delaymagic) {
2365 PL_delaymagic |= DM_RGID;
2366 break; /* don't do magic till later */
2369 (void)setrgid((Gid_t)PL_gid);
2372 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
2374 #ifdef HAS_SETRESGID
2375 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2377 if (PL_gid == PL_egid) /* special case $( = $) */
2378 (void)PerlProc_setgid(PL_gid);
2380 PL_gid = PerlProc_getgid();
2381 Perl_croak(aTHX_ "setrgid() not implemented");
2386 PL_gid = PerlProc_getgid();
2387 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2390 #ifdef HAS_SETGROUPS
2392 char *p = SvPV(sv, len);
2393 Groups_t gary[NGROUPS];
2398 for (i = 0; i < NGROUPS; ++i) {
2399 while (*p && !isSPACE(*p))
2408 (void)setgroups(i, gary);
2410 #else /* HAS_SETGROUPS */
2411 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2412 #endif /* HAS_SETGROUPS */
2413 if (PL_delaymagic) {
2414 PL_delaymagic |= DM_EGID;
2415 break; /* don't do magic till later */
2418 (void)setegid((Gid_t)PL_egid);
2421 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2423 #ifdef HAS_SETRESGID
2424 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2426 if (PL_egid == PL_gid) /* special case $) = $( */
2427 (void)PerlProc_setgid(PL_egid);
2429 PL_egid = PerlProc_getegid();
2430 Perl_croak(aTHX_ "setegid() not implemented");
2435 PL_egid = PerlProc_getegid();
2436 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2439 PL_chopset = SvPV_force(sv,len);
2441 #ifndef MACOS_TRADITIONAL
2443 LOCK_DOLLARZERO_MUTEX;
2444 #ifdef HAS_SETPROCTITLE
2445 /* The BSDs don't show the argv[] in ps(1) output, they
2446 * show a string from the process struct and provide
2447 * the setproctitle() routine to manipulate that. */
2450 # if __FreeBSD_version > 410001
2451 /* The leading "-" removes the "perl: " prefix,
2452 * but not the "(perl) suffix from the ps(1)
2453 * output, because that's what ps(1) shows if the
2454 * argv[] is modified. */
2455 setproctitle("-%s", s);
2456 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2457 /* This doesn't really work if you assume that
2458 * $0 = 'foobar'; will wipe out 'perl' from the $0
2459 * because in ps(1) output the result will be like
2460 * sprintf("perl: %s (perl)", s)
2461 * I guess this is a security feature:
2462 * one (a user process) cannot get rid of the original name.
2464 setproctitle("%s", s);
2468 #if defined(__hpux) && defined(PSTAT_SETCMD)
2473 pstat(PSTAT_SETCMD, un, len, 0, 0);
2476 /* PL_origalen is set in perl_parse(). */
2477 s = SvPV_force(sv,len);
2478 if (len >= (STRLEN)PL_origalen-1) {
2479 /* Longer than original, will be truncated. We assume that
2480 * PL_origalen bytes are available. */
2481 Copy(s, PL_origargv[0], PL_origalen-1, char);
2484 /* Shorter than original, will be padded. */
2485 Copy(s, PL_origargv[0], len, char);
2486 PL_origargv[0][len] = 0;
2487 memset(PL_origargv[0] + len + 1,
2488 /* Is the space counterintuitive? Yes.
2489 * (You were expecting \0?)
2490 * Does it work? Seems to. (In Linux 2.4.20 at least.)
2493 PL_origalen - len - 1);
2495 PL_origargv[0][PL_origalen-1] = 0;
2496 for (i = 1; i < PL_origargc; i++)
2498 UNLOCK_DOLLARZERO_MUTEX;
2506 Perl_whichsig(pTHX_ char *sig)
2508 register char **sigv;
2510 for (sigv = PL_sig_name; *sigv; sigv++)
2511 if (strEQ(sig,*sigv))
2512 return PL_sig_num[sigv - PL_sig_name];
2514 if (strEQ(sig,"CHLD"))
2518 if (strEQ(sig,"CLD"))
2524 #if !defined(PERL_IMPLICIT_CONTEXT)
2529 Perl_sighandler(int sig)
2531 #ifdef PERL_GET_SIG_CONTEXT
2532 dTHXa(PERL_GET_SIG_CONTEXT);
2539 SV *sv = Nullsv, *tSv = PL_Sv;
2545 if (PL_savestack_ix + 15 <= PL_savestack_max)
2547 if (PL_markstack_ptr < PL_markstack_max - 2)
2549 if (PL_scopestack_ix < PL_scopestack_max - 3)
2552 if (!PL_psig_ptr[sig]) {
2553 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
2558 /* Max number of items pushed there is 3*n or 4. We cannot fix
2559 infinity, so we fix 4 (in fact 5): */
2561 PL_savestack_ix += 5; /* Protect save in progress. */
2562 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2565 PL_markstack_ptr++; /* Protect mark. */
2567 PL_scopestack_ix += 1;
2568 /* sv_2cv is too complicated, try a simpler variant first: */
2569 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2570 || SvTYPE(cv) != SVt_PVCV)
2571 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2573 if (!cv || !CvROOT(cv)) {
2574 if (ckWARN(WARN_SIGNAL))
2575 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
2576 PL_sig_name[sig], (gv ? GvENAME(gv)
2583 if(PL_psig_name[sig]) {
2584 sv = SvREFCNT_inc(PL_psig_name[sig]);
2586 #if !defined(PERL_IMPLICIT_CONTEXT)
2590 sv = sv_newmortal();
2591 sv_setpv(sv,PL_sig_name[sig]);
2594 PUSHSTACKi(PERLSI_SIGNAL);
2599 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2602 if (SvTRUE(ERRSV)) {
2604 #ifdef HAS_SIGPROCMASK
2605 /* Handler "died", for example to get out of a restart-able read().
2606 * Before we re-do that on its behalf re-enable the signal which was
2607 * blocked by the system when we entered.
2611 sigaddset(&set,sig);
2612 sigprocmask(SIG_UNBLOCK, &set, NULL);
2614 /* Not clear if this will work */
2615 (void)rsignal(sig, SIG_IGN);
2616 (void)rsignal(sig, PL_csighandlerp);
2618 #endif /* !PERL_MICRO */
2623 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2627 PL_scopestack_ix -= 1;
2630 PL_op = myop; /* Apparently not needed... */
2632 PL_Sv = tSv; /* Restore global temporaries. */
2639 restore_magic(pTHX_ void *p)
2641 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2642 SV* sv = mgs->mgs_sv;
2647 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2649 #ifdef PERL_COPY_ON_WRITE
2650 /* While magic was saved (and off) sv_setsv may well have seen
2651 this SV as a prime candidate for COW. */
2653 sv_force_normal(sv);
2657 SvFLAGS(sv) |= mgs->mgs_flags;
2661 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2664 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2666 /* If we're still on top of the stack, pop us off. (That condition
2667 * will be satisfied if restore_magic was called explicitly, but *not*
2668 * if it's being called via leave_scope.)
2669 * The reason for doing this is that otherwise, things like sv_2cv()
2670 * may leave alloc gunk on the savestack, and some code
2671 * (e.g. sighandler) doesn't expect that...
2673 if (PL_savestack_ix == mgs->mgs_ss_ix)
2675 I32 popval = SSPOPINT;
2676 assert(popval == SAVEt_DESTRUCTOR_X);
2677 PL_savestack_ix -= 2;
2679 assert(popval == SAVEt_ALLOC);
2681 PL_savestack_ix -= popval;
2687 unwind_handler_stack(pTHX_ void *p)
2689 U32 flags = *(U32*)p;
2692 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2693 /* cxstack_ix-- Not needed, die already unwound it. */
2694 #if !defined(PERL_IMPLICIT_CONTEXT)
2696 SvREFCNT_dec(sig_sv);