3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * "Sam sat on the ground and put his head in his hands. 'I wish I had never
13 * come here, and I don't want to see no more magic,' he said, and fell silent."
17 =head1 Magical Functions
24 #if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
34 # include <sys/pstat.h>
37 Signal_t Perl_csighandler(int sig);
39 /* if you only have signal() and it resets on each signal, FAKE_PERSISTENT_SIGNAL_HANDLERS fixes */
40 #if !defined(HAS_SIGACTION) && defined(VMS)
41 # define FAKE_PERSISTENT_SIGNAL_HANDLERS
43 /* if we're doing kill() with sys$sigprc on VMS, FAKE_DEFAULT_SIGNAL_HANDLERS */
44 #if defined(KILL_BY_SIGPRC)
45 # define FAKE_DEFAULT_SIGNAL_HANDLERS
48 static void restore_magic(pTHX_ void *p);
49 static void unwind_handler_stack(pTHX_ void *p);
52 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
60 /* MGS is typedef'ed to struct magic_state in perl.h */
63 S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
66 assert(SvMAGICAL(sv));
67 #ifdef PERL_COPY_ON_WRITE
68 /* Turning READONLY off for a copy-on-write scalar is a bad idea. */
73 SAVEDESTRUCTOR_X(restore_magic, INT2PTR(void*, (IV)mgs_ix));
75 mgs = SSPTR(mgs_ix, MGS*);
77 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
78 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
82 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
86 =for apidoc mg_magical
88 Turns on the magical status of an SV. See C<sv_magic>.
94 Perl_mg_magical(pTHX_ SV *sv)
97 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
98 MGVTBL* vtbl = mg->mg_virtual;
100 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
104 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
113 Do magic after a value is retrieved from the SV. See C<sv_magic>.
119 Perl_mg_get(pTHX_ SV *sv)
122 MAGIC *newmg, *head, *cur, *mg;
123 I32 mgs_ix = SSNEW(sizeof(MGS));
125 save_magic(mgs_ix, sv);
127 /* We must call svt_get(sv, mg) for each valid entry in the linked
128 list of magic. svt_get() may delete the current entry, add new
129 magic to the head of the list, or upgrade the SV. AMS 20010810 */
131 newmg = cur = head = mg = SvMAGIC(sv);
133 MGVTBL *vtbl = mg->mg_virtual;
135 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
136 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
138 /* guard against sv having been freed */
139 if (SvTYPE(sv) == SVTYPEMASK) {
140 Perl_croak(aTHX_ "Tied variable freed while still in use");
142 /* guard against magic having been deleted - eg FETCH calling
147 /* Don't restore the flags for this entry if it was deleted. */
148 if (mg->mg_flags & MGf_GSKIP)
149 (SSPTR(mgs_ix, MGS *))->mgs_flags = 0;
152 mg = mg->mg_moremagic;
155 /* Have we finished with the new entries we saw? Start again
156 where we left off (unless there are more new entries). */
164 /* Were any new entries added? */
165 if (!new && (newmg = SvMAGIC(sv)) != head) {
172 restore_magic(aTHX_ INT2PTR(void *, (IV)mgs_ix));
179 Do magic after a value is assigned to the SV. See C<sv_magic>.
185 Perl_mg_set(pTHX_ SV *sv)
191 mgs_ix = SSNEW(sizeof(MGS));
192 save_magic(mgs_ix, sv);
194 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
195 MGVTBL* vtbl = mg->mg_virtual;
196 nextmg = mg->mg_moremagic; /* it may delete itself */
197 if (mg->mg_flags & MGf_GSKIP) {
198 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
199 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
201 if (vtbl && vtbl->svt_set)
202 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
205 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
210 =for apidoc mg_length
212 Report on the SV's length. See C<sv_magic>.
218 Perl_mg_length(pTHX_ SV *sv)
223 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
224 MGVTBL* vtbl = mg->mg_virtual;
225 if (vtbl && vtbl->svt_len) {
228 mgs_ix = SSNEW(sizeof(MGS));
229 save_magic(mgs_ix, sv);
230 /* omit MGf_GSKIP -- not changed here */
231 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
232 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
239 U8 *s = (U8*)SvPV(sv, len);
240 len = Perl_utf8_length(aTHX_ s, s + len);
248 Perl_mg_size(pTHX_ SV *sv)
253 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
254 MGVTBL* vtbl = mg->mg_virtual;
255 if (vtbl && vtbl->svt_len) {
258 mgs_ix = SSNEW(sizeof(MGS));
259 save_magic(mgs_ix, sv);
260 /* omit MGf_GSKIP -- not changed here */
261 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
262 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
269 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
274 Perl_croak(aTHX_ "Size magic not implemented");
283 Clear something magical that the SV represents. See C<sv_magic>.
289 Perl_mg_clear(pTHX_ SV *sv)
294 mgs_ix = SSNEW(sizeof(MGS));
295 save_magic(mgs_ix, sv);
297 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
298 MGVTBL* vtbl = mg->mg_virtual;
299 /* omit GSKIP -- never set here */
301 if (vtbl && vtbl->svt_clear)
302 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
305 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
312 Finds the magic pointer for type matching the SV. See C<sv_magic>.
318 Perl_mg_find(pTHX_ SV *sv, int type)
323 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
324 if (mg->mg_type == type)
333 Copies the magic from one SV to another. See C<sv_magic>.
339 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
343 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
344 MGVTBL* vtbl = mg->mg_virtual;
345 if ((mg->mg_flags & MGf_COPY) && vtbl->svt_copy){
346 count += CALL_FPTR(vtbl->svt_copy)(aTHX_ sv, mg, nsv, key, klen);
348 else if (isUPPER(mg->mg_type)) {
350 mg->mg_type == PERL_MAGIC_tied ? SvTIED_obj(sv, mg) :
351 (mg->mg_type == PERL_MAGIC_regdata && mg->mg_obj)
353 toLOWER(mg->mg_type), key, klen);
363 Free any magic storage used by the SV. See C<sv_magic>.
369 Perl_mg_free(pTHX_ SV *sv)
373 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
374 MGVTBL* vtbl = mg->mg_virtual;
375 moremagic = mg->mg_moremagic;
376 if (vtbl && vtbl->svt_free)
377 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
378 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
380 Safefree(mg->mg_ptr);
381 else if (mg->mg_len == HEf_SVKEY)
382 SvREFCNT_dec((SV*)mg->mg_ptr);
384 if (mg->mg_flags & MGf_REFCOUNTED)
385 SvREFCNT_dec(mg->mg_obj);
393 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
398 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
402 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
403 if (mg->mg_obj) /* @+ */
406 return rx->lastparen;
413 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
421 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
425 if (paren <= (I32)rx->nparens &&
426 (s = rx->startp[paren]) != -1 &&
427 (t = rx->endp[paren]) != -1)
429 if (mg->mg_obj) /* @+ */
434 if (i > 0 && RX_MATCH_UTF8(rx)) {
435 char *b = rx->subbeg;
437 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
447 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
449 Perl_croak(aTHX_ PL_no_modify);
455 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
462 switch (*mg->mg_ptr) {
463 case '1': case '2': case '3': case '4':
464 case '5': case '6': case '7': case '8': case '9': case '&':
465 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
467 paren = atoi(mg->mg_ptr); /* $& is in [0] */
469 if (paren <= (I32)rx->nparens &&
470 (s1 = rx->startp[paren]) != -1 &&
471 (t1 = rx->endp[paren]) != -1)
475 if (i > 0 && RX_MATCH_UTF8(rx)) {
476 char *s = rx->subbeg + s1;
477 char *send = rx->subbeg + t1;
480 if (is_utf8_string((U8*)s, i))
481 i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send);
484 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
488 if (ckWARN(WARN_UNINITIALIZED))
493 if (ckWARN(WARN_UNINITIALIZED))
498 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
499 paren = rx->lastparen;
504 case '\016': /* ^N */
505 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
506 paren = rx->lastcloseparen;
512 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
513 if (rx->startp[0] != -1) {
524 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
525 if (rx->endp[0] != -1) {
526 i = rx->sublen - rx->endp[0];
537 if (!SvPOK(sv) && SvNIOK(sv)) {
547 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
550 register char *s = NULL;
554 switch (*mg->mg_ptr) {
555 case '\001': /* ^A */
556 sv_setsv(sv, PL_bodytarget);
558 case '\003': /* ^C */
559 sv_setiv(sv, (IV)PL_minus_c);
562 case '\004': /* ^D */
563 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
564 #if defined(YYDEBUG) && defined(DEBUGGING)
565 PL_yydebug = DEBUG_p_TEST;
568 case '\005': /* ^E */
569 if (*(mg->mg_ptr+1) == '\0') {
570 #ifdef MACOS_TRADITIONAL
574 sv_setnv(sv,(double)gMacPerl_OSErr);
575 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
580 # include <descrip.h>
581 # include <starlet.h>
583 $DESCRIPTOR(msgdsc,msg);
584 sv_setnv(sv,(NV) vaxc$errno);
585 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
586 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
592 if (!(_emx_env & 0x200)) { /* Under DOS */
593 sv_setnv(sv, (NV)errno);
594 sv_setpv(sv, errno ? Strerror(errno) : "");
596 if (errno != errno_isOS2) {
597 int tmp = _syserrno();
598 if (tmp) /* 2nd call to _syserrno() makes it 0 */
601 sv_setnv(sv, (NV)Perl_rc);
602 sv_setpv(sv, os2error(Perl_rc));
607 DWORD dwErr = GetLastError();
608 sv_setnv(sv, (NV)dwErr);
611 PerlProc_GetOSError(sv, dwErr);
618 sv_setnv(sv, (NV)errno);
619 sv_setpv(sv, errno ? Strerror(errno) : "");
624 SvNOK_on(sv); /* what a wonderful hack! */
626 else if (strEQ(mg->mg_ptr+1, "NCODING"))
627 sv_setsv(sv, PL_encoding);
629 case '\006': /* ^F */
630 sv_setiv(sv, (IV)PL_maxsysfd);
632 case '\010': /* ^H */
633 sv_setiv(sv, (IV)PL_hints);
635 case '\011': /* ^I */ /* NOT \t in EBCDIC */
637 sv_setpv(sv, PL_inplace);
639 sv_setsv(sv, &PL_sv_undef);
641 case '\017': /* ^O & ^OPEN */
642 if (*(mg->mg_ptr+1) == '\0')
643 sv_setpv(sv, PL_osname);
644 else if (strEQ(mg->mg_ptr, "\017PEN")) {
645 if (!PL_compiling.cop_io)
646 sv_setsv(sv, &PL_sv_undef);
648 sv_setsv(sv, PL_compiling.cop_io);
652 case '\020': /* ^P */
653 sv_setiv(sv, (IV)PL_perldb);
655 case '\023': /* ^S */
656 if (*(mg->mg_ptr+1) == '\0') {
657 if (PL_lex_state != LEX_NOTPARSING)
660 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
665 case '\024': /* ^T */
666 if (*(mg->mg_ptr+1) == '\0') {
668 sv_setnv(sv, PL_basetime);
670 sv_setiv(sv, (IV)PL_basetime);
673 else if (strEQ(mg->mg_ptr, "\024AINT"))
674 sv_setiv(sv, PL_tainting
675 ? (PL_taint_warn || PL_unsafe ? -1 : 1)
678 case '\025': /* $^UNICODE */
679 if (strEQ(mg->mg_ptr, "\025NICODE"))
680 sv_setuv(sv, (UV) PL_unicode);
682 case '\027': /* ^W & $^WARNING_BITS */
683 if (*(mg->mg_ptr+1) == '\0')
684 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
685 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
686 if (PL_compiling.cop_warnings == pWARN_NONE ||
687 PL_compiling.cop_warnings == pWARN_STD)
689 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
691 else if (PL_compiling.cop_warnings == pWARN_ALL) {
692 /* Get the bit mask for $warnings::Bits{all}, because
693 * it could have been extended by warnings::register */
695 HV *bits=get_hv("warnings::Bits", FALSE);
696 if (bits && (bits_all=hv_fetch(bits, "all", 3, FALSE))) {
697 sv_setsv(sv, *bits_all);
700 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
704 sv_setsv(sv, PL_compiling.cop_warnings);
709 case '1': case '2': case '3': case '4':
710 case '5': case '6': case '7': case '8': case '9': case '&':
711 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
715 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
716 * XXX Does the new way break anything?
718 paren = atoi(mg->mg_ptr); /* $& is in [0] */
720 if (paren <= (I32)rx->nparens &&
721 (s1 = rx->startp[paren]) != -1 &&
722 (t1 = rx->endp[paren]) != -1)
732 if (RX_MATCH_UTF8(rx) && is_utf8_string((U8*)s, i))
737 if (RX_MATCH_TAINTED(rx)) {
738 MAGIC* mg = SvMAGIC(sv);
741 SvMAGIC(sv) = mg->mg_moremagic;
743 if ((mgt = SvMAGIC(sv))) {
744 mg->mg_moremagic = mgt;
754 sv_setsv(sv,&PL_sv_undef);
757 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
758 paren = rx->lastparen;
762 sv_setsv(sv,&PL_sv_undef);
764 case '\016': /* ^N */
765 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
766 paren = rx->lastcloseparen;
770 sv_setsv(sv,&PL_sv_undef);
773 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
774 if ((s = rx->subbeg) && rx->startp[0] != -1) {
779 sv_setsv(sv,&PL_sv_undef);
782 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
783 if (rx->subbeg && rx->endp[0] != -1) {
784 s = rx->subbeg + rx->endp[0];
785 i = rx->sublen - rx->endp[0];
789 sv_setsv(sv,&PL_sv_undef);
793 if (GvIO(PL_last_in_gv)) {
794 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
800 sv_setiv(sv, (IV)STATUS_CURRENT);
801 #ifdef COMPLEX_STATUS
802 LvTARGOFF(sv) = PL_statusvalue;
803 LvTARGLEN(sv) = PL_statusvalue_vms;
808 if (GvIOp(PL_defoutgv))
809 s = IoTOP_NAME(GvIOp(PL_defoutgv));
813 sv_setpv(sv,GvENAME(PL_defoutgv));
818 if (GvIOp(PL_defoutgv))
819 s = IoFMT_NAME(GvIOp(PL_defoutgv));
821 s = GvENAME(PL_defoutgv);
826 if (GvIOp(PL_defoutgv))
827 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
830 if (GvIOp(PL_defoutgv))
831 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
834 if (GvIOp(PL_defoutgv))
835 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
843 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
846 if (GvIOp(PL_defoutgv))
847 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
853 sv_copypv(sv, PL_ors_sv);
856 sv_setpv(sv,PL_ofmt);
860 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
861 sv_setpv(sv, errno ? Strerror(errno) : "");
864 int saveerrno = errno;
865 sv_setnv(sv, (NV)errno);
867 if (errno == errno_isOS2 || errno == errno_isOS2_set)
868 sv_setpv(sv, os2error(Perl_rc));
871 sv_setpv(sv, errno ? Strerror(errno) : "");
875 SvNOK_on(sv); /* what a wonderful hack! */
878 sv_setiv(sv, (IV)PL_uid);
881 sv_setiv(sv, (IV)PL_euid);
884 sv_setiv(sv, (IV)PL_gid);
886 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
890 sv_setiv(sv, (IV)PL_egid);
892 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
897 Groups_t gary[NGROUPS];
898 i = getgroups(NGROUPS,gary);
900 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
903 (void)SvIOK_on(sv); /* what a wonderful hack! */
905 #ifndef MACOS_TRADITIONAL
914 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
916 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
918 if (uf && uf->uf_val)
919 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
924 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
934 #ifdef DYNAMIC_ENV_FETCH
935 /* We just undefd an environment var. Is a replacement */
936 /* waiting in the wings? */
939 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
940 s = SvPV(*valp, len);
944 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
945 /* And you'll never guess what the dog had */
946 /* in its mouth... */
948 MgTAINTEDDIR_off(mg);
950 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
951 char pathbuf[256], eltbuf[256], *cp, *elt = s;
955 do { /* DCL$PATH may be a search list */
956 while (1) { /* as may dev portion of any element */
957 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
958 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
959 cando_by_name(S_IWUSR,0,elt) ) {
964 if ((cp = strchr(elt, ':')) != Nullch)
966 if (my_trnlnm(elt, eltbuf, j++))
972 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
975 if (s && klen == 4 && strEQ(ptr,"PATH")) {
976 char *strend = s + len;
982 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
985 if (i >= sizeof tmpbuf /* too long -- assume the worst */
987 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
994 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
1000 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
1003 my_setenv(MgPV(mg,n_a),Nullch);
1008 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
1011 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1013 if (PL_localizing) {
1016 magic_clear_all_env(sv,mg);
1017 hv_iterinit((HV*)sv);
1018 while ((entry = hv_iternext((HV*)sv))) {
1020 my_setenv(hv_iterkey(entry, &keylen),
1021 SvPV(hv_iterval((HV*)sv, entry), n_a));
1029 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
1032 #if defined(VMS) || defined(EPOC)
1033 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1035 # if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
1038 # ifdef USE_ENVIRON_ARRAY
1039 # if defined(USE_ITHREADS)
1040 /* only the parent thread can clobber the process environment */
1041 if (PL_curinterp == aTHX)
1044 # ifndef PERL_USE_SAFE_PUTENV
1047 if (environ == PL_origenviron)
1048 environ = (char**)safesysmalloc(sizeof(char*));
1050 for (i = 0; environ[i]; i++)
1051 safesysfree(environ[i]);
1052 # endif /* PERL_USE_SAFE_PUTENV */
1054 environ[0] = Nullch;
1056 # endif /* USE_ENVIRON_ARRAY */
1057 # endif /* PERL_IMPLICIT_SYS || WIN32 */
1058 #endif /* VMS || EPOC */
1059 #endif /* !PERL_MICRO */
1063 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS)||defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1064 static int sig_handlers_initted = 0;
1066 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1067 static int sig_ignoring[SIG_SIZE]; /* which signals we are ignoring */
1069 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1070 static int sig_defaulting[SIG_SIZE];
1074 #ifdef HAS_SIGPROCMASK
1076 restore_sigmask(pTHX_ SV *save_sv)
1078 sigset_t *ossetp = (sigset_t *) SvPV_nolen( save_sv );
1079 (void)sigprocmask(SIG_SETMASK, ossetp, (sigset_t *)0);
1083 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
1087 /* Are we fetching a signal entry? */
1088 i = whichsig(MgPV(mg,n_a));
1091 sv_setsv(sv,PL_psig_ptr[i]);
1093 Sighandler_t sigstate;
1094 sigstate = rsignal_state(i);
1095 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1096 if (sig_handlers_initted && sig_ignoring[i]) sigstate = SIG_IGN;
1098 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1099 if (sig_handlers_initted && sig_defaulting[i]) sigstate = SIG_DFL;
1101 /* cache state so we don't fetch it again */
1102 if(sigstate == SIG_IGN)
1103 sv_setpv(sv,"IGNORE");
1105 sv_setsv(sv,&PL_sv_undef);
1106 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1113 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
1115 /* XXX Some of this code was copied from Perl_magic_setsig. A little
1116 * refactoring might be in order.
1124 if (strEQ(s,"__DIE__"))
1126 else if (strEQ(s,"__WARN__"))
1129 Perl_croak(aTHX_ "No such hook: %s", s);
1133 SvREFCNT_dec(to_dec);
1138 /* Are we clearing a signal entry? */
1141 #ifdef HAS_SIGPROCMASK
1144 /* Avoid having the signal arrive at a bad time, if possible. */
1147 sigprocmask(SIG_BLOCK, &set, &save);
1149 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1150 SAVEFREESV(save_sv);
1151 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1154 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1155 if (!sig_handlers_initted) Perl_csighandler_init();
1157 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1158 sig_defaulting[i] = 1;
1159 (void)rsignal(i, PL_csighandlerp);
1161 (void)rsignal(i, SIG_DFL);
1163 if(PL_psig_name[i]) {
1164 SvREFCNT_dec(PL_psig_name[i]);
1167 if(PL_psig_ptr[i]) {
1168 to_dec=PL_psig_ptr[i];
1171 SvREFCNT_dec(to_dec);
1181 Perl_raise_signal(pTHX_ int sig)
1183 /* Set a flag to say this signal is pending */
1184 PL_psig_pend[sig]++;
1185 /* And one to say _a_ signal is pending */
1190 Perl_csighandler(int sig)
1192 #ifdef PERL_GET_SIG_CONTEXT
1193 dTHXa(PERL_GET_SIG_CONTEXT);
1197 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1198 (void) rsignal(sig, PL_csighandlerp);
1199 if (sig_ignoring[sig]) return;
1201 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1202 if (sig_defaulting[sig])
1203 #ifdef KILL_BY_SIGPRC
1204 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1209 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
1210 /* Call the perl level handler now--
1211 * with risk we may be in malloc() etc. */
1212 (*PL_sighandlerp)(sig);
1214 Perl_raise_signal(aTHX_ sig);
1217 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1219 Perl_csighandler_init(void)
1222 if (sig_handlers_initted) return;
1224 for (sig = 1; sig < SIG_SIZE; sig++) {
1225 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1227 sig_defaulting[sig] = 1;
1228 (void) rsignal(sig, PL_csighandlerp);
1230 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1231 sig_ignoring[sig] = 0;
1234 sig_handlers_initted = 1;
1239 Perl_despatch_signals(pTHX)
1243 for (sig = 1; sig < SIG_SIZE; sig++) {
1244 if (PL_psig_pend[sig]) {
1245 PERL_BLOCKSIG_ADD(set, sig);
1246 PL_psig_pend[sig] = 0;
1247 PERL_BLOCKSIG_BLOCK(set);
1248 (*PL_sighandlerp)(sig);
1249 PERL_BLOCKSIG_UNBLOCK(set);
1255 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1260 /* Need to be careful with SvREFCNT_dec(), because that can have side
1261 * effects (due to closures). We must make sure that the new disposition
1262 * is in place before it is called.
1266 #ifdef HAS_SIGPROCMASK
1273 if (strEQ(s,"__DIE__"))
1275 else if (strEQ(s,"__WARN__"))
1278 Perl_croak(aTHX_ "No such hook: %s", s);
1286 i = whichsig(s); /* ...no, a brick */
1288 if (ckWARN(WARN_SIGNAL))
1289 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
1292 #ifdef HAS_SIGPROCMASK
1293 /* Avoid having the signal arrive at a bad time, if possible. */
1296 sigprocmask(SIG_BLOCK, &set, &save);
1298 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1299 SAVEFREESV(save_sv);
1300 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1303 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1304 if (!sig_handlers_initted) Perl_csighandler_init();
1306 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1307 sig_ignoring[i] = 0;
1309 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1310 sig_defaulting[i] = 0;
1312 SvREFCNT_dec(PL_psig_name[i]);
1313 to_dec = PL_psig_ptr[i];
1314 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1315 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1316 PL_psig_name[i] = newSVpvn(s, len);
1317 SvREADONLY_on(PL_psig_name[i]);
1319 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1321 (void)rsignal(i, PL_csighandlerp);
1322 #ifdef HAS_SIGPROCMASK
1327 *svp = SvREFCNT_inc(sv);
1329 SvREFCNT_dec(to_dec);
1332 s = SvPV_force(sv,len);
1333 if (strEQ(s,"IGNORE")) {
1335 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1336 sig_ignoring[i] = 1;
1337 (void)rsignal(i, PL_csighandlerp);
1339 (void)rsignal(i, SIG_IGN);
1343 else if (strEQ(s,"DEFAULT") || !*s) {
1345 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1347 sig_defaulting[i] = 1;
1348 (void)rsignal(i, PL_csighandlerp);
1351 (void)rsignal(i, SIG_DFL);
1356 * We should warn if HINT_STRICT_REFS, but without
1357 * access to a known hint bit in a known OP, we can't
1358 * tell whether HINT_STRICT_REFS is in force or not.
1360 if (!strchr(s,':') && !strchr(s,'\''))
1361 sv_insert(sv, 0, 0, "main::", 6);
1363 (void)rsignal(i, PL_csighandlerp);
1365 *svp = SvREFCNT_inc(sv);
1367 #ifdef HAS_SIGPROCMASK
1372 SvREFCNT_dec(to_dec);
1375 #endif /* !PERL_MICRO */
1378 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1380 PL_sub_generation++;
1385 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1387 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1388 PL_amagic_generation++;
1394 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1396 HV *hv = (HV*)LvTARG(sv);
1400 (void) hv_iterinit(hv);
1401 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1404 while (hv_iternext(hv))
1409 sv_setiv(sv, (IV)i);
1414 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1417 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1422 /* caller is responsible for stack switching/cleanup */
1424 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1430 PUSHs(SvTIED_obj(sv, mg));
1433 if (mg->mg_len >= 0)
1434 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1435 else if (mg->mg_len == HEf_SVKEY)
1436 PUSHs((SV*)mg->mg_ptr);
1438 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1439 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1447 return call_method(meth, flags);
1451 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1457 PUSHSTACKi(PERLSI_MAGIC);
1459 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1460 sv_setsv(sv, *PL_stack_sp--);
1470 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1473 mg->mg_flags |= MGf_GSKIP;
1474 magic_methpack(sv,mg,"FETCH");
1479 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1483 PUSHSTACKi(PERLSI_MAGIC);
1484 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1491 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1493 return magic_methpack(sv,mg,"DELETE");
1498 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1505 PUSHSTACKi(PERLSI_MAGIC);
1506 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1507 sv = *PL_stack_sp--;
1508 retval = (U32) SvIV(sv)-1;
1517 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1522 PUSHSTACKi(PERLSI_MAGIC);
1524 XPUSHs(SvTIED_obj(sv, mg));
1526 call_method("CLEAR", G_SCALAR|G_DISCARD);
1533 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1536 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1540 PUSHSTACKi(PERLSI_MAGIC);
1543 PUSHs(SvTIED_obj(sv, mg));
1548 if (call_method(meth, G_SCALAR))
1549 sv_setsv(key, *PL_stack_sp--);
1558 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1560 return magic_methpack(sv,mg,"EXISTS");
1564 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1574 svp = av_fetch(GvAV(gv),
1575 atoi(MgPV(mg,n_a)), FALSE);
1576 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp)))) {
1577 /* set or clear breakpoint in the relevant control op */
1579 o->op_flags |= OPf_SPECIAL;
1581 o->op_flags &= ~OPf_SPECIAL;
1587 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1589 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1594 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1596 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1601 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1603 SV* lsv = LvTARG(sv);
1605 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1606 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1607 if (mg && mg->mg_len >= 0) {
1610 sv_pos_b2u(lsv, &i);
1611 sv_setiv(sv, i + PL_curcop->cop_arybase);
1620 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1622 SV* lsv = LvTARG(sv);
1629 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1630 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1634 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1635 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1637 else if (!SvOK(sv)) {
1641 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1643 pos = SvIV(sv) - PL_curcop->cop_arybase;
1646 ulen = sv_len_utf8(lsv);
1656 else if (pos > (SSize_t)len)
1661 sv_pos_u2b(lsv, &p, 0);
1666 mg->mg_flags &= ~MGf_MINMATCH;
1672 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1674 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1676 gv_efullname3(sv,((GV*)sv), "*");
1680 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1685 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1694 if (*s == '*' && s[1])
1696 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1701 GvGP(sv) = gp_ref(GvGP(gv));
1706 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1709 SV *lsv = LvTARG(sv);
1710 char *tmps = SvPV(lsv,len);
1711 I32 offs = LvTARGOFF(sv);
1712 I32 rem = LvTARGLEN(sv);
1715 sv_pos_u2b(lsv, &offs, &rem);
1716 if (offs > (I32)len)
1718 if (rem + offs > (I32)len)
1720 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1727 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1730 char *tmps = SvPV(sv, len);
1731 SV *lsv = LvTARG(sv);
1732 I32 lvoff = LvTARGOFF(sv);
1733 I32 lvlen = LvTARGLEN(sv);
1736 sv_utf8_upgrade(lsv);
1737 sv_pos_u2b(lsv, &lvoff, &lvlen);
1738 sv_insert(lsv, lvoff, lvlen, tmps, len);
1741 else if (lsv && SvUTF8(lsv)) {
1742 sv_pos_u2b(lsv, &lvoff, &lvlen);
1743 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
1744 sv_insert(lsv, lvoff, lvlen, tmps, len);
1748 sv_insert(lsv, lvoff, lvlen, tmps, len);
1754 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1756 TAINT_IF((mg->mg_len & 1) ||
1757 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1762 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1764 if (PL_localizing) {
1765 if (PL_localizing == 1)
1770 else if (PL_tainted)
1778 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1780 SV *lsv = LvTARG(sv);
1787 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1792 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1794 do_vecset(sv); /* XXX slurp this routine */
1799 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1802 if (LvTARGLEN(sv)) {
1804 SV *ahv = LvTARG(sv);
1805 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1810 AV* av = (AV*)LvTARG(sv);
1811 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1812 targ = AvARRAY(av)[LvTARGOFF(sv)];
1814 if (targ && targ != &PL_sv_undef) {
1815 /* somebody else defined it for us */
1816 SvREFCNT_dec(LvTARG(sv));
1817 LvTARG(sv) = SvREFCNT_inc(targ);
1819 SvREFCNT_dec(mg->mg_obj);
1820 mg->mg_obj = Nullsv;
1821 mg->mg_flags &= ~MGf_REFCOUNTED;
1826 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1831 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1836 sv_setsv(LvTARG(sv), sv);
1837 SvSETMAGIC(LvTARG(sv));
1843 Perl_vivify_defelem(pTHX_ SV *sv)
1848 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
1851 SV *ahv = LvTARG(sv);
1853 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1856 if (!value || value == &PL_sv_undef)
1857 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1860 AV* av = (AV*)LvTARG(sv);
1861 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1862 LvTARG(sv) = Nullsv; /* array can't be extended */
1864 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1865 if (!svp || (value = *svp) == &PL_sv_undef)
1866 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1869 (void)SvREFCNT_inc(value);
1870 SvREFCNT_dec(LvTARG(sv));
1873 SvREFCNT_dec(mg->mg_obj);
1874 mg->mg_obj = Nullsv;
1875 mg->mg_flags &= ~MGf_REFCOUNTED;
1879 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1881 AV *av = (AV*)mg->mg_obj;
1882 SV **svp = AvARRAY(av);
1883 I32 i = AvFILLp(av);
1885 if (svp[i] && svp[i] != &PL_sv_undef) {
1886 if (!SvWEAKREF(svp[i]))
1887 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1888 /* XXX Should we check that it hasn't changed? */
1890 (void)SvOK_off(svp[i]);
1891 SvWEAKREF_off(svp[i]);
1892 svp[i] = &PL_sv_undef;
1896 SvREFCNT_dec(av); /* remove extra count added by sv_add_backref() */
1901 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1909 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1911 sv_unmagic(sv, PERL_MAGIC_bm);
1917 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1919 sv_unmagic(sv, PERL_MAGIC_fm);
1925 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1927 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1929 if (uf && uf->uf_set)
1930 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
1935 Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
1937 sv_unmagic(sv, PERL_MAGIC_qr);
1942 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1944 regexp *re = (regexp *)mg->mg_obj;
1949 #ifdef USE_LOCALE_COLLATE
1951 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1954 * RenE<eacute> Descartes said "I think not."
1955 * and vanished with a faint plop.
1958 Safefree(mg->mg_ptr);
1964 #endif /* USE_LOCALE_COLLATE */
1966 /* Just clear the UTF-8 cache data. */
1968 Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
1970 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
1972 mg->mg_len = -1; /* The mg_len holds the len cache. */
1977 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1982 switch (*mg->mg_ptr) {
1983 case '\001': /* ^A */
1984 sv_setsv(PL_bodytarget, sv);
1986 case '\003': /* ^C */
1987 PL_minus_c = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1990 case '\004': /* ^D */
1993 PL_debug = get_debug_opts(&s) | DEBUG_TOP_FLAG;
1994 DEBUG_x(dump_all());
1996 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
1999 case '\005': /* ^E */
2000 if (*(mg->mg_ptr+1) == '\0') {
2001 #ifdef MACOS_TRADITIONAL
2002 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2005 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2008 SetLastError( SvIV(sv) );
2011 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2013 /* will anyone ever use this? */
2014 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
2020 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
2022 SvREFCNT_dec(PL_encoding);
2023 if (SvOK(sv) || SvGMAGICAL(sv)) {
2024 PL_encoding = newSVsv(sv);
2027 PL_encoding = Nullsv;
2031 case '\006': /* ^F */
2032 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2034 case '\010': /* ^H */
2035 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2037 case '\011': /* ^I */ /* NOT \t in EBCDIC */
2039 Safefree(PL_inplace);
2041 PL_inplace = savepv(SvPV(sv,len));
2043 PL_inplace = Nullch;
2045 case '\017': /* ^O */
2046 if (*(mg->mg_ptr+1) == '\0') {
2048 Safefree(PL_osname);
2050 PL_osname = savepv(SvPV(sv,len));
2054 else if (strEQ(mg->mg_ptr, "\017PEN")) {
2055 if (!PL_compiling.cop_io)
2056 PL_compiling.cop_io = newSVsv(sv);
2058 sv_setsv(PL_compiling.cop_io,sv);
2061 case '\020': /* ^P */
2062 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2063 if (PL_perldb && !PL_DBsingle)
2066 case '\024': /* ^T */
2068 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
2070 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2073 case '\027': /* ^W & $^WARNING_BITS */
2074 if (*(mg->mg_ptr+1) == '\0') {
2075 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2076 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2077 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
2078 | (i ? G_WARN_ON : G_WARN_OFF) ;
2081 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
2082 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2083 if (!SvPOK(sv) && PL_localizing) {
2084 sv_setpvn(sv, WARN_NONEstring, WARNsize);
2085 PL_compiling.cop_warnings = pWARN_NONE;
2090 int accumulate = 0 ;
2091 int any_fatals = 0 ;
2092 char * ptr = (char*)SvPV(sv, len) ;
2093 for (i = 0 ; i < len ; ++i) {
2094 accumulate |= ptr[i] ;
2095 any_fatals |= (ptr[i] & 0xAA) ;
2098 PL_compiling.cop_warnings = pWARN_NONE;
2099 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
2100 PL_compiling.cop_warnings = pWARN_ALL;
2101 PL_dowarn |= G_WARN_ONCE ;
2104 if (specialWARN(PL_compiling.cop_warnings))
2105 PL_compiling.cop_warnings = newSVsv(sv) ;
2107 sv_setsv(PL_compiling.cop_warnings, sv);
2108 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2109 PL_dowarn |= G_WARN_ONCE ;
2117 if (PL_localizing) {
2118 if (PL_localizing == 1)
2119 SAVESPTR(PL_last_in_gv);
2121 else if (SvOK(sv) && GvIO(PL_last_in_gv))
2122 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
2125 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2126 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
2127 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
2130 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2131 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
2132 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
2135 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2138 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2139 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2140 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
2143 IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2147 IO *io = GvIOp(PL_defoutgv);
2150 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
2151 IoFLAGS(io) &= ~IOf_FLUSH;
2153 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2154 PerlIO *ofp = IoOFP(io);
2156 (void)PerlIO_flush(ofp);
2157 IoFLAGS(io) |= IOf_FLUSH;
2163 SvREFCNT_dec(PL_rs);
2164 PL_rs = newSVsv(sv);
2168 SvREFCNT_dec(PL_ors_sv);
2169 if (SvOK(sv) || SvGMAGICAL(sv)) {
2170 PL_ors_sv = newSVsv(sv);
2178 SvREFCNT_dec(PL_ofs_sv);
2179 if (SvOK(sv) || SvGMAGICAL(sv)) {
2180 PL_ofs_sv = newSVsv(sv);
2189 PL_ofmt = savepv(SvPV(sv,len));
2192 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2195 #ifdef COMPLEX_STATUS
2196 if (PL_localizing == 2) {
2197 PL_statusvalue = LvTARGOFF(sv);
2198 PL_statusvalue_vms = LvTARGLEN(sv);
2202 #ifdef VMSISH_STATUS
2204 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
2207 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2212 # define PERL_VMS_BANG vaxc$errno
2214 # define PERL_VMS_BANG 0
2216 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
2217 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
2221 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2222 if (PL_delaymagic) {
2223 PL_delaymagic |= DM_RUID;
2224 break; /* don't do magic till later */
2227 (void)setruid((Uid_t)PL_uid);
2230 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
2232 #ifdef HAS_SETRESUID
2233 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
2235 if (PL_uid == PL_euid) /* special case $< = $> */
2236 (void)PerlProc_setuid(PL_uid);
2238 PL_uid = PerlProc_getuid();
2239 Perl_croak(aTHX_ "setruid() not implemented");
2244 PL_uid = PerlProc_getuid();
2245 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2248 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2249 if (PL_delaymagic) {
2250 PL_delaymagic |= DM_EUID;
2251 break; /* don't do magic till later */
2254 (void)seteuid((Uid_t)PL_euid);
2257 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
2259 #ifdef HAS_SETRESUID
2260 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
2262 if (PL_euid == PL_uid) /* special case $> = $< */
2263 PerlProc_setuid(PL_euid);
2265 PL_euid = PerlProc_geteuid();
2266 Perl_croak(aTHX_ "seteuid() not implemented");
2271 PL_euid = PerlProc_geteuid();
2272 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2275 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2276 if (PL_delaymagic) {
2277 PL_delaymagic |= DM_RGID;
2278 break; /* don't do magic till later */
2281 (void)setrgid((Gid_t)PL_gid);
2284 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
2286 #ifdef HAS_SETRESGID
2287 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2289 if (PL_gid == PL_egid) /* special case $( = $) */
2290 (void)PerlProc_setgid(PL_gid);
2292 PL_gid = PerlProc_getgid();
2293 Perl_croak(aTHX_ "setrgid() not implemented");
2298 PL_gid = PerlProc_getgid();
2299 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2302 #ifdef HAS_SETGROUPS
2304 char *p = SvPV(sv, len);
2305 Groups_t gary[NGROUPS];
2310 for (i = 0; i < NGROUPS; ++i) {
2311 while (*p && !isSPACE(*p))
2320 (void)setgroups(i, gary);
2322 #else /* HAS_SETGROUPS */
2323 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2324 #endif /* HAS_SETGROUPS */
2325 if (PL_delaymagic) {
2326 PL_delaymagic |= DM_EGID;
2327 break; /* don't do magic till later */
2330 (void)setegid((Gid_t)PL_egid);
2333 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2335 #ifdef HAS_SETRESGID
2336 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2338 if (PL_egid == PL_gid) /* special case $) = $( */
2339 (void)PerlProc_setgid(PL_egid);
2341 PL_egid = PerlProc_getegid();
2342 Perl_croak(aTHX_ "setegid() not implemented");
2347 PL_egid = PerlProc_getegid();
2348 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2351 PL_chopset = SvPV_force(sv,len);
2353 #ifndef MACOS_TRADITIONAL
2355 LOCK_DOLLARZERO_MUTEX;
2356 #ifdef HAS_SETPROCTITLE
2357 /* The BSDs don't show the argv[] in ps(1) output, they
2358 * show a string from the process struct and provide
2359 * the setproctitle() routine to manipulate that. */
2362 # if __FreeBSD_version > 410001
2363 /* The leading "-" removes the "perl: " prefix,
2364 * but not the "(perl) suffix from the ps(1)
2365 * output, because that's what ps(1) shows if the
2366 * argv[] is modified. */
2367 setproctitle("-%s", s);
2368 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2369 /* This doesn't really work if you assume that
2370 * $0 = 'foobar'; will wipe out 'perl' from the $0
2371 * because in ps(1) output the result will be like
2372 * sprintf("perl: %s (perl)", s)
2373 * I guess this is a security feature:
2374 * one (a user process) cannot get rid of the original name.
2376 setproctitle("%s", s);
2380 #if defined(__hpux) && defined(PSTAT_SETCMD)
2385 pstat(PSTAT_SETCMD, un, len, 0, 0);
2388 /* PL_origalen is set in perl_parse(). */
2389 s = SvPV_force(sv,len);
2390 if (len >= (STRLEN)PL_origalen) {
2391 /* Longer than original, will be truncated. */
2392 Copy(s, PL_origargv[0], PL_origalen, char);
2393 PL_origargv[0][PL_origalen - 1] = 0;
2396 /* Shorter than original, will be padded. */
2397 Copy(s, PL_origargv[0], len, char);
2398 PL_origargv[0][len] = 0;
2399 memset(PL_origargv[0] + len + 1,
2400 /* Is the space counterintuitive? Yes.
2401 * (You were expecting \0?)
2402 * Does it work? Seems to. (In Linux 2.4.20 at least.)
2405 PL_origalen - len - 1);
2406 for (i = 1; i < PL_origargc; i++)
2409 UNLOCK_DOLLARZERO_MUTEX;
2417 Perl_whichsig(pTHX_ char *sig)
2419 register char **sigv;
2421 for (sigv = PL_sig_name; *sigv; sigv++)
2422 if (strEQ(sig,*sigv))
2423 return PL_sig_num[sigv - PL_sig_name];
2425 if (strEQ(sig,"CHLD"))
2429 if (strEQ(sig,"CLD"))
2435 #if !defined(PERL_IMPLICIT_CONTEXT)
2440 Perl_sighandler(int sig)
2442 #ifdef PERL_GET_SIG_CONTEXT
2443 dTHXa(PERL_GET_SIG_CONTEXT);
2450 SV *sv = Nullsv, *tSv = PL_Sv;
2456 if (PL_savestack_ix + 15 <= PL_savestack_max)
2458 if (PL_markstack_ptr < PL_markstack_max - 2)
2460 if (PL_retstack_ix < PL_retstack_max - 2)
2462 if (PL_scopestack_ix < PL_scopestack_max - 3)
2465 if (!PL_psig_ptr[sig]) {
2466 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
2471 /* Max number of items pushed there is 3*n or 4. We cannot fix
2472 infinity, so we fix 4 (in fact 5): */
2474 PL_savestack_ix += 5; /* Protect save in progress. */
2475 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2478 PL_markstack_ptr++; /* Protect mark. */
2481 PL_retstack[PL_retstack_ix] = NULL;
2484 PL_scopestack_ix += 1;
2485 /* sv_2cv is too complicated, try a simpler variant first: */
2486 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2487 || SvTYPE(cv) != SVt_PVCV)
2488 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2490 if (!cv || !CvROOT(cv)) {
2491 if (ckWARN(WARN_SIGNAL))
2492 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
2493 PL_sig_name[sig], (gv ? GvENAME(gv)
2500 if(PL_psig_name[sig]) {
2501 sv = SvREFCNT_inc(PL_psig_name[sig]);
2503 #if !defined(PERL_IMPLICIT_CONTEXT)
2507 sv = sv_newmortal();
2508 sv_setpv(sv,PL_sig_name[sig]);
2511 PUSHSTACKi(PERLSI_SIGNAL);
2516 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2519 if (SvTRUE(ERRSV)) {
2521 #ifdef HAS_SIGPROCMASK
2522 /* Handler "died", for example to get out of a restart-able read().
2523 * Before we re-do that on its behalf re-enable the signal which was
2524 * blocked by the system when we entered.
2528 sigaddset(&set,sig);
2529 sigprocmask(SIG_UNBLOCK, &set, NULL);
2531 /* Not clear if this will work */
2532 (void)rsignal(sig, SIG_IGN);
2533 (void)rsignal(sig, PL_csighandlerp);
2535 #endif /* !PERL_MICRO */
2536 Perl_die(aTHX_ Nullformat);
2540 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2546 PL_scopestack_ix -= 1;
2549 PL_op = myop; /* Apparently not needed... */
2551 PL_Sv = tSv; /* Restore global temporaries. */
2558 restore_magic(pTHX_ void *p)
2560 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2561 SV* sv = mgs->mgs_sv;
2566 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2569 SvFLAGS(sv) |= mgs->mgs_flags;
2573 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2576 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2578 /* If we're still on top of the stack, pop us off. (That condition
2579 * will be satisfied if restore_magic was called explicitly, but *not*
2580 * if it's being called via leave_scope.)
2581 * The reason for doing this is that otherwise, things like sv_2cv()
2582 * may leave alloc gunk on the savestack, and some code
2583 * (e.g. sighandler) doesn't expect that...
2585 if (PL_savestack_ix == mgs->mgs_ss_ix)
2587 I32 popval = SSPOPINT;
2588 assert(popval == SAVEt_DESTRUCTOR_X);
2589 PL_savestack_ix -= 2;
2591 assert(popval == SAVEt_ALLOC);
2593 PL_savestack_ix -= popval;
2599 unwind_handler_stack(pTHX_ void *p)
2601 U32 flags = *(U32*)p;
2604 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2605 /* cxstack_ix-- Not needed, die already unwound it. */
2606 #if !defined(PERL_IMPLICIT_CONTEXT)
2608 SvREFCNT_dec(sig_sv);