3 * Copyright (c) 1991-2002, 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)
32 /* if you only have signal() and it resets on each signal, FAKE_PERSISTENT_SIGNAL_HANDLERS fixes */
33 #if !defined(HAS_SIGACTION) && defined(VMS)
34 # define FAKE_PERSISTENT_SIGNAL_HANDLERS
36 /* if we're doing kill() with sys$sigprc on VMS, FAKE_DEFAULT_SIGNAL_HANDLERS */
37 #if defined(KILL_BY_SIGPRC)
38 # define FAKE_DEFAULT_SIGNAL_HANDLERS
41 static void restore_magic(pTHX_ void *p);
42 static void unwind_handler_stack(pTHX_ void *p);
45 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
53 /* MGS is typedef'ed to struct magic_state in perl.h */
56 S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
59 assert(SvMAGICAL(sv));
61 SAVEDESTRUCTOR_X(restore_magic, INT2PTR(void*, (IV)mgs_ix));
63 mgs = SSPTR(mgs_ix, MGS*);
65 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
66 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
70 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
74 =for apidoc mg_magical
76 Turns on the magical status of an SV. See C<sv_magic>.
82 Perl_mg_magical(pTHX_ SV *sv)
85 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
86 MGVTBL* vtbl = mg->mg_virtual;
88 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
92 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
101 Do magic after a value is retrieved from the SV. See C<sv_magic>.
107 Perl_mg_get(pTHX_ SV *sv)
110 MAGIC *newmg, *head, *cur, *mg;
111 I32 mgs_ix = SSNEW(sizeof(MGS));
113 save_magic(mgs_ix, sv);
115 /* We must call svt_get(sv, mg) for each valid entry in the linked
116 list of magic. svt_get() may delete the current entry, add new
117 magic to the head of the list, or upgrade the SV. AMS 20010810 */
119 newmg = cur = head = mg = SvMAGIC(sv);
121 MGVTBL *vtbl = mg->mg_virtual;
123 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
124 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
125 /* Don't restore the flags for this entry if it was deleted. */
126 if (mg->mg_flags & MGf_GSKIP)
127 (SSPTR(mgs_ix, MGS *))->mgs_flags = 0;
130 mg = mg->mg_moremagic;
133 /* Have we finished with the new entries we saw? Start again
134 where we left off (unless there are more new entries). */
142 /* Were any new entries added? */
143 if (!new && (newmg = SvMAGIC(sv)) != head) {
150 restore_magic(aTHX_ INT2PTR(void *, (IV)mgs_ix));
157 Do magic after a value is assigned to the SV. See C<sv_magic>.
163 Perl_mg_set(pTHX_ SV *sv)
169 mgs_ix = SSNEW(sizeof(MGS));
170 save_magic(mgs_ix, sv);
172 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
173 MGVTBL* vtbl = mg->mg_virtual;
174 nextmg = mg->mg_moremagic; /* it may delete itself */
175 if (mg->mg_flags & MGf_GSKIP) {
176 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
177 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
179 if (vtbl && vtbl->svt_set)
180 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
183 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
188 =for apidoc mg_length
190 Report on the SV's length. See C<sv_magic>.
196 Perl_mg_length(pTHX_ SV *sv)
201 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
202 MGVTBL* vtbl = mg->mg_virtual;
203 if (vtbl && vtbl->svt_len) {
206 mgs_ix = SSNEW(sizeof(MGS));
207 save_magic(mgs_ix, sv);
208 /* omit MGf_GSKIP -- not changed here */
209 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
210 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
217 U8 *s = (U8*)SvPV(sv, len);
218 len = Perl_utf8_length(aTHX_ s, s + len);
226 Perl_mg_size(pTHX_ SV *sv)
231 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
232 MGVTBL* vtbl = mg->mg_virtual;
233 if (vtbl && vtbl->svt_len) {
236 mgs_ix = SSNEW(sizeof(MGS));
237 save_magic(mgs_ix, sv);
238 /* omit MGf_GSKIP -- not changed here */
239 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
240 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
247 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
252 Perl_croak(aTHX_ "Size magic not implemented");
261 Clear something magical that the SV represents. See C<sv_magic>.
267 Perl_mg_clear(pTHX_ SV *sv)
272 mgs_ix = SSNEW(sizeof(MGS));
273 save_magic(mgs_ix, sv);
275 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
276 MGVTBL* vtbl = mg->mg_virtual;
277 /* omit GSKIP -- never set here */
279 if (vtbl && vtbl->svt_clear)
280 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
283 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
290 Finds the magic pointer for type matching the SV. See C<sv_magic>.
296 Perl_mg_find(pTHX_ SV *sv, int type)
301 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
302 if (mg->mg_type == type)
311 Copies the magic from one SV to another. See C<sv_magic>.
317 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
321 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
322 MGVTBL* vtbl = mg->mg_virtual;
323 if ((mg->mg_flags & MGf_COPY) && vtbl->svt_copy){
324 count += CALL_FPTR(vtbl->svt_copy)(aTHX_ sv, mg, nsv, key, klen);
326 else if (isUPPER(mg->mg_type)) {
328 mg->mg_type == PERL_MAGIC_tied ? SvTIED_obj(sv, mg) :
329 (mg->mg_type == PERL_MAGIC_regdata && mg->mg_obj)
331 toLOWER(mg->mg_type), key, klen);
341 Free any magic storage used by the SV. See C<sv_magic>.
347 Perl_mg_free(pTHX_ SV *sv)
351 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
352 MGVTBL* vtbl = mg->mg_virtual;
353 moremagic = mg->mg_moremagic;
354 if (vtbl && vtbl->svt_free)
355 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
356 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
358 Safefree(mg->mg_ptr);
359 else if (mg->mg_len == HEf_SVKEY)
360 SvREFCNT_dec((SV*)mg->mg_ptr);
362 if (mg->mg_flags & MGf_REFCOUNTED)
363 SvREFCNT_dec(mg->mg_obj);
371 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
376 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
380 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
381 if (mg->mg_obj) /* @+ */
384 return rx->lastparen;
391 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
399 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
403 if (paren <= (I32)rx->nparens &&
404 (s = rx->startp[paren]) != -1 &&
405 (t = rx->endp[paren]) != -1)
407 if (mg->mg_obj) /* @+ */
412 if (i > 0 && PL_reg_match_utf8) {
413 char *b = rx->subbeg;
415 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
425 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
427 Perl_croak(aTHX_ PL_no_modify);
433 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
440 switch (*mg->mg_ptr) {
441 case '1': case '2': case '3': case '4':
442 case '5': case '6': case '7': case '8': case '9': case '&':
443 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
445 paren = atoi(mg->mg_ptr); /* $& is in [0] */
447 if (paren <= (I32)rx->nparens &&
448 (s1 = rx->startp[paren]) != -1 &&
449 (t1 = rx->endp[paren]) != -1)
453 if (i > 0 && PL_reg_match_utf8) {
454 char *s = rx->subbeg + s1;
455 char *send = rx->subbeg + t1;
458 if (is_utf8_string((U8*)s, i))
459 i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send);
462 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
466 if (ckWARN(WARN_UNINITIALIZED))
471 if (ckWARN(WARN_UNINITIALIZED))
476 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
477 paren = rx->lastparen;
482 case '\016': /* ^N */
483 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
484 paren = rx->lastcloseparen;
490 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
491 if (rx->startp[0] != -1) {
502 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
503 if (rx->endp[0] != -1) {
504 i = rx->sublen - rx->endp[0];
515 if (!SvPOK(sv) && SvNIOK(sv)) {
525 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
532 switch (*mg->mg_ptr) {
533 case '\001': /* ^A */
534 sv_setsv(sv, PL_bodytarget);
536 case '\003': /* ^C */
537 sv_setiv(sv, (IV)PL_minus_c);
540 case '\004': /* ^D */
541 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
542 #if defined(YYDEBUG) && defined(DEBUGGING)
543 PL_yydebug = DEBUG_p_TEST;
546 case '\005': /* ^E */
547 if (*(mg->mg_ptr+1) == '\0') {
548 #ifdef MACOS_TRADITIONAL
552 sv_setnv(sv,(double)gMacPerl_OSErr);
553 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
558 # include <descrip.h>
559 # include <starlet.h>
561 $DESCRIPTOR(msgdsc,msg);
562 sv_setnv(sv,(NV) vaxc$errno);
563 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
564 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
570 if (!(_emx_env & 0x200)) { /* Under DOS */
571 sv_setnv(sv, (NV)errno);
572 sv_setpv(sv, errno ? Strerror(errno) : "");
574 if (errno != errno_isOS2) {
575 int tmp = _syserrno();
576 if (tmp) /* 2nd call to _syserrno() makes it 0 */
579 sv_setnv(sv, (NV)Perl_rc);
580 sv_setpv(sv, os2error(Perl_rc));
585 DWORD dwErr = GetLastError();
586 sv_setnv(sv, (NV)dwErr);
589 PerlProc_GetOSError(sv, dwErr);
596 sv_setnv(sv, (NV)errno);
597 sv_setpv(sv, errno ? Strerror(errno) : "");
602 SvNOK_on(sv); /* what a wonderful hack! */
604 else if (strEQ(mg->mg_ptr+1, "NCODING"))
605 sv_setsv(sv, PL_encoding);
607 case '\006': /* ^F */
608 sv_setiv(sv, (IV)PL_maxsysfd);
610 case '\010': /* ^H */
611 sv_setiv(sv, (IV)PL_hints);
613 case '\011': /* ^I */ /* NOT \t in EBCDIC */
615 sv_setpv(sv, PL_inplace);
617 sv_setsv(sv, &PL_sv_undef);
619 case '\017': /* ^O & ^OPEN */
620 if (*(mg->mg_ptr+1) == '\0')
621 sv_setpv(sv, PL_osname);
622 else if (strEQ(mg->mg_ptr, "\017PEN")) {
623 if (!PL_compiling.cop_io)
624 sv_setsv(sv, &PL_sv_undef);
626 sv_setsv(sv, PL_compiling.cop_io);
630 case '\020': /* ^P */
631 sv_setiv(sv, (IV)PL_perldb);
633 case '\023': /* ^S */
635 if (PL_lex_state != LEX_NOTPARSING)
638 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
643 case '\024': /* ^T */
644 if (*(mg->mg_ptr+1) == '\0') {
646 sv_setnv(sv, PL_basetime);
648 sv_setiv(sv, (IV)PL_basetime);
651 else if (strEQ(mg->mg_ptr, "\024AINT"))
652 sv_setiv(sv, PL_tainting);
654 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
655 if (*(mg->mg_ptr+1) == '\0')
656 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
657 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
658 if (PL_compiling.cop_warnings == pWARN_NONE ||
659 PL_compiling.cop_warnings == pWARN_STD)
661 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
663 else if (PL_compiling.cop_warnings == pWARN_ALL) {
664 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
667 sv_setsv(sv, PL_compiling.cop_warnings);
671 else if (strEQ(mg->mg_ptr+1, "IDE_SYSTEM_CALLS"))
672 sv_setiv(sv, (IV)PL_widesyscalls);
674 case '1': case '2': case '3': case '4':
675 case '5': case '6': case '7': case '8': case '9': case '&':
676 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
680 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
681 * XXX Does the new way break anything?
683 paren = atoi(mg->mg_ptr); /* $& is in [0] */
685 if (paren <= (I32)rx->nparens &&
686 (s1 = rx->startp[paren]) != -1 &&
687 (t1 = rx->endp[paren]) != -1)
697 if (PL_reg_match_utf8 && is_utf8_string((U8*)s, i))
702 if (RX_MATCH_TAINTED(rx)) {
703 MAGIC* mg = SvMAGIC(sv);
706 SvMAGIC(sv) = mg->mg_moremagic;
708 if ((mgt = SvMAGIC(sv))) {
709 mg->mg_moremagic = mgt;
719 sv_setsv(sv,&PL_sv_undef);
722 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
723 paren = rx->lastparen;
727 sv_setsv(sv,&PL_sv_undef);
729 case '\016': /* ^N */
730 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
731 paren = rx->lastcloseparen;
735 sv_setsv(sv,&PL_sv_undef);
738 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
739 if ((s = rx->subbeg) && rx->startp[0] != -1) {
744 sv_setsv(sv,&PL_sv_undef);
747 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
748 if (rx->subbeg && rx->endp[0] != -1) {
749 s = rx->subbeg + rx->endp[0];
750 i = rx->sublen - rx->endp[0];
754 sv_setsv(sv,&PL_sv_undef);
758 if (GvIO(PL_last_in_gv)) {
759 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
765 sv_setiv(sv, (IV)STATUS_CURRENT);
766 #ifdef COMPLEX_STATUS
767 LvTARGOFF(sv) = PL_statusvalue;
768 LvTARGLEN(sv) = PL_statusvalue_vms;
773 s = IoTOP_NAME(GvIOp(PL_defoutgv));
777 sv_setpv(sv,GvENAME(PL_defoutgv));
782 s = IoFMT_NAME(GvIOp(PL_defoutgv));
784 s = GvENAME(PL_defoutgv);
789 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
792 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
795 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
803 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
806 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
812 sv_setpv(sv,SvPVX(PL_ors_sv));
815 sv_setpv(sv,PL_ofmt);
819 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
820 sv_setpv(sv, errno ? Strerror(errno) : "");
823 int saveerrno = errno;
824 sv_setnv(sv, (NV)errno);
826 if (errno == errno_isOS2 || errno == errno_isOS2_set)
827 sv_setpv(sv, os2error(Perl_rc));
830 sv_setpv(sv, errno ? Strerror(errno) : "");
834 SvNOK_on(sv); /* what a wonderful hack! */
837 sv_setiv(sv, (IV)PL_uid);
840 sv_setiv(sv, (IV)PL_euid);
843 sv_setiv(sv, (IV)PL_gid);
845 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
849 sv_setiv(sv, (IV)PL_egid);
851 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
856 Groups_t gary[NGROUPS];
857 i = getgroups(NGROUPS,gary);
859 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
862 (void)SvIOK_on(sv); /* what a wonderful hack! */
866 #ifndef MACOS_TRADITIONAL
870 #ifdef USE_5005THREADS
872 sv_setsv(sv, thr->errsv);
874 #endif /* USE_5005THREADS */
880 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
882 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
884 if (uf && uf->uf_val)
885 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
890 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
900 #ifdef DYNAMIC_ENV_FETCH
901 /* We just undefd an environment var. Is a replacement */
902 /* waiting in the wings? */
905 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
906 s = SvPV(*valp, len);
910 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
911 /* And you'll never guess what the dog had */
912 /* in its mouth... */
914 MgTAINTEDDIR_off(mg);
916 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
917 char pathbuf[256], eltbuf[256], *cp, *elt = s;
921 do { /* DCL$PATH may be a search list */
922 while (1) { /* as may dev portion of any element */
923 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
924 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
925 cando_by_name(S_IWUSR,0,elt) ) {
930 if ((cp = strchr(elt, ':')) != Nullch)
932 if (my_trnlnm(elt, eltbuf, j++))
938 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
941 if (s && klen == 4 && strEQ(ptr,"PATH")) {
942 char *strend = s + len;
948 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
951 if (i >= sizeof tmpbuf /* too long -- assume the worst */
953 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
960 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
966 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
969 my_setenv(MgPV(mg,n_a),Nullch);
974 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
977 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
982 magic_clear_all_env(sv,mg);
983 hv_iterinit((HV*)sv);
984 while ((entry = hv_iternext((HV*)sv))) {
986 my_setenv(hv_iterkey(entry, &keylen),
987 SvPV(hv_iterval((HV*)sv, entry), n_a));
995 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
997 #if defined(VMS) || defined(EPOC)
998 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1000 # if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
1003 # ifdef USE_ENVIRON_ARRAY
1004 # if defined(USE_ITHREADS)
1005 /* only the parent thread can clobber the process environment */
1006 if (PL_curinterp == aTHX)
1009 # ifndef PERL_USE_SAFE_PUTENV
1012 if (environ == PL_origenviron)
1013 environ = (char**)safesysmalloc(sizeof(char*));
1015 for (i = 0; environ[i]; i++)
1016 safesysfree(environ[i]);
1017 # endif /* PERL_USE_SAFE_PUTENV */
1019 environ[0] = Nullch;
1021 # endif /* USE_ENVIRON_ARRAY */
1022 # endif /* PERL_IMPLICIT_SYS || WIN32 */
1023 #endif /* VMS || EPC */
1027 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS)||defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1028 static int sig_handlers_initted = 0;
1030 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1031 static int sig_ignoring[SIG_SIZE]; /* which signals we are ignoring */
1033 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1034 static int sig_defaulting[SIG_SIZE];
1039 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
1043 /* Are we fetching a signal entry? */
1044 i = whichsig(MgPV(mg,n_a));
1047 sv_setsv(sv,PL_psig_ptr[i]);
1049 Sighandler_t sigstate;
1050 sigstate = rsignal_state(i);
1051 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1052 if (sig_handlers_initted && sig_ignoring[i]) sigstate = SIG_IGN;
1054 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1055 if (sig_handlers_initted && sig_defaulting[i]) sigstate = SIG_DFL;
1057 /* cache state so we don't fetch it again */
1058 if(sigstate == SIG_IGN)
1059 sv_setpv(sv,"IGNORE");
1061 sv_setsv(sv,&PL_sv_undef);
1062 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1069 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
1073 /* Are we clearing a signal entry? */
1074 i = whichsig(MgPV(mg,n_a));
1076 if(PL_psig_ptr[i]) {
1077 SvREFCNT_dec(PL_psig_ptr[i]);
1080 if(PL_psig_name[i]) {
1081 SvREFCNT_dec(PL_psig_name[i]);
1089 Perl_raise_signal(pTHX_ int sig)
1091 /* Set a flag to say this signal is pending */
1092 PL_psig_pend[sig]++;
1093 /* And one to say _a_ signal is pending */
1098 Perl_csighandler(int sig)
1100 #ifdef PERL_GET_SIG_CONTEXT
1101 dTHXa(PERL_GET_SIG_CONTEXT);
1105 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1106 (void) rsignal(sig, &Perl_csighandler);
1107 if (sig_ignoring[sig]) return;
1109 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1110 if (sig_defaulting[sig])
1111 #ifdef KILL_BY_SIGPRC
1112 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1118 #ifdef PERL_OLD_SIGNALS
1119 /* Call the perl level handler now with risk we may be in malloc() etc. */
1120 (*PL_sighandlerp)(sig);
1122 Perl_raise_signal(aTHX_ sig);
1126 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1128 Perl_csighandler_init(void)
1131 if (sig_handlers_initted) return;
1133 for (sig = 1; sig < SIG_SIZE; sig++) {
1134 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1136 sig_defaulting[sig] = 1;
1137 (void) rsignal(sig, &Perl_csighandler);
1139 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1140 sig_ignoring[sig] = 0;
1143 sig_handlers_initted = 1;
1148 Perl_despatch_signals(pTHX)
1152 for (sig = 1; sig < SIG_SIZE; sig++) {
1153 if (PL_psig_pend[sig]) {
1154 PL_psig_pend[sig] = 0;
1155 (*PL_sighandlerp)(sig);
1161 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1170 if (strEQ(s,"__DIE__"))
1172 else if (strEQ(s,"__WARN__"))
1175 Perl_croak(aTHX_ "No such hook: %s", s);
1183 i = whichsig(s); /* ...no, a brick */
1185 if (ckWARN(WARN_SIGNAL))
1186 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
1189 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1190 if (!sig_handlers_initted) Perl_csighandler_init();
1192 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1193 sig_ignoring[i] = 0;
1195 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1196 sig_defaulting[i] = 0;
1198 SvREFCNT_dec(PL_psig_name[i]);
1199 SvREFCNT_dec(PL_psig_ptr[i]);
1200 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1201 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1202 PL_psig_name[i] = newSVpvn(s, len);
1203 SvREADONLY_on(PL_psig_name[i]);
1205 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1207 (void)rsignal(i, &Perl_csighandler);
1209 *svp = SvREFCNT_inc(sv);
1212 s = SvPV_force(sv,len);
1213 if (strEQ(s,"IGNORE")) {
1215 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1216 sig_ignoring[i] = 1;
1217 (void)rsignal(i, &Perl_csighandler);
1219 (void)rsignal(i, SIG_IGN);
1224 else if (strEQ(s,"DEFAULT") || !*s) {
1226 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1228 sig_defaulting[i] = 1;
1229 (void)rsignal(i, &Perl_csighandler);
1232 (void)rsignal(i, SIG_DFL);
1239 * We should warn if HINT_STRICT_REFS, but without
1240 * access to a known hint bit in a known OP, we can't
1241 * tell whether HINT_STRICT_REFS is in force or not.
1243 if (!strchr(s,':') && !strchr(s,'\''))
1244 sv_insert(sv, 0, 0, "main::", 6);
1246 (void)rsignal(i, &Perl_csighandler);
1248 *svp = SvREFCNT_inc(sv);
1252 #endif /* !PERL_MICRO */
1255 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1257 PL_sub_generation++;
1262 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1264 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1265 PL_amagic_generation++;
1271 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1273 HV *hv = (HV*)LvTARG(sv);
1277 (void) hv_iterinit(hv);
1278 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1281 while (hv_iternext(hv))
1286 sv_setiv(sv, (IV)i);
1291 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1294 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1299 /* caller is responsible for stack switching/cleanup */
1301 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1307 PUSHs(SvTIED_obj(sv, mg));
1310 if (mg->mg_len >= 0)
1311 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1312 else if (mg->mg_len == HEf_SVKEY)
1313 PUSHs((SV*)mg->mg_ptr);
1315 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1316 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1324 return call_method(meth, flags);
1328 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1334 PUSHSTACKi(PERLSI_MAGIC);
1336 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1337 sv_setsv(sv, *PL_stack_sp--);
1347 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1349 magic_methpack(sv,mg,"FETCH");
1351 mg->mg_flags |= MGf_GSKIP;
1356 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1360 PUSHSTACKi(PERLSI_MAGIC);
1361 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1368 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1370 return magic_methpack(sv,mg,"DELETE");
1375 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1382 PUSHSTACKi(PERLSI_MAGIC);
1383 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1384 sv = *PL_stack_sp--;
1385 retval = (U32) SvIV(sv)-1;
1394 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1399 PUSHSTACKi(PERLSI_MAGIC);
1401 XPUSHs(SvTIED_obj(sv, mg));
1403 call_method("CLEAR", G_SCALAR|G_DISCARD);
1410 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1413 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1417 PUSHSTACKi(PERLSI_MAGIC);
1420 PUSHs(SvTIED_obj(sv, mg));
1425 if (call_method(meth, G_SCALAR))
1426 sv_setsv(key, *PL_stack_sp--);
1435 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1437 return magic_methpack(sv,mg,"EXISTS");
1441 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1451 svp = av_fetch(GvAV(gv),
1452 atoi(MgPV(mg,n_a)), FALSE);
1453 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
1454 o->op_private = (U8)i;
1459 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1461 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1466 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1468 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1473 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1475 SV* lsv = LvTARG(sv);
1477 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1478 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1479 if (mg && mg->mg_len >= 0) {
1482 sv_pos_b2u(lsv, &i);
1483 sv_setiv(sv, i + PL_curcop->cop_arybase);
1492 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1494 SV* lsv = LvTARG(sv);
1501 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1502 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1506 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1507 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1509 else if (!SvOK(sv)) {
1513 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1515 pos = SvIV(sv) - PL_curcop->cop_arybase;
1518 ulen = sv_len_utf8(lsv);
1528 else if (pos > (SSize_t)len)
1533 sv_pos_u2b(lsv, &p, 0);
1538 mg->mg_flags &= ~MGf_MINMATCH;
1544 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1546 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1548 gv_efullname3(sv,((GV*)sv), "*");
1552 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1557 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1566 if (*s == '*' && s[1])
1568 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1573 GvGP(sv) = gp_ref(GvGP(gv));
1578 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1581 SV *lsv = LvTARG(sv);
1582 char *tmps = SvPV(lsv,len);
1583 I32 offs = LvTARGOFF(sv);
1584 I32 rem = LvTARGLEN(sv);
1587 sv_pos_u2b(lsv, &offs, &rem);
1588 if (offs > (I32)len)
1590 if (rem + offs > (I32)len)
1592 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1599 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1602 char *tmps = SvPV(sv, len);
1603 SV *lsv = LvTARG(sv);
1604 I32 lvoff = LvTARGOFF(sv);
1605 I32 lvlen = LvTARGLEN(sv);
1608 sv_utf8_upgrade(lsv);
1609 sv_pos_u2b(lsv, &lvoff, &lvlen);
1610 sv_insert(lsv, lvoff, lvlen, tmps, len);
1613 else if (lsv && SvUTF8(lsv)) {
1614 sv_pos_u2b(lsv, &lvoff, &lvlen);
1615 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
1616 sv_insert(lsv, lvoff, lvlen, tmps, len);
1620 sv_insert(lsv, lvoff, lvlen, tmps, len);
1626 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1628 TAINT_IF((mg->mg_len & 1) ||
1629 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1634 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1636 if (PL_localizing) {
1637 if (PL_localizing == 1)
1642 else if (PL_tainted)
1650 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1652 SV *lsv = LvTARG(sv);
1659 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1664 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1666 do_vecset(sv); /* XXX slurp this routine */
1671 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1674 if (LvTARGLEN(sv)) {
1676 SV *ahv = LvTARG(sv);
1677 if (SvTYPE(ahv) == SVt_PVHV) {
1678 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1683 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1689 AV* av = (AV*)LvTARG(sv);
1690 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1691 targ = AvARRAY(av)[LvTARGOFF(sv)];
1693 if (targ && targ != &PL_sv_undef) {
1694 /* somebody else defined it for us */
1695 SvREFCNT_dec(LvTARG(sv));
1696 LvTARG(sv) = SvREFCNT_inc(targ);
1698 SvREFCNT_dec(mg->mg_obj);
1699 mg->mg_obj = Nullsv;
1700 mg->mg_flags &= ~MGf_REFCOUNTED;
1705 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1710 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1715 sv_setsv(LvTARG(sv), sv);
1716 SvSETMAGIC(LvTARG(sv));
1722 Perl_vivify_defelem(pTHX_ SV *sv)
1727 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
1730 SV *ahv = LvTARG(sv);
1732 if (SvTYPE(ahv) == SVt_PVHV) {
1733 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1738 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
1742 if (!value || value == &PL_sv_undef)
1743 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1746 AV* av = (AV*)LvTARG(sv);
1747 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1748 LvTARG(sv) = Nullsv; /* array can't be extended */
1750 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1751 if (!svp || (value = *svp) == &PL_sv_undef)
1752 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1755 (void)SvREFCNT_inc(value);
1756 SvREFCNT_dec(LvTARG(sv));
1759 SvREFCNT_dec(mg->mg_obj);
1760 mg->mg_obj = Nullsv;
1761 mg->mg_flags &= ~MGf_REFCOUNTED;
1765 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1767 AV *av = (AV*)mg->mg_obj;
1768 SV **svp = AvARRAY(av);
1769 I32 i = AvFILLp(av);
1771 if (svp[i] && svp[i] != &PL_sv_undef) {
1772 if (!SvWEAKREF(svp[i]))
1773 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1774 /* XXX Should we check that it hasn't changed? */
1776 (void)SvOK_off(svp[i]);
1777 SvWEAKREF_off(svp[i]);
1778 svp[i] = &PL_sv_undef;
1786 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1794 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1796 sv_unmagic(sv, PERL_MAGIC_bm);
1802 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1804 sv_unmagic(sv, PERL_MAGIC_fm);
1810 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1812 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1814 if (uf && uf->uf_set)
1815 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
1820 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1822 regexp *re = (regexp *)mg->mg_obj;
1827 #ifdef USE_LOCALE_COLLATE
1829 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1832 * RenE<eacute> Descartes said "I think not."
1833 * and vanished with a faint plop.
1836 Safefree(mg->mg_ptr);
1842 #endif /* USE_LOCALE_COLLATE */
1845 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1850 switch (*mg->mg_ptr) {
1851 case '\001': /* ^A */
1852 sv_setsv(PL_bodytarget, sv);
1854 case '\003': /* ^C */
1855 PL_minus_c = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1858 case '\004': /* ^D */
1859 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
1860 DEBUG_x(dump_all());
1862 case '\005': /* ^E */
1863 if (*(mg->mg_ptr+1) == '\0') {
1864 #ifdef MACOS_TRADITIONAL
1865 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1868 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1871 SetLastError( SvIV(sv) );
1874 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1876 /* will anyone ever use this? */
1877 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1883 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
1885 sv_setsv(PL_encoding, sv);
1887 PL_encoding = newSVsv(sv);
1889 case '\006': /* ^F */
1890 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1892 case '\010': /* ^H */
1893 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1895 case '\011': /* ^I */ /* NOT \t in EBCDIC */
1897 Safefree(PL_inplace);
1899 PL_inplace = savepv(SvPV(sv,len));
1901 PL_inplace = Nullch;
1903 case '\017': /* ^O */
1904 if (*(mg->mg_ptr+1) == '\0') {
1906 Safefree(PL_osname);
1908 PL_osname = savepv(SvPV(sv,len));
1912 else if (strEQ(mg->mg_ptr, "\017PEN")) {
1913 if (!PL_compiling.cop_io)
1914 PL_compiling.cop_io = newSVsv(sv);
1916 sv_setsv(PL_compiling.cop_io,sv);
1919 case '\020': /* ^P */
1920 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1921 if (PL_perldb && !PL_DBsingle)
1924 case '\024': /* ^T */
1926 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1928 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1931 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
1932 if (*(mg->mg_ptr+1) == '\0') {
1933 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1934 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1935 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1936 | (i ? G_WARN_ON : G_WARN_OFF) ;
1939 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
1940 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1941 if (!SvPOK(sv) && PL_localizing) {
1942 sv_setpvn(sv, WARN_NONEstring, WARNsize);
1943 PL_compiling.cop_warnings = pWARN_NONE;
1948 int accumulate = 0 ;
1949 int any_fatals = 0 ;
1950 char * ptr = (char*)SvPV(sv, len) ;
1951 for (i = 0 ; i < len ; ++i) {
1952 accumulate |= ptr[i] ;
1953 any_fatals |= (ptr[i] & 0xAA) ;
1956 PL_compiling.cop_warnings = pWARN_NONE;
1957 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
1958 PL_compiling.cop_warnings = pWARN_ALL;
1959 PL_dowarn |= G_WARN_ONCE ;
1962 if (specialWARN(PL_compiling.cop_warnings))
1963 PL_compiling.cop_warnings = newSVsv(sv) ;
1965 sv_setsv(PL_compiling.cop_warnings, sv);
1966 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1967 PL_dowarn |= G_WARN_ONCE ;
1973 else if (strEQ(mg->mg_ptr+1, "IDE_SYSTEM_CALLS"))
1974 PL_widesyscalls = (bool)SvTRUE(sv);
1977 if (PL_localizing) {
1978 if (PL_localizing == 1)
1979 SAVESPTR(PL_last_in_gv);
1981 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1982 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
1985 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
1986 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1987 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1990 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
1991 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1992 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1995 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1998 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1999 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2000 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
2003 IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2007 IO *io = GvIOp(PL_defoutgv);
2010 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
2011 IoFLAGS(io) &= ~IOf_FLUSH;
2013 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2014 PerlIO *ofp = IoOFP(io);
2016 (void)PerlIO_flush(ofp);
2017 IoFLAGS(io) |= IOf_FLUSH;
2023 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2024 PL_multiline = (i != 0);
2027 SvREFCNT_dec(PL_rs);
2028 PL_rs = newSVsv(sv);
2032 SvREFCNT_dec(PL_ors_sv);
2033 if (SvOK(sv) || SvGMAGICAL(sv)) {
2034 PL_ors_sv = newSVsv(sv);
2042 SvREFCNT_dec(PL_ofs_sv);
2043 if (SvOK(sv) || SvGMAGICAL(sv)) {
2044 PL_ofs_sv = newSVsv(sv);
2053 PL_ofmt = savepv(SvPV(sv,len));
2056 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2059 #ifdef COMPLEX_STATUS
2060 if (PL_localizing == 2) {
2061 PL_statusvalue = LvTARGOFF(sv);
2062 PL_statusvalue_vms = LvTARGLEN(sv);
2066 #ifdef VMSISH_STATUS
2068 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
2071 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2074 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
2075 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
2078 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2079 if (PL_delaymagic) {
2080 PL_delaymagic |= DM_RUID;
2081 break; /* don't do magic till later */
2084 (void)setruid((Uid_t)PL_uid);
2087 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
2089 #ifdef HAS_SETRESUID
2090 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
2092 if (PL_uid == PL_euid) /* special case $< = $> */
2093 (void)PerlProc_setuid(PL_uid);
2095 PL_uid = PerlProc_getuid();
2096 Perl_croak(aTHX_ "setruid() not implemented");
2101 PL_uid = PerlProc_getuid();
2102 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2105 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2106 if (PL_delaymagic) {
2107 PL_delaymagic |= DM_EUID;
2108 break; /* don't do magic till later */
2111 (void)seteuid((Uid_t)PL_euid);
2114 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
2116 #ifdef HAS_SETRESUID
2117 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
2119 if (PL_euid == PL_uid) /* special case $> = $< */
2120 PerlProc_setuid(PL_euid);
2122 PL_euid = PerlProc_geteuid();
2123 Perl_croak(aTHX_ "seteuid() not implemented");
2128 PL_euid = PerlProc_geteuid();
2129 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2132 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2133 if (PL_delaymagic) {
2134 PL_delaymagic |= DM_RGID;
2135 break; /* don't do magic till later */
2138 (void)setrgid((Gid_t)PL_gid);
2141 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
2143 #ifdef HAS_SETRESGID
2144 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2146 if (PL_gid == PL_egid) /* special case $( = $) */
2147 (void)PerlProc_setgid(PL_gid);
2149 PL_gid = PerlProc_getgid();
2150 Perl_croak(aTHX_ "setrgid() not implemented");
2155 PL_gid = PerlProc_getgid();
2156 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2159 #ifdef HAS_SETGROUPS
2161 char *p = SvPV(sv, len);
2162 Groups_t gary[NGROUPS];
2167 for (i = 0; i < NGROUPS; ++i) {
2168 while (*p && !isSPACE(*p))
2177 (void)setgroups(i, gary);
2179 #else /* HAS_SETGROUPS */
2180 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2181 #endif /* HAS_SETGROUPS */
2182 if (PL_delaymagic) {
2183 PL_delaymagic |= DM_EGID;
2184 break; /* don't do magic till later */
2187 (void)setegid((Gid_t)PL_egid);
2190 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2192 #ifdef HAS_SETRESGID
2193 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2195 if (PL_egid == PL_gid) /* special case $) = $( */
2196 (void)PerlProc_setgid(PL_egid);
2198 PL_egid = PerlProc_getegid();
2199 Perl_croak(aTHX_ "setegid() not implemented");
2204 PL_egid = PerlProc_getegid();
2205 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2208 PL_chopset = SvPV_force(sv,len);
2210 #ifndef MACOS_TRADITIONAL
2212 #ifdef HAS_SETPROCTITLE
2213 /* The BSDs don't show the argv[] in ps(1) output, they
2214 * show a string from the process struct and provide
2215 * the setproctitle() routine to manipulate that. */
2218 # if __FreeBSD_version > 410001
2219 /* The leading "-" removes the "perl: " prefix,
2220 * but not the "(perl) suffix from the ps(1)
2221 * output, because that's what ps(1) shows if the
2222 * argv[] is modified. */
2223 setproctitle("-%s", s);
2224 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2225 /* This doesn't really work if you assume that
2226 * $0 = 'foobar'; will wipe out 'perl' from the $0
2227 * because in ps(1) output the result will be like
2228 * sprintf("perl: %s (perl)", s)
2229 * I guess this is a security feature:
2230 * one (a user process) cannot get rid of the original name.
2232 setproctitle("%s", s);
2239 /* See if all the arguments are contiguous in memory */
2240 for (i = 1; i < PL_origargc; i++) {
2241 if (PL_origargv[i] == s + 1
2243 || PL_origargv[i] == s + 2
2248 s += strlen(s); /* this one is ok too */
2253 /* can grab env area too? */
2256 && PL_curinterp == aTHX
2258 && (PL_origenviron[0] == s + 1))
2260 my_setenv("NoNe SuCh", Nullch);
2261 /* force copy of environment */
2262 for (i = 0; PL_origenviron[i]; i++)
2263 if (PL_origenviron[i] == s + 1) {
2270 PL_origalen = s - PL_origargv[0];
2272 s = SvPV_force(sv,len);
2274 if (i >= (I32)PL_origalen) {
2276 /* don't allow system to limit $0 seen by script */
2277 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2278 Copy(s, PL_origargv[0], i, char);
2279 s = PL_origargv[0]+i;
2283 Copy(s, PL_origargv[0], i, char);
2284 s = PL_origargv[0]+i;
2286 while (++i < (I32)PL_origalen)
2288 for (i = 1; i < PL_origargc; i++)
2289 PL_origargv[i] = Nullch;
2293 #ifdef USE_5005THREADS
2295 sv_setsv(thr->errsv, sv);
2297 #endif /* USE_5005THREADS */
2302 #ifdef USE_5005THREADS
2304 Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
2306 DEBUG_S(PerlIO_printf(Perl_debug_log,
2307 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2308 PTR2UV(thr), PTR2UV(sv)));
2310 Perl_croak(aTHX_ "panic: magic_mutexfree");
2311 MUTEX_DESTROY(MgMUTEXP(mg));
2312 COND_DESTROY(MgCONDP(mg));
2315 #endif /* USE_5005THREADS */
2318 Perl_whichsig(pTHX_ char *sig)
2320 register char **sigv;
2322 for (sigv = PL_sig_name+1; *sigv; sigv++)
2323 if (strEQ(sig,*sigv))
2324 return PL_sig_num[sigv - PL_sig_name];
2326 if (strEQ(sig,"CHLD"))
2330 if (strEQ(sig,"CLD"))
2336 #if !defined(PERL_IMPLICIT_CONTEXT)
2341 Perl_sighandler(int sig)
2343 #ifdef PERL_GET_SIG_CONTEXT
2344 dTHXa(PERL_GET_SIG_CONTEXT);
2351 SV *sv = Nullsv, *tSv = PL_Sv;
2357 if (PL_savestack_ix + 15 <= PL_savestack_max)
2359 if (PL_markstack_ptr < PL_markstack_max - 2)
2361 if (PL_retstack_ix < PL_retstack_max - 2)
2363 if (PL_scopestack_ix < PL_scopestack_max - 3)
2366 if (!PL_psig_ptr[sig]) {
2367 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
2372 /* Max number of items pushed there is 3*n or 4. We cannot fix
2373 infinity, so we fix 4 (in fact 5): */
2375 PL_savestack_ix += 5; /* Protect save in progress. */
2376 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2379 PL_markstack_ptr++; /* Protect mark. */
2382 PL_retstack[PL_retstack_ix] = NULL;
2385 PL_scopestack_ix += 1;
2386 /* sv_2cv is too complicated, try a simpler variant first: */
2387 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2388 || SvTYPE(cv) != SVt_PVCV)
2389 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2391 if (!cv || !CvROOT(cv)) {
2392 if (ckWARN(WARN_SIGNAL))
2393 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
2394 PL_sig_name[sig], (gv ? GvENAME(gv)
2401 if(PL_psig_name[sig]) {
2402 sv = SvREFCNT_inc(PL_psig_name[sig]);
2404 #if !defined(PERL_IMPLICIT_CONTEXT)
2408 sv = sv_newmortal();
2409 sv_setpv(sv,PL_sig_name[sig]);
2412 PUSHSTACKi(PERLSI_SIGNAL);
2417 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2420 if (SvTRUE(ERRSV)) {
2422 #ifdef HAS_SIGPROCMASK
2423 /* Handler "died", for example to get out of a restart-able read().
2424 * Before we re-do that on its behalf re-enable the signal which was
2425 * blocked by the system when we entered.
2429 sigaddset(&set,sig);
2430 sigprocmask(SIG_UNBLOCK, &set, NULL);
2432 /* Not clear if this will work */
2433 (void)rsignal(sig, SIG_IGN);
2434 (void)rsignal(sig, &Perl_csighandler);
2436 #endif /* !PERL_MICRO */
2437 Perl_die(aTHX_ Nullformat);
2441 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2447 PL_scopestack_ix -= 1;
2450 PL_op = myop; /* Apparently not needed... */
2452 PL_Sv = tSv; /* Restore global temporaries. */
2459 restore_magic(pTHX_ void *p)
2461 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2462 SV* sv = mgs->mgs_sv;
2467 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2470 SvFLAGS(sv) |= mgs->mgs_flags;
2474 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2477 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2479 /* If we're still on top of the stack, pop us off. (That condition
2480 * will be satisfied if restore_magic was called explicitly, but *not*
2481 * if it's being called via leave_scope.)
2482 * The reason for doing this is that otherwise, things like sv_2cv()
2483 * may leave alloc gunk on the savestack, and some code
2484 * (e.g. sighandler) doesn't expect that...
2486 if (PL_savestack_ix == mgs->mgs_ss_ix)
2488 I32 popval = SSPOPINT;
2489 assert(popval == SAVEt_DESTRUCTOR_X);
2490 PL_savestack_ix -= 2;
2492 assert(popval == SAVEt_ALLOC);
2494 PL_savestack_ix -= popval;
2500 unwind_handler_stack(pTHX_ void *p)
2502 U32 flags = *(U32*)p;
2505 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2506 /* cxstack_ix-- Not needed, die already unwound it. */
2507 #if !defined(PERL_IMPLICIT_CONTEXT)
2509 SvREFCNT_dec(sig_sv);