3 * Copyright (c) 1991-2003, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "Sam sat on the ground and put his head in his hands. 'I wish I had never
12 * come here, and I don't want to see no more magic,' he said, and fell silent."
16 =head1 Magical Functions
23 #if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
33 # include <sys/pstat.h>
36 Signal_t Perl_csighandler(int sig);
38 /* if you only have signal() and it resets on each signal, FAKE_PERSISTENT_SIGNAL_HANDLERS fixes */
39 #if !defined(HAS_SIGACTION) && defined(VMS)
40 # define FAKE_PERSISTENT_SIGNAL_HANDLERS
42 /* if we're doing kill() with sys$sigprc on VMS, FAKE_DEFAULT_SIGNAL_HANDLERS */
43 #if defined(KILL_BY_SIGPRC)
44 # define FAKE_DEFAULT_SIGNAL_HANDLERS
47 static void restore_magic(pTHX_ void *p);
48 static void unwind_handler_stack(pTHX_ void *p);
51 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
59 /* MGS is typedef'ed to struct magic_state in perl.h */
62 S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
65 assert(SvMAGICAL(sv));
66 #ifdef PERL_COPY_ON_WRITE
67 /* Turning READONLY off for a copy-on-write scalar is a bad idea. */
72 SAVEDESTRUCTOR_X(restore_magic, INT2PTR(void*, (IV)mgs_ix));
74 mgs = SSPTR(mgs_ix, MGS*);
76 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
77 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
81 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
85 =for apidoc mg_magical
87 Turns on the magical status of an SV. See C<sv_magic>.
93 Perl_mg_magical(pTHX_ SV *sv)
96 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
97 MGVTBL* vtbl = mg->mg_virtual;
99 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
103 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
112 Do magic after a value is retrieved from the SV. See C<sv_magic>.
118 Perl_mg_get(pTHX_ SV *sv)
121 MAGIC *newmg, *head, *cur, *mg;
122 I32 mgs_ix = SSNEW(sizeof(MGS));
124 save_magic(mgs_ix, sv);
126 /* We must call svt_get(sv, mg) for each valid entry in the linked
127 list of magic. svt_get() may delete the current entry, add new
128 magic to the head of the list, or upgrade the SV. AMS 20010810 */
130 newmg = cur = head = mg = SvMAGIC(sv);
132 MGVTBL *vtbl = mg->mg_virtual;
134 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
135 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
137 /* guard against sv having been freed */
138 if (SvTYPE(sv) == SVTYPEMASK) {
139 Perl_croak(aTHX_ "Tied variable freed while still in use");
142 /* Don't restore the flags for this entry if it was deleted. */
143 if (mg->mg_flags & MGf_GSKIP)
144 (SSPTR(mgs_ix, MGS *))->mgs_flags = 0;
147 mg = mg->mg_moremagic;
150 /* Have we finished with the new entries we saw? Start again
151 where we left off (unless there are more new entries). */
159 /* Were any new entries added? */
160 if (!new && (newmg = SvMAGIC(sv)) != head) {
167 restore_magic(aTHX_ INT2PTR(void *, (IV)mgs_ix));
174 Do magic after a value is assigned to the SV. See C<sv_magic>.
180 Perl_mg_set(pTHX_ SV *sv)
186 mgs_ix = SSNEW(sizeof(MGS));
187 save_magic(mgs_ix, sv);
189 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
190 MGVTBL* vtbl = mg->mg_virtual;
191 nextmg = mg->mg_moremagic; /* it may delete itself */
192 if (mg->mg_flags & MGf_GSKIP) {
193 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
194 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
196 if (vtbl && vtbl->svt_set)
197 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
200 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
205 =for apidoc mg_length
207 Report on the SV's length. See C<sv_magic>.
213 Perl_mg_length(pTHX_ SV *sv)
218 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
219 MGVTBL* vtbl = mg->mg_virtual;
220 if (vtbl && vtbl->svt_len) {
223 mgs_ix = SSNEW(sizeof(MGS));
224 save_magic(mgs_ix, sv);
225 /* omit MGf_GSKIP -- not changed here */
226 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
227 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
234 U8 *s = (U8*)SvPV(sv, len);
235 len = Perl_utf8_length(aTHX_ s, s + len);
243 Perl_mg_size(pTHX_ SV *sv)
248 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
249 MGVTBL* vtbl = mg->mg_virtual;
250 if (vtbl && vtbl->svt_len) {
253 mgs_ix = SSNEW(sizeof(MGS));
254 save_magic(mgs_ix, sv);
255 /* omit MGf_GSKIP -- not changed here */
256 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
257 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
264 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
269 Perl_croak(aTHX_ "Size magic not implemented");
278 Clear something magical that the SV represents. See C<sv_magic>.
284 Perl_mg_clear(pTHX_ SV *sv)
289 mgs_ix = SSNEW(sizeof(MGS));
290 save_magic(mgs_ix, sv);
292 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
293 MGVTBL* vtbl = mg->mg_virtual;
294 /* omit GSKIP -- never set here */
296 if (vtbl && vtbl->svt_clear)
297 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
300 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
307 Finds the magic pointer for type matching the SV. See C<sv_magic>.
313 Perl_mg_find(pTHX_ SV *sv, int type)
318 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
319 if (mg->mg_type == type)
328 Copies the magic from one SV to another. See C<sv_magic>.
334 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
338 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
339 MGVTBL* vtbl = mg->mg_virtual;
340 if ((mg->mg_flags & MGf_COPY) && vtbl->svt_copy){
341 count += CALL_FPTR(vtbl->svt_copy)(aTHX_ sv, mg, nsv, key, klen);
343 else if (isUPPER(mg->mg_type)) {
345 mg->mg_type == PERL_MAGIC_tied ? SvTIED_obj(sv, mg) :
346 (mg->mg_type == PERL_MAGIC_regdata && mg->mg_obj)
348 toLOWER(mg->mg_type), key, klen);
358 Free any magic storage used by the SV. See C<sv_magic>.
364 Perl_mg_free(pTHX_ SV *sv)
368 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
369 MGVTBL* vtbl = mg->mg_virtual;
370 moremagic = mg->mg_moremagic;
371 if (vtbl && vtbl->svt_free)
372 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
373 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
375 Safefree(mg->mg_ptr);
376 else if (mg->mg_len == HEf_SVKEY)
377 SvREFCNT_dec((SV*)mg->mg_ptr);
379 if (mg->mg_flags & MGf_REFCOUNTED)
380 SvREFCNT_dec(mg->mg_obj);
388 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
393 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
397 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
398 if (mg->mg_obj) /* @+ */
401 return rx->lastparen;
408 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
416 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
420 if (paren <= (I32)rx->nparens &&
421 (s = rx->startp[paren]) != -1 &&
422 (t = rx->endp[paren]) != -1)
424 if (mg->mg_obj) /* @+ */
429 if (i > 0 && RX_MATCH_UTF8(rx)) {
430 char *b = rx->subbeg;
432 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
442 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
444 Perl_croak(aTHX_ PL_no_modify);
450 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
457 switch (*mg->mg_ptr) {
458 case '1': case '2': case '3': case '4':
459 case '5': case '6': case '7': case '8': case '9': case '&':
460 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
462 paren = atoi(mg->mg_ptr); /* $& is in [0] */
464 if (paren <= (I32)rx->nparens &&
465 (s1 = rx->startp[paren]) != -1 &&
466 (t1 = rx->endp[paren]) != -1)
470 if (i > 0 && RX_MATCH_UTF8(rx)) {
471 char *s = rx->subbeg + s1;
472 char *send = rx->subbeg + t1;
475 if (is_utf8_string((U8*)s, i))
476 i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send);
479 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
483 if (ckWARN(WARN_UNINITIALIZED))
488 if (ckWARN(WARN_UNINITIALIZED))
493 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
494 paren = rx->lastparen;
499 case '\016': /* ^N */
500 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
501 paren = rx->lastcloseparen;
507 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
508 if (rx->startp[0] != -1) {
519 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
520 if (rx->endp[0] != -1) {
521 i = rx->sublen - rx->endp[0];
532 if (!SvPOK(sv) && SvNIOK(sv)) {
542 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
549 switch (*mg->mg_ptr) {
550 case '\001': /* ^A */
551 sv_setsv(sv, PL_bodytarget);
553 case '\003': /* ^C */
554 sv_setiv(sv, (IV)PL_minus_c);
557 case '\004': /* ^D */
558 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
559 #if defined(YYDEBUG) && defined(DEBUGGING)
560 PL_yydebug = DEBUG_p_TEST;
563 case '\005': /* ^E */
564 if (*(mg->mg_ptr+1) == '\0') {
565 #ifdef MACOS_TRADITIONAL
569 sv_setnv(sv,(double)gMacPerl_OSErr);
570 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
575 # include <descrip.h>
576 # include <starlet.h>
578 $DESCRIPTOR(msgdsc,msg);
579 sv_setnv(sv,(NV) vaxc$errno);
580 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
581 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
587 if (!(_emx_env & 0x200)) { /* Under DOS */
588 sv_setnv(sv, (NV)errno);
589 sv_setpv(sv, errno ? Strerror(errno) : "");
591 if (errno != errno_isOS2) {
592 int tmp = _syserrno();
593 if (tmp) /* 2nd call to _syserrno() makes it 0 */
596 sv_setnv(sv, (NV)Perl_rc);
597 sv_setpv(sv, os2error(Perl_rc));
602 DWORD dwErr = GetLastError();
603 sv_setnv(sv, (NV)dwErr);
606 PerlProc_GetOSError(sv, dwErr);
613 sv_setnv(sv, (NV)errno);
614 sv_setpv(sv, errno ? Strerror(errno) : "");
619 SvNOK_on(sv); /* what a wonderful hack! */
621 else if (strEQ(mg->mg_ptr+1, "NCODING"))
622 sv_setsv(sv, PL_encoding);
624 case '\006': /* ^F */
625 sv_setiv(sv, (IV)PL_maxsysfd);
627 case '\010': /* ^H */
628 sv_setiv(sv, (IV)PL_hints);
630 case '\011': /* ^I */ /* NOT \t in EBCDIC */
632 sv_setpv(sv, PL_inplace);
634 sv_setsv(sv, &PL_sv_undef);
636 case '\017': /* ^O & ^OPEN */
637 if (*(mg->mg_ptr+1) == '\0')
638 sv_setpv(sv, PL_osname);
639 else if (strEQ(mg->mg_ptr, "\017PEN")) {
640 if (!PL_compiling.cop_io)
641 sv_setsv(sv, &PL_sv_undef);
643 sv_setsv(sv, PL_compiling.cop_io);
647 case '\020': /* ^P */
648 sv_setiv(sv, (IV)PL_perldb);
650 case '\023': /* ^S */
651 if (*(mg->mg_ptr+1) == '\0') {
652 if (PL_lex_state != LEX_NOTPARSING)
655 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
660 case '\024': /* ^T */
661 if (*(mg->mg_ptr+1) == '\0') {
663 sv_setnv(sv, PL_basetime);
665 sv_setiv(sv, (IV)PL_basetime);
668 else if (strEQ(mg->mg_ptr, "\024AINT"))
669 sv_setiv(sv, PL_tainting
670 ? (PL_taint_warn || PL_unsafe ? -1 : 1)
673 case '\025': /* $^UNICODE */
674 if (strEQ(mg->mg_ptr, "\025NICODE"))
675 sv_setuv(sv, (UV) PL_unicode);
677 case '\027': /* ^W & $^WARNING_BITS */
678 if (*(mg->mg_ptr+1) == '\0')
679 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
680 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
681 if (PL_compiling.cop_warnings == pWARN_NONE ||
682 PL_compiling.cop_warnings == pWARN_STD)
684 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
686 else if (PL_compiling.cop_warnings == pWARN_ALL) {
687 /* Get the bit mask for $warnings::Bits{all}, because
688 * it could have been extended by warnings::register */
690 HV *bits=get_hv("warnings::Bits", FALSE);
691 if (bits && (bits_all=hv_fetch(bits, "all", 3, FALSE))) {
692 sv_setsv(sv, *bits_all);
695 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
699 sv_setsv(sv, PL_compiling.cop_warnings);
704 case '1': case '2': case '3': case '4':
705 case '5': case '6': case '7': case '8': case '9': case '&':
706 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
710 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
711 * XXX Does the new way break anything?
713 paren = atoi(mg->mg_ptr); /* $& is in [0] */
715 if (paren <= (I32)rx->nparens &&
716 (s1 = rx->startp[paren]) != -1 &&
717 (t1 = rx->endp[paren]) != -1)
727 if (RX_MATCH_UTF8(rx) && is_utf8_string((U8*)s, i))
732 if (RX_MATCH_TAINTED(rx)) {
733 MAGIC* mg = SvMAGIC(sv);
736 SvMAGIC(sv) = mg->mg_moremagic;
738 if ((mgt = SvMAGIC(sv))) {
739 mg->mg_moremagic = mgt;
749 sv_setsv(sv,&PL_sv_undef);
752 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
753 paren = rx->lastparen;
757 sv_setsv(sv,&PL_sv_undef);
759 case '\016': /* ^N */
760 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
761 paren = rx->lastcloseparen;
765 sv_setsv(sv,&PL_sv_undef);
768 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
769 if ((s = rx->subbeg) && rx->startp[0] != -1) {
774 sv_setsv(sv,&PL_sv_undef);
777 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
778 if (rx->subbeg && rx->endp[0] != -1) {
779 s = rx->subbeg + rx->endp[0];
780 i = rx->sublen - rx->endp[0];
784 sv_setsv(sv,&PL_sv_undef);
788 if (GvIO(PL_last_in_gv)) {
789 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
795 sv_setiv(sv, (IV)STATUS_CURRENT);
796 #ifdef COMPLEX_STATUS
797 LvTARGOFF(sv) = PL_statusvalue;
798 LvTARGLEN(sv) = PL_statusvalue_vms;
803 s = IoTOP_NAME(GvIOp(PL_defoutgv));
807 sv_setpv(sv,GvENAME(PL_defoutgv));
812 s = IoFMT_NAME(GvIOp(PL_defoutgv));
814 s = GvENAME(PL_defoutgv);
819 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
822 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
825 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
833 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
836 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
842 sv_copypv(sv, PL_ors_sv);
845 sv_setpv(sv,PL_ofmt);
849 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
850 sv_setpv(sv, errno ? Strerror(errno) : "");
853 int saveerrno = errno;
854 sv_setnv(sv, (NV)errno);
856 if (errno == errno_isOS2 || errno == errno_isOS2_set)
857 sv_setpv(sv, os2error(Perl_rc));
860 sv_setpv(sv, errno ? Strerror(errno) : "");
864 SvNOK_on(sv); /* what a wonderful hack! */
867 sv_setiv(sv, (IV)PL_uid);
870 sv_setiv(sv, (IV)PL_euid);
873 sv_setiv(sv, (IV)PL_gid);
875 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
879 sv_setiv(sv, (IV)PL_egid);
881 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
886 Groups_t gary[NGROUPS];
887 i = getgroups(NGROUPS,gary);
889 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
892 (void)SvIOK_on(sv); /* what a wonderful hack! */
896 #ifndef MACOS_TRADITIONAL
905 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
907 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
909 if (uf && uf->uf_val)
910 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
915 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
925 #ifdef DYNAMIC_ENV_FETCH
926 /* We just undefd an environment var. Is a replacement */
927 /* waiting in the wings? */
930 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
931 s = SvPV(*valp, len);
935 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
936 /* And you'll never guess what the dog had */
937 /* in its mouth... */
939 MgTAINTEDDIR_off(mg);
941 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
942 char pathbuf[256], eltbuf[256], *cp, *elt = s;
946 do { /* DCL$PATH may be a search list */
947 while (1) { /* as may dev portion of any element */
948 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
949 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
950 cando_by_name(S_IWUSR,0,elt) ) {
955 if ((cp = strchr(elt, ':')) != Nullch)
957 if (my_trnlnm(elt, eltbuf, j++))
963 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
966 if (s && klen == 4 && strEQ(ptr,"PATH")) {
967 char *strend = s + len;
973 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
976 if (i >= sizeof tmpbuf /* too long -- assume the worst */
978 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
985 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
991 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
994 my_setenv(MgPV(mg,n_a),Nullch);
999 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
1002 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1004 if (PL_localizing) {
1007 magic_clear_all_env(sv,mg);
1008 hv_iterinit((HV*)sv);
1009 while ((entry = hv_iternext((HV*)sv))) {
1011 my_setenv(hv_iterkey(entry, &keylen),
1012 SvPV(hv_iterval((HV*)sv, entry), n_a));
1020 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
1022 #if defined(VMS) || defined(EPOC)
1023 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1025 # if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
1028 # ifdef USE_ENVIRON_ARRAY
1029 # if defined(USE_ITHREADS)
1030 /* only the parent thread can clobber the process environment */
1031 if (PL_curinterp == aTHX)
1034 # ifndef PERL_USE_SAFE_PUTENV
1037 if (environ == PL_origenviron)
1038 environ = (char**)safesysmalloc(sizeof(char*));
1040 for (i = 0; environ[i]; i++)
1041 safesysfree(environ[i]);
1042 # endif /* PERL_USE_SAFE_PUTENV */
1044 environ[0] = Nullch;
1046 # endif /* USE_ENVIRON_ARRAY */
1047 # endif /* PERL_IMPLICIT_SYS || WIN32 */
1048 #endif /* VMS || EPC */
1052 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS)||defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1053 static int sig_handlers_initted = 0;
1055 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1056 static int sig_ignoring[SIG_SIZE]; /* which signals we are ignoring */
1058 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1059 static int sig_defaulting[SIG_SIZE];
1063 #ifdef HAS_SIGPROCMASK
1065 restore_sigmask(pTHX_ SV *save_sv)
1067 sigset_t *ossetp = (sigset_t *) SvPV_nolen( save_sv );
1068 (void)sigprocmask(SIG_SETMASK, ossetp, (sigset_t *)0);
1072 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
1076 /* Are we fetching a signal entry? */
1077 i = whichsig(MgPV(mg,n_a));
1080 sv_setsv(sv,PL_psig_ptr[i]);
1082 Sighandler_t sigstate;
1083 sigstate = rsignal_state(i);
1084 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1085 if (sig_handlers_initted && sig_ignoring[i]) sigstate = SIG_IGN;
1087 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1088 if (sig_handlers_initted && sig_defaulting[i]) sigstate = SIG_DFL;
1090 /* cache state so we don't fetch it again */
1091 if(sigstate == SIG_IGN)
1092 sv_setpv(sv,"IGNORE");
1094 sv_setsv(sv,&PL_sv_undef);
1095 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1102 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
1104 /* XXX Some of this code was copied from Perl_magic_setsig. A little
1105 * refactoring might be in order.
1113 if (strEQ(s,"__DIE__"))
1115 else if (strEQ(s,"__WARN__"))
1118 Perl_croak(aTHX_ "No such hook: %s", s);
1122 SvREFCNT_dec(to_dec);
1127 /* Are we clearing a signal entry? */
1130 #ifdef HAS_SIGPROCMASK
1133 /* Avoid having the signal arrive at a bad time, if possible. */
1136 sigprocmask(SIG_BLOCK, &set, &save);
1138 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1139 SAVEFREESV(save_sv);
1140 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1143 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1144 if (!sig_handlers_initted) Perl_csighandler_init();
1146 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1147 sig_defaulting[i] = 1;
1148 (void)rsignal(i, &Perl_csighandler);
1150 (void)rsignal(i, SIG_DFL);
1152 if(PL_psig_name[i]) {
1153 SvREFCNT_dec(PL_psig_name[i]);
1156 if(PL_psig_ptr[i]) {
1157 to_dec=PL_psig_ptr[i];
1160 SvREFCNT_dec(to_dec);
1170 Perl_raise_signal(pTHX_ int sig)
1172 /* Set a flag to say this signal is pending */
1173 PL_psig_pend[sig]++;
1174 /* And one to say _a_ signal is pending */
1179 Perl_csighandler(int sig)
1181 #ifdef PERL_GET_SIG_CONTEXT
1182 dTHXa(PERL_GET_SIG_CONTEXT);
1186 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1187 (void) rsignal(sig, &Perl_csighandler);
1188 if (sig_ignoring[sig]) return;
1190 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1191 if (sig_defaulting[sig])
1192 #ifdef KILL_BY_SIGPRC
1193 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1198 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
1199 /* Call the perl level handler now--
1200 * with risk we may be in malloc() etc. */
1201 (*PL_sighandlerp)(sig);
1203 Perl_raise_signal(aTHX_ sig);
1206 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1208 Perl_csighandler_init(void)
1211 if (sig_handlers_initted) return;
1213 for (sig = 1; sig < SIG_SIZE; sig++) {
1214 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1216 sig_defaulting[sig] = 1;
1217 (void) rsignal(sig, &Perl_csighandler);
1219 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1220 sig_ignoring[sig] = 0;
1223 sig_handlers_initted = 1;
1228 Perl_despatch_signals(pTHX)
1232 for (sig = 1; sig < SIG_SIZE; sig++) {
1233 if (PL_psig_pend[sig]) {
1234 PERL_BLOCKSIG_ADD(set, sig);
1235 PL_psig_pend[sig] = 0;
1236 PERL_BLOCKSIG_BLOCK(set);
1237 (*PL_sighandlerp)(sig);
1238 PERL_BLOCKSIG_UNBLOCK(set);
1244 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1249 /* Need to be careful with SvREFCNT_dec(), because that can have side
1250 * effects (due to closures). We must make sure that the new disposition
1251 * is in place before it is called.
1255 #ifdef HAS_SIGPROCMASK
1262 if (strEQ(s,"__DIE__"))
1264 else if (strEQ(s,"__WARN__"))
1267 Perl_croak(aTHX_ "No such hook: %s", s);
1275 i = whichsig(s); /* ...no, a brick */
1277 if (ckWARN(WARN_SIGNAL))
1278 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
1281 #ifdef HAS_SIGPROCMASK
1282 /* Avoid having the signal arrive at a bad time, if possible. */
1285 sigprocmask(SIG_BLOCK, &set, &save);
1287 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1288 SAVEFREESV(save_sv);
1289 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1292 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1293 if (!sig_handlers_initted) Perl_csighandler_init();
1295 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1296 sig_ignoring[i] = 0;
1298 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1299 sig_defaulting[i] = 0;
1301 SvREFCNT_dec(PL_psig_name[i]);
1302 to_dec = PL_psig_ptr[i];
1303 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1304 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1305 PL_psig_name[i] = newSVpvn(s, len);
1306 SvREADONLY_on(PL_psig_name[i]);
1308 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1310 (void)rsignal(i, &Perl_csighandler);
1311 #ifdef HAS_SIGPROCMASK
1316 *svp = SvREFCNT_inc(sv);
1318 SvREFCNT_dec(to_dec);
1321 s = SvPV_force(sv,len);
1322 if (strEQ(s,"IGNORE")) {
1324 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1325 sig_ignoring[i] = 1;
1326 (void)rsignal(i, &Perl_csighandler);
1328 (void)rsignal(i, SIG_IGN);
1332 else if (strEQ(s,"DEFAULT") || !*s) {
1334 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1336 sig_defaulting[i] = 1;
1337 (void)rsignal(i, &Perl_csighandler);
1340 (void)rsignal(i, SIG_DFL);
1345 * We should warn if HINT_STRICT_REFS, but without
1346 * access to a known hint bit in a known OP, we can't
1347 * tell whether HINT_STRICT_REFS is in force or not.
1349 if (!strchr(s,':') && !strchr(s,'\''))
1350 sv_insert(sv, 0, 0, "main::", 6);
1352 (void)rsignal(i, &Perl_csighandler);
1354 *svp = SvREFCNT_inc(sv);
1356 #ifdef HAS_SIGPROCMASK
1361 SvREFCNT_dec(to_dec);
1364 #endif /* !PERL_MICRO */
1367 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1369 PL_sub_generation++;
1374 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1376 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1377 PL_amagic_generation++;
1383 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1385 HV *hv = (HV*)LvTARG(sv);
1389 (void) hv_iterinit(hv);
1390 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1393 while (hv_iternext(hv))
1398 sv_setiv(sv, (IV)i);
1403 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1406 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1411 /* caller is responsible for stack switching/cleanup */
1413 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1419 PUSHs(SvTIED_obj(sv, mg));
1422 if (mg->mg_len >= 0)
1423 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1424 else if (mg->mg_len == HEf_SVKEY)
1425 PUSHs((SV*)mg->mg_ptr);
1427 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1428 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1436 return call_method(meth, flags);
1440 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1446 PUSHSTACKi(PERLSI_MAGIC);
1448 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1449 sv_setsv(sv, *PL_stack_sp--);
1459 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1461 magic_methpack(sv,mg,"FETCH");
1463 mg->mg_flags |= MGf_GSKIP;
1468 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1472 PUSHSTACKi(PERLSI_MAGIC);
1473 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1480 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1482 return magic_methpack(sv,mg,"DELETE");
1487 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1494 PUSHSTACKi(PERLSI_MAGIC);
1495 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1496 sv = *PL_stack_sp--;
1497 retval = (U32) SvIV(sv)-1;
1506 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1511 PUSHSTACKi(PERLSI_MAGIC);
1513 XPUSHs(SvTIED_obj(sv, mg));
1515 call_method("CLEAR", G_SCALAR|G_DISCARD);
1522 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1525 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1529 PUSHSTACKi(PERLSI_MAGIC);
1532 PUSHs(SvTIED_obj(sv, mg));
1537 if (call_method(meth, G_SCALAR))
1538 sv_setsv(key, *PL_stack_sp--);
1547 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1549 return magic_methpack(sv,mg,"EXISTS");
1553 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1563 svp = av_fetch(GvAV(gv),
1564 atoi(MgPV(mg,n_a)), FALSE);
1565 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp)))) {
1566 /* set or clear breakpoint in the relevant control op */
1568 o->op_flags |= OPf_SPECIAL;
1570 o->op_flags &= ~OPf_SPECIAL;
1576 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1578 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1583 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1585 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1590 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1592 SV* lsv = LvTARG(sv);
1594 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1595 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1596 if (mg && mg->mg_len >= 0) {
1599 sv_pos_b2u(lsv, &i);
1600 sv_setiv(sv, i + PL_curcop->cop_arybase);
1609 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1611 SV* lsv = LvTARG(sv);
1618 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1619 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1623 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1624 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1626 else if (!SvOK(sv)) {
1630 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1632 pos = SvIV(sv) - PL_curcop->cop_arybase;
1635 ulen = sv_len_utf8(lsv);
1645 else if (pos > (SSize_t)len)
1650 sv_pos_u2b(lsv, &p, 0);
1655 mg->mg_flags &= ~MGf_MINMATCH;
1661 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1663 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1665 gv_efullname3(sv,((GV*)sv), "*");
1669 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1674 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1683 if (*s == '*' && s[1])
1685 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1690 GvGP(sv) = gp_ref(GvGP(gv));
1695 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1698 SV *lsv = LvTARG(sv);
1699 char *tmps = SvPV(lsv,len);
1700 I32 offs = LvTARGOFF(sv);
1701 I32 rem = LvTARGLEN(sv);
1704 sv_pos_u2b(lsv, &offs, &rem);
1705 if (offs > (I32)len)
1707 if (rem + offs > (I32)len)
1709 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1716 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1719 char *tmps = SvPV(sv, len);
1720 SV *lsv = LvTARG(sv);
1721 I32 lvoff = LvTARGOFF(sv);
1722 I32 lvlen = LvTARGLEN(sv);
1725 sv_utf8_upgrade(lsv);
1726 sv_pos_u2b(lsv, &lvoff, &lvlen);
1727 sv_insert(lsv, lvoff, lvlen, tmps, len);
1730 else if (lsv && SvUTF8(lsv)) {
1731 sv_pos_u2b(lsv, &lvoff, &lvlen);
1732 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
1733 sv_insert(lsv, lvoff, lvlen, tmps, len);
1737 sv_insert(lsv, lvoff, lvlen, tmps, len);
1743 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1745 TAINT_IF((mg->mg_len & 1) ||
1746 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1751 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1753 if (PL_localizing) {
1754 if (PL_localizing == 1)
1759 else if (PL_tainted)
1767 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1769 SV *lsv = LvTARG(sv);
1776 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1781 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1783 do_vecset(sv); /* XXX slurp this routine */
1788 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1791 if (LvTARGLEN(sv)) {
1793 SV *ahv = LvTARG(sv);
1794 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1799 AV* av = (AV*)LvTARG(sv);
1800 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1801 targ = AvARRAY(av)[LvTARGOFF(sv)];
1803 if (targ && targ != &PL_sv_undef) {
1804 /* somebody else defined it for us */
1805 SvREFCNT_dec(LvTARG(sv));
1806 LvTARG(sv) = SvREFCNT_inc(targ);
1808 SvREFCNT_dec(mg->mg_obj);
1809 mg->mg_obj = Nullsv;
1810 mg->mg_flags &= ~MGf_REFCOUNTED;
1815 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1820 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1825 sv_setsv(LvTARG(sv), sv);
1826 SvSETMAGIC(LvTARG(sv));
1832 Perl_vivify_defelem(pTHX_ SV *sv)
1837 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
1840 SV *ahv = LvTARG(sv);
1842 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1845 if (!value || value == &PL_sv_undef)
1846 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1849 AV* av = (AV*)LvTARG(sv);
1850 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1851 LvTARG(sv) = Nullsv; /* array can't be extended */
1853 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1854 if (!svp || (value = *svp) == &PL_sv_undef)
1855 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1858 (void)SvREFCNT_inc(value);
1859 SvREFCNT_dec(LvTARG(sv));
1862 SvREFCNT_dec(mg->mg_obj);
1863 mg->mg_obj = Nullsv;
1864 mg->mg_flags &= ~MGf_REFCOUNTED;
1868 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1870 AV *av = (AV*)mg->mg_obj;
1871 SV **svp = AvARRAY(av);
1872 I32 i = AvFILLp(av);
1874 if (svp[i] && svp[i] != &PL_sv_undef) {
1875 if (!SvWEAKREF(svp[i]))
1876 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1877 /* XXX Should we check that it hasn't changed? */
1879 (void)SvOK_off(svp[i]);
1880 SvWEAKREF_off(svp[i]);
1881 svp[i] = &PL_sv_undef;
1889 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1897 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1899 sv_unmagic(sv, PERL_MAGIC_bm);
1905 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1907 sv_unmagic(sv, PERL_MAGIC_fm);
1913 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1915 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1917 if (uf && uf->uf_set)
1918 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
1923 Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
1925 sv_unmagic(sv, PERL_MAGIC_qr);
1930 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1932 regexp *re = (regexp *)mg->mg_obj;
1937 #ifdef USE_LOCALE_COLLATE
1939 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1942 * RenE<eacute> Descartes said "I think not."
1943 * and vanished with a faint plop.
1946 Safefree(mg->mg_ptr);
1952 #endif /* USE_LOCALE_COLLATE */
1954 /* Just clear the UTF-8 cache data. */
1956 Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
1958 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
1960 mg->mg_len = -1; /* The mg_len holds the len cache. */
1965 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1970 switch (*mg->mg_ptr) {
1971 case '\001': /* ^A */
1972 sv_setsv(PL_bodytarget, sv);
1974 case '\003': /* ^C */
1975 PL_minus_c = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1978 case '\004': /* ^D */
1979 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
1980 DEBUG_x(dump_all());
1982 case '\005': /* ^E */
1983 if (*(mg->mg_ptr+1) == '\0') {
1984 #ifdef MACOS_TRADITIONAL
1985 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1988 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1991 SetLastError( SvIV(sv) );
1994 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1996 /* will anyone ever use this? */
1997 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
2003 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
2005 SvREFCNT_dec(PL_encoding);
2006 if (SvOK(sv) || SvGMAGICAL(sv)) {
2007 PL_encoding = newSVsv(sv);
2010 PL_encoding = Nullsv;
2014 case '\006': /* ^F */
2015 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2017 case '\010': /* ^H */
2018 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2020 case '\011': /* ^I */ /* NOT \t in EBCDIC */
2022 Safefree(PL_inplace);
2024 PL_inplace = savepv(SvPV(sv,len));
2026 PL_inplace = Nullch;
2028 case '\017': /* ^O */
2029 if (*(mg->mg_ptr+1) == '\0') {
2031 Safefree(PL_osname);
2033 PL_osname = savepv(SvPV(sv,len));
2037 else if (strEQ(mg->mg_ptr, "\017PEN")) {
2038 if (!PL_compiling.cop_io)
2039 PL_compiling.cop_io = newSVsv(sv);
2041 sv_setsv(PL_compiling.cop_io,sv);
2044 case '\020': /* ^P */
2045 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2046 if (PL_perldb && !PL_DBsingle)
2049 case '\024': /* ^T */
2051 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
2053 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2056 case '\027': /* ^W & $^WARNING_BITS */
2057 if (*(mg->mg_ptr+1) == '\0') {
2058 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2059 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2060 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
2061 | (i ? G_WARN_ON : G_WARN_OFF) ;
2064 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
2065 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2066 if (!SvPOK(sv) && PL_localizing) {
2067 sv_setpvn(sv, WARN_NONEstring, WARNsize);
2068 PL_compiling.cop_warnings = pWARN_NONE;
2073 int accumulate = 0 ;
2074 int any_fatals = 0 ;
2075 char * ptr = (char*)SvPV(sv, len) ;
2076 for (i = 0 ; i < len ; ++i) {
2077 accumulate |= ptr[i] ;
2078 any_fatals |= (ptr[i] & 0xAA) ;
2081 PL_compiling.cop_warnings = pWARN_NONE;
2082 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
2083 PL_compiling.cop_warnings = pWARN_ALL;
2084 PL_dowarn |= G_WARN_ONCE ;
2087 if (specialWARN(PL_compiling.cop_warnings))
2088 PL_compiling.cop_warnings = newSVsv(sv) ;
2090 sv_setsv(PL_compiling.cop_warnings, sv);
2091 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2092 PL_dowarn |= G_WARN_ONCE ;
2100 if (PL_localizing) {
2101 if (PL_localizing == 1)
2102 SAVESPTR(PL_last_in_gv);
2104 else if (SvOK(sv) && GvIO(PL_last_in_gv))
2105 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
2108 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2109 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
2110 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
2113 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2114 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
2115 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
2118 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2121 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2122 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2123 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
2126 IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2130 IO *io = GvIOp(PL_defoutgv);
2133 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
2134 IoFLAGS(io) &= ~IOf_FLUSH;
2136 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2137 PerlIO *ofp = IoOFP(io);
2139 (void)PerlIO_flush(ofp);
2140 IoFLAGS(io) |= IOf_FLUSH;
2146 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2147 PL_multiline = (i != 0);
2150 SvREFCNT_dec(PL_rs);
2151 PL_rs = newSVsv(sv);
2155 SvREFCNT_dec(PL_ors_sv);
2156 if (SvOK(sv) || SvGMAGICAL(sv)) {
2157 PL_ors_sv = newSVsv(sv);
2165 SvREFCNT_dec(PL_ofs_sv);
2166 if (SvOK(sv) || SvGMAGICAL(sv)) {
2167 PL_ofs_sv = newSVsv(sv);
2176 PL_ofmt = savepv(SvPV(sv,len));
2179 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2182 #ifdef COMPLEX_STATUS
2183 if (PL_localizing == 2) {
2184 PL_statusvalue = LvTARGOFF(sv);
2185 PL_statusvalue_vms = LvTARGLEN(sv);
2189 #ifdef VMSISH_STATUS
2191 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
2194 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2199 # define PERL_VMS_BANG vaxc$errno
2201 # define PERL_VMS_BANG 0
2203 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
2204 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
2208 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2209 if (PL_delaymagic) {
2210 PL_delaymagic |= DM_RUID;
2211 break; /* don't do magic till later */
2214 (void)setruid((Uid_t)PL_uid);
2217 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
2219 #ifdef HAS_SETRESUID
2220 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
2222 if (PL_uid == PL_euid) /* special case $< = $> */
2223 (void)PerlProc_setuid(PL_uid);
2225 PL_uid = PerlProc_getuid();
2226 Perl_croak(aTHX_ "setruid() not implemented");
2231 PL_uid = PerlProc_getuid();
2232 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2235 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2236 if (PL_delaymagic) {
2237 PL_delaymagic |= DM_EUID;
2238 break; /* don't do magic till later */
2241 (void)seteuid((Uid_t)PL_euid);
2244 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
2246 #ifdef HAS_SETRESUID
2247 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
2249 if (PL_euid == PL_uid) /* special case $> = $< */
2250 PerlProc_setuid(PL_euid);
2252 PL_euid = PerlProc_geteuid();
2253 Perl_croak(aTHX_ "seteuid() not implemented");
2258 PL_euid = PerlProc_geteuid();
2259 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2262 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2263 if (PL_delaymagic) {
2264 PL_delaymagic |= DM_RGID;
2265 break; /* don't do magic till later */
2268 (void)setrgid((Gid_t)PL_gid);
2271 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
2273 #ifdef HAS_SETRESGID
2274 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2276 if (PL_gid == PL_egid) /* special case $( = $) */
2277 (void)PerlProc_setgid(PL_gid);
2279 PL_gid = PerlProc_getgid();
2280 Perl_croak(aTHX_ "setrgid() not implemented");
2285 PL_gid = PerlProc_getgid();
2286 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2289 #ifdef HAS_SETGROUPS
2291 char *p = SvPV(sv, len);
2292 Groups_t gary[NGROUPS];
2297 for (i = 0; i < NGROUPS; ++i) {
2298 while (*p && !isSPACE(*p))
2307 (void)setgroups(i, gary);
2309 #else /* HAS_SETGROUPS */
2310 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2311 #endif /* HAS_SETGROUPS */
2312 if (PL_delaymagic) {
2313 PL_delaymagic |= DM_EGID;
2314 break; /* don't do magic till later */
2317 (void)setegid((Gid_t)PL_egid);
2320 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2322 #ifdef HAS_SETRESGID
2323 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2325 if (PL_egid == PL_gid) /* special case $) = $( */
2326 (void)PerlProc_setgid(PL_egid);
2328 PL_egid = PerlProc_getegid();
2329 Perl_croak(aTHX_ "setegid() not implemented");
2334 PL_egid = PerlProc_getegid();
2335 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2338 PL_chopset = SvPV_force(sv,len);
2340 #ifndef MACOS_TRADITIONAL
2342 LOCK_DOLLARZERO_MUTEX;
2343 #ifdef HAS_SETPROCTITLE
2344 /* The BSDs don't show the argv[] in ps(1) output, they
2345 * show a string from the process struct and provide
2346 * the setproctitle() routine to manipulate that. */
2349 # if __FreeBSD_version > 410001
2350 /* The leading "-" removes the "perl: " prefix,
2351 * but not the "(perl) suffix from the ps(1)
2352 * output, because that's what ps(1) shows if the
2353 * argv[] is modified. */
2354 setproctitle("-%s", s);
2355 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2356 /* This doesn't really work if you assume that
2357 * $0 = 'foobar'; will wipe out 'perl' from the $0
2358 * because in ps(1) output the result will be like
2359 * sprintf("perl: %s (perl)", s)
2360 * I guess this is a security feature:
2361 * one (a user process) cannot get rid of the original name.
2363 setproctitle("%s", s);
2367 #if defined(__hpux) && defined(PSTAT_SETCMD)
2372 pstat(PSTAT_SETCMD, un, len, 0, 0);
2378 /* See if all the arguments are contiguous in memory */
2379 for (i = 1; i < PL_origargc; i++) {
2380 if (PL_origargv[i] == s + 1
2382 || PL_origargv[i] == s + 2
2387 s += strlen(s); /* this one is ok too */
2392 /* can grab env area too? */
2395 && PL_curinterp == aTHX
2397 && (PL_origenviron[0] == s + 1))
2399 my_setenv("NoNe SuCh", Nullch);
2400 /* force copy of environment */
2401 for (i = 0; PL_origenviron[i]; i++)
2402 if (PL_origenviron[i] == s + 1) {
2409 PL_origalen = s - PL_origargv[0];
2411 s = SvPV_force(sv,len);
2413 if (i >= (I32)PL_origalen) {
2415 /* don't allow system to limit $0 seen by script */
2416 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2417 Copy(s, PL_origargv[0], i, char);
2418 s = PL_origargv[0]+i;
2422 Copy(s, PL_origargv[0], i, char);
2423 s = PL_origargv[0]+i;
2425 while (++i < (I32)PL_origalen)
2427 for (i = 1; i < PL_origargc; i++)
2428 PL_origargv[i] = Nullch;
2430 UNLOCK_DOLLARZERO_MUTEX;
2438 Perl_whichsig(pTHX_ char *sig)
2440 register char **sigv;
2442 for (sigv = PL_sig_name; *sigv; sigv++)
2443 if (strEQ(sig,*sigv))
2444 return PL_sig_num[sigv - PL_sig_name];
2446 if (strEQ(sig,"CHLD"))
2450 if (strEQ(sig,"CLD"))
2456 #if !defined(PERL_IMPLICIT_CONTEXT)
2461 Perl_sighandler(int sig)
2463 #ifdef PERL_GET_SIG_CONTEXT
2464 dTHXa(PERL_GET_SIG_CONTEXT);
2471 SV *sv = Nullsv, *tSv = PL_Sv;
2477 if (PL_savestack_ix + 15 <= PL_savestack_max)
2479 if (PL_markstack_ptr < PL_markstack_max - 2)
2481 if (PL_retstack_ix < PL_retstack_max - 2)
2483 if (PL_scopestack_ix < PL_scopestack_max - 3)
2486 if (!PL_psig_ptr[sig]) {
2487 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
2492 /* Max number of items pushed there is 3*n or 4. We cannot fix
2493 infinity, so we fix 4 (in fact 5): */
2495 PL_savestack_ix += 5; /* Protect save in progress. */
2496 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2499 PL_markstack_ptr++; /* Protect mark. */
2502 PL_retstack[PL_retstack_ix] = NULL;
2505 PL_scopestack_ix += 1;
2506 /* sv_2cv is too complicated, try a simpler variant first: */
2507 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2508 || SvTYPE(cv) != SVt_PVCV)
2509 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2511 if (!cv || !CvROOT(cv)) {
2512 if (ckWARN(WARN_SIGNAL))
2513 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
2514 PL_sig_name[sig], (gv ? GvENAME(gv)
2521 if(PL_psig_name[sig]) {
2522 sv = SvREFCNT_inc(PL_psig_name[sig]);
2524 #if !defined(PERL_IMPLICIT_CONTEXT)
2528 sv = sv_newmortal();
2529 sv_setpv(sv,PL_sig_name[sig]);
2532 PUSHSTACKi(PERLSI_SIGNAL);
2537 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2540 if (SvTRUE(ERRSV)) {
2542 #ifdef HAS_SIGPROCMASK
2543 /* Handler "died", for example to get out of a restart-able read().
2544 * Before we re-do that on its behalf re-enable the signal which was
2545 * blocked by the system when we entered.
2549 sigaddset(&set,sig);
2550 sigprocmask(SIG_UNBLOCK, &set, NULL);
2552 /* Not clear if this will work */
2553 (void)rsignal(sig, SIG_IGN);
2554 (void)rsignal(sig, &Perl_csighandler);
2556 #endif /* !PERL_MICRO */
2557 Perl_die(aTHX_ Nullformat);
2561 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2567 PL_scopestack_ix -= 1;
2570 PL_op = myop; /* Apparently not needed... */
2572 PL_Sv = tSv; /* Restore global temporaries. */
2579 restore_magic(pTHX_ void *p)
2581 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2582 SV* sv = mgs->mgs_sv;
2587 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2590 SvFLAGS(sv) |= mgs->mgs_flags;
2594 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2597 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2599 /* If we're still on top of the stack, pop us off. (That condition
2600 * will be satisfied if restore_magic was called explicitly, but *not*
2601 * if it's being called via leave_scope.)
2602 * The reason for doing this is that otherwise, things like sv_2cv()
2603 * may leave alloc gunk on the savestack, and some code
2604 * (e.g. sighandler) doesn't expect that...
2606 if (PL_savestack_ix == mgs->mgs_ss_ix)
2608 I32 popval = SSPOPINT;
2609 assert(popval == SAVEt_DESTRUCTOR_X);
2610 PL_savestack_ix -= 2;
2612 assert(popval == SAVEt_ALLOC);
2614 PL_savestack_ix -= popval;
2620 unwind_handler_stack(pTHX_ void *p)
2622 U32 flags = *(U32*)p;
2625 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2626 /* cxstack_ix-- Not needed, die already unwound it. */
2627 #if !defined(PERL_IMPLICIT_CONTEXT)
2629 SvREFCNT_dec(sig_sv);