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)
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 s = IoTOP_NAME(GvIOp(PL_defoutgv));
812 sv_setpv(sv,GvENAME(PL_defoutgv));
817 s = IoFMT_NAME(GvIOp(PL_defoutgv));
819 s = GvENAME(PL_defoutgv);
824 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
827 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
830 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
838 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
841 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
847 sv_copypv(sv, PL_ors_sv);
850 sv_setpv(sv,PL_ofmt);
854 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
855 sv_setpv(sv, errno ? Strerror(errno) : "");
858 int saveerrno = errno;
859 sv_setnv(sv, (NV)errno);
861 if (errno == errno_isOS2 || errno == errno_isOS2_set)
862 sv_setpv(sv, os2error(Perl_rc));
865 sv_setpv(sv, errno ? Strerror(errno) : "");
869 SvNOK_on(sv); /* what a wonderful hack! */
872 sv_setiv(sv, (IV)PL_uid);
875 sv_setiv(sv, (IV)PL_euid);
878 sv_setiv(sv, (IV)PL_gid);
880 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
884 sv_setiv(sv, (IV)PL_egid);
886 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
891 Groups_t gary[NGROUPS];
892 i = getgroups(NGROUPS,gary);
894 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
897 (void)SvIOK_on(sv); /* what a wonderful hack! */
899 #ifndef MACOS_TRADITIONAL
908 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
910 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
912 if (uf && uf->uf_val)
913 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
918 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
928 #ifdef DYNAMIC_ENV_FETCH
929 /* We just undefd an environment var. Is a replacement */
930 /* waiting in the wings? */
933 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
934 s = SvPV(*valp, len);
938 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
939 /* And you'll never guess what the dog had */
940 /* in its mouth... */
942 MgTAINTEDDIR_off(mg);
944 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
945 char pathbuf[256], eltbuf[256], *cp, *elt = s;
949 do { /* DCL$PATH may be a search list */
950 while (1) { /* as may dev portion of any element */
951 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
952 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
953 cando_by_name(S_IWUSR,0,elt) ) {
958 if ((cp = strchr(elt, ':')) != Nullch)
960 if (my_trnlnm(elt, eltbuf, j++))
966 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
969 if (s && klen == 4 && strEQ(ptr,"PATH")) {
970 char *strend = s + len;
976 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
979 if (i >= sizeof tmpbuf /* too long -- assume the worst */
981 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
988 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
994 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
997 my_setenv(MgPV(mg,n_a),Nullch);
1002 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
1005 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1007 if (PL_localizing) {
1010 magic_clear_all_env(sv,mg);
1011 hv_iterinit((HV*)sv);
1012 while ((entry = hv_iternext((HV*)sv))) {
1014 my_setenv(hv_iterkey(entry, &keylen),
1015 SvPV(hv_iterval((HV*)sv, entry), n_a));
1023 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
1026 #if defined(VMS) || defined(EPOC)
1027 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1029 # if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
1032 # ifdef USE_ENVIRON_ARRAY
1033 # if defined(USE_ITHREADS)
1034 /* only the parent thread can clobber the process environment */
1035 if (PL_curinterp == aTHX)
1038 # ifndef PERL_USE_SAFE_PUTENV
1041 if (environ == PL_origenviron)
1042 environ = (char**)safesysmalloc(sizeof(char*));
1044 for (i = 0; environ[i]; i++)
1045 safesysfree(environ[i]);
1046 # endif /* PERL_USE_SAFE_PUTENV */
1048 environ[0] = Nullch;
1050 # endif /* USE_ENVIRON_ARRAY */
1051 # endif /* PERL_IMPLICIT_SYS || WIN32 */
1052 #endif /* VMS || EPOC */
1053 #endif /* !PERL_MICRO */
1057 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS)||defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1058 static int sig_handlers_initted = 0;
1060 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1061 static int sig_ignoring[SIG_SIZE]; /* which signals we are ignoring */
1063 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1064 static int sig_defaulting[SIG_SIZE];
1068 #ifdef HAS_SIGPROCMASK
1070 restore_sigmask(pTHX_ SV *save_sv)
1072 sigset_t *ossetp = (sigset_t *) SvPV_nolen( save_sv );
1073 (void)sigprocmask(SIG_SETMASK, ossetp, (sigset_t *)0);
1077 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
1081 /* Are we fetching a signal entry? */
1082 i = whichsig(MgPV(mg,n_a));
1085 sv_setsv(sv,PL_psig_ptr[i]);
1087 Sighandler_t sigstate;
1088 sigstate = rsignal_state(i);
1089 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1090 if (sig_handlers_initted && sig_ignoring[i]) sigstate = SIG_IGN;
1092 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1093 if (sig_handlers_initted && sig_defaulting[i]) sigstate = SIG_DFL;
1095 /* cache state so we don't fetch it again */
1096 if(sigstate == SIG_IGN)
1097 sv_setpv(sv,"IGNORE");
1099 sv_setsv(sv,&PL_sv_undef);
1100 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1107 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
1109 /* XXX Some of this code was copied from Perl_magic_setsig. A little
1110 * refactoring might be in order.
1118 if (strEQ(s,"__DIE__"))
1120 else if (strEQ(s,"__WARN__"))
1123 Perl_croak(aTHX_ "No such hook: %s", s);
1127 SvREFCNT_dec(to_dec);
1132 /* Are we clearing a signal entry? */
1135 #ifdef HAS_SIGPROCMASK
1138 /* Avoid having the signal arrive at a bad time, if possible. */
1141 sigprocmask(SIG_BLOCK, &set, &save);
1143 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1144 SAVEFREESV(save_sv);
1145 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1148 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1149 if (!sig_handlers_initted) Perl_csighandler_init();
1151 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1152 sig_defaulting[i] = 1;
1153 (void)rsignal(i, PL_csighandlerp);
1155 (void)rsignal(i, SIG_DFL);
1157 if(PL_psig_name[i]) {
1158 SvREFCNT_dec(PL_psig_name[i]);
1161 if(PL_psig_ptr[i]) {
1162 to_dec=PL_psig_ptr[i];
1165 SvREFCNT_dec(to_dec);
1175 Perl_raise_signal(pTHX_ int sig)
1177 /* Set a flag to say this signal is pending */
1178 PL_psig_pend[sig]++;
1179 /* And one to say _a_ signal is pending */
1184 Perl_csighandler(int sig)
1186 #ifdef PERL_GET_SIG_CONTEXT
1187 dTHXa(PERL_GET_SIG_CONTEXT);
1191 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1192 (void) rsignal(sig, PL_csighandlerp);
1193 if (sig_ignoring[sig]) return;
1195 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1196 if (sig_defaulting[sig])
1197 #ifdef KILL_BY_SIGPRC
1198 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1203 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
1204 /* Call the perl level handler now--
1205 * with risk we may be in malloc() etc. */
1206 (*PL_sighandlerp)(sig);
1208 Perl_raise_signal(aTHX_ sig);
1211 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1213 Perl_csighandler_init(void)
1216 if (sig_handlers_initted) return;
1218 for (sig = 1; sig < SIG_SIZE; sig++) {
1219 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1221 sig_defaulting[sig] = 1;
1222 (void) rsignal(sig, PL_csighandlerp);
1224 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1225 sig_ignoring[sig] = 0;
1228 sig_handlers_initted = 1;
1233 Perl_despatch_signals(pTHX)
1237 for (sig = 1; sig < SIG_SIZE; sig++) {
1238 if (PL_psig_pend[sig]) {
1239 PERL_BLOCKSIG_ADD(set, sig);
1240 PL_psig_pend[sig] = 0;
1241 PERL_BLOCKSIG_BLOCK(set);
1242 (*PL_sighandlerp)(sig);
1243 PERL_BLOCKSIG_UNBLOCK(set);
1249 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1254 /* Need to be careful with SvREFCNT_dec(), because that can have side
1255 * effects (due to closures). We must make sure that the new disposition
1256 * is in place before it is called.
1260 #ifdef HAS_SIGPROCMASK
1267 if (strEQ(s,"__DIE__"))
1269 else if (strEQ(s,"__WARN__"))
1272 Perl_croak(aTHX_ "No such hook: %s", s);
1280 i = whichsig(s); /* ...no, a brick */
1282 if (ckWARN(WARN_SIGNAL))
1283 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
1286 #ifdef HAS_SIGPROCMASK
1287 /* Avoid having the signal arrive at a bad time, if possible. */
1290 sigprocmask(SIG_BLOCK, &set, &save);
1292 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1293 SAVEFREESV(save_sv);
1294 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1297 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1298 if (!sig_handlers_initted) Perl_csighandler_init();
1300 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1301 sig_ignoring[i] = 0;
1303 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1304 sig_defaulting[i] = 0;
1306 SvREFCNT_dec(PL_psig_name[i]);
1307 to_dec = PL_psig_ptr[i];
1308 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1309 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1310 PL_psig_name[i] = newSVpvn(s, len);
1311 SvREADONLY_on(PL_psig_name[i]);
1313 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1315 (void)rsignal(i, PL_csighandlerp);
1316 #ifdef HAS_SIGPROCMASK
1321 *svp = SvREFCNT_inc(sv);
1323 SvREFCNT_dec(to_dec);
1326 s = SvPV_force(sv,len);
1327 if (strEQ(s,"IGNORE")) {
1329 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1330 sig_ignoring[i] = 1;
1331 (void)rsignal(i, PL_csighandlerp);
1333 (void)rsignal(i, SIG_IGN);
1337 else if (strEQ(s,"DEFAULT") || !*s) {
1339 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1341 sig_defaulting[i] = 1;
1342 (void)rsignal(i, PL_csighandlerp);
1345 (void)rsignal(i, SIG_DFL);
1350 * We should warn if HINT_STRICT_REFS, but without
1351 * access to a known hint bit in a known OP, we can't
1352 * tell whether HINT_STRICT_REFS is in force or not.
1354 if (!strchr(s,':') && !strchr(s,'\''))
1355 sv_insert(sv, 0, 0, "main::", 6);
1357 (void)rsignal(i, PL_csighandlerp);
1359 *svp = SvREFCNT_inc(sv);
1361 #ifdef HAS_SIGPROCMASK
1366 SvREFCNT_dec(to_dec);
1369 #endif /* !PERL_MICRO */
1372 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1374 PL_sub_generation++;
1379 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1381 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1382 PL_amagic_generation++;
1388 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1390 HV *hv = (HV*)LvTARG(sv);
1394 (void) hv_iterinit(hv);
1395 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1398 while (hv_iternext(hv))
1403 sv_setiv(sv, (IV)i);
1408 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1411 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1416 /* caller is responsible for stack switching/cleanup */
1418 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1424 PUSHs(SvTIED_obj(sv, mg));
1427 if (mg->mg_len >= 0)
1428 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1429 else if (mg->mg_len == HEf_SVKEY)
1430 PUSHs((SV*)mg->mg_ptr);
1432 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1433 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1441 return call_method(meth, flags);
1445 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1451 PUSHSTACKi(PERLSI_MAGIC);
1453 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1454 sv_setsv(sv, *PL_stack_sp--);
1464 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1467 mg->mg_flags |= MGf_GSKIP;
1468 magic_methpack(sv,mg,"FETCH");
1473 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1477 PUSHSTACKi(PERLSI_MAGIC);
1478 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1485 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1487 return magic_methpack(sv,mg,"DELETE");
1492 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1499 PUSHSTACKi(PERLSI_MAGIC);
1500 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1501 sv = *PL_stack_sp--;
1502 retval = (U32) SvIV(sv)-1;
1511 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1516 PUSHSTACKi(PERLSI_MAGIC);
1518 XPUSHs(SvTIED_obj(sv, mg));
1520 call_method("CLEAR", G_SCALAR|G_DISCARD);
1527 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1530 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1534 PUSHSTACKi(PERLSI_MAGIC);
1537 PUSHs(SvTIED_obj(sv, mg));
1542 if (call_method(meth, G_SCALAR))
1543 sv_setsv(key, *PL_stack_sp--);
1552 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1554 return magic_methpack(sv,mg,"EXISTS");
1558 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1568 svp = av_fetch(GvAV(gv),
1569 atoi(MgPV(mg,n_a)), FALSE);
1570 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp)))) {
1571 /* set or clear breakpoint in the relevant control op */
1573 o->op_flags |= OPf_SPECIAL;
1575 o->op_flags &= ~OPf_SPECIAL;
1581 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1583 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1588 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1590 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1595 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1597 SV* lsv = LvTARG(sv);
1599 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1600 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1601 if (mg && mg->mg_len >= 0) {
1604 sv_pos_b2u(lsv, &i);
1605 sv_setiv(sv, i + PL_curcop->cop_arybase);
1614 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1616 SV* lsv = LvTARG(sv);
1623 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1624 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1628 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1629 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1631 else if (!SvOK(sv)) {
1635 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1637 pos = SvIV(sv) - PL_curcop->cop_arybase;
1640 ulen = sv_len_utf8(lsv);
1650 else if (pos > (SSize_t)len)
1655 sv_pos_u2b(lsv, &p, 0);
1660 mg->mg_flags &= ~MGf_MINMATCH;
1666 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1668 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1670 gv_efullname3(sv,((GV*)sv), "*");
1674 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1679 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1688 if (*s == '*' && s[1])
1690 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1695 GvGP(sv) = gp_ref(GvGP(gv));
1700 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1703 SV *lsv = LvTARG(sv);
1704 char *tmps = SvPV(lsv,len);
1705 I32 offs = LvTARGOFF(sv);
1706 I32 rem = LvTARGLEN(sv);
1709 sv_pos_u2b(lsv, &offs, &rem);
1710 if (offs > (I32)len)
1712 if (rem + offs > (I32)len)
1714 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1721 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1724 char *tmps = SvPV(sv, len);
1725 SV *lsv = LvTARG(sv);
1726 I32 lvoff = LvTARGOFF(sv);
1727 I32 lvlen = LvTARGLEN(sv);
1730 sv_utf8_upgrade(lsv);
1731 sv_pos_u2b(lsv, &lvoff, &lvlen);
1732 sv_insert(lsv, lvoff, lvlen, tmps, len);
1735 else if (lsv && SvUTF8(lsv)) {
1736 sv_pos_u2b(lsv, &lvoff, &lvlen);
1737 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
1738 sv_insert(lsv, lvoff, lvlen, tmps, len);
1742 sv_insert(lsv, lvoff, lvlen, tmps, len);
1748 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1750 TAINT_IF((mg->mg_len & 1) ||
1751 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1756 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1758 if (PL_localizing) {
1759 if (PL_localizing == 1)
1764 else if (PL_tainted)
1772 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1774 SV *lsv = LvTARG(sv);
1781 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1786 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1788 do_vecset(sv); /* XXX slurp this routine */
1793 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1796 if (LvTARGLEN(sv)) {
1798 SV *ahv = LvTARG(sv);
1799 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1804 AV* av = (AV*)LvTARG(sv);
1805 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1806 targ = AvARRAY(av)[LvTARGOFF(sv)];
1808 if (targ && targ != &PL_sv_undef) {
1809 /* somebody else defined it for us */
1810 SvREFCNT_dec(LvTARG(sv));
1811 LvTARG(sv) = SvREFCNT_inc(targ);
1813 SvREFCNT_dec(mg->mg_obj);
1814 mg->mg_obj = Nullsv;
1815 mg->mg_flags &= ~MGf_REFCOUNTED;
1820 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1825 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1830 sv_setsv(LvTARG(sv), sv);
1831 SvSETMAGIC(LvTARG(sv));
1837 Perl_vivify_defelem(pTHX_ SV *sv)
1842 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
1845 SV *ahv = LvTARG(sv);
1847 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1850 if (!value || value == &PL_sv_undef)
1851 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1854 AV* av = (AV*)LvTARG(sv);
1855 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1856 LvTARG(sv) = Nullsv; /* array can't be extended */
1858 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1859 if (!svp || (value = *svp) == &PL_sv_undef)
1860 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1863 (void)SvREFCNT_inc(value);
1864 SvREFCNT_dec(LvTARG(sv));
1867 SvREFCNT_dec(mg->mg_obj);
1868 mg->mg_obj = Nullsv;
1869 mg->mg_flags &= ~MGf_REFCOUNTED;
1873 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1875 AV *av = (AV*)mg->mg_obj;
1876 SV **svp = AvARRAY(av);
1877 I32 i = AvFILLp(av);
1879 if (svp[i] && svp[i] != &PL_sv_undef) {
1880 if (!SvWEAKREF(svp[i]))
1881 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1882 /* XXX Should we check that it hasn't changed? */
1884 (void)SvOK_off(svp[i]);
1885 SvWEAKREF_off(svp[i]);
1886 svp[i] = &PL_sv_undef;
1890 SvREFCNT_dec(av); /* remove extra count added by sv_add_backref() */
1895 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1903 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1905 sv_unmagic(sv, PERL_MAGIC_bm);
1911 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1913 sv_unmagic(sv, PERL_MAGIC_fm);
1919 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1921 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1923 if (uf && uf->uf_set)
1924 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
1929 Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
1931 sv_unmagic(sv, PERL_MAGIC_qr);
1936 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1938 regexp *re = (regexp *)mg->mg_obj;
1943 #ifdef USE_LOCALE_COLLATE
1945 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1948 * RenE<eacute> Descartes said "I think not."
1949 * and vanished with a faint plop.
1952 Safefree(mg->mg_ptr);
1958 #endif /* USE_LOCALE_COLLATE */
1960 /* Just clear the UTF-8 cache data. */
1962 Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
1964 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
1966 mg->mg_len = -1; /* The mg_len holds the len cache. */
1971 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1976 switch (*mg->mg_ptr) {
1977 case '\001': /* ^A */
1978 sv_setsv(PL_bodytarget, sv);
1980 case '\003': /* ^C */
1981 PL_minus_c = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1984 case '\004': /* ^D */
1987 PL_debug = get_debug_opts(&s) | DEBUG_TOP_FLAG;
1988 DEBUG_x(dump_all());
1990 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
1993 case '\005': /* ^E */
1994 if (*(mg->mg_ptr+1) == '\0') {
1995 #ifdef MACOS_TRADITIONAL
1996 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1999 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2002 SetLastError( SvIV(sv) );
2005 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2007 /* will anyone ever use this? */
2008 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
2014 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
2016 SvREFCNT_dec(PL_encoding);
2017 if (SvOK(sv) || SvGMAGICAL(sv)) {
2018 PL_encoding = newSVsv(sv);
2021 PL_encoding = Nullsv;
2025 case '\006': /* ^F */
2026 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2028 case '\010': /* ^H */
2029 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2031 case '\011': /* ^I */ /* NOT \t in EBCDIC */
2033 Safefree(PL_inplace);
2035 PL_inplace = savepv(SvPV(sv,len));
2037 PL_inplace = Nullch;
2039 case '\017': /* ^O */
2040 if (*(mg->mg_ptr+1) == '\0') {
2042 Safefree(PL_osname);
2044 PL_osname = savepv(SvPV(sv,len));
2048 else if (strEQ(mg->mg_ptr, "\017PEN")) {
2049 if (!PL_compiling.cop_io)
2050 PL_compiling.cop_io = newSVsv(sv);
2052 sv_setsv(PL_compiling.cop_io,sv);
2055 case '\020': /* ^P */
2056 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2057 if (PL_perldb && !PL_DBsingle)
2060 case '\024': /* ^T */
2062 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
2064 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2067 case '\027': /* ^W & $^WARNING_BITS */
2068 if (*(mg->mg_ptr+1) == '\0') {
2069 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2070 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2071 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
2072 | (i ? G_WARN_ON : G_WARN_OFF) ;
2075 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
2076 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2077 if (!SvPOK(sv) && PL_localizing) {
2078 sv_setpvn(sv, WARN_NONEstring, WARNsize);
2079 PL_compiling.cop_warnings = pWARN_NONE;
2084 int accumulate = 0 ;
2085 int any_fatals = 0 ;
2086 char * ptr = (char*)SvPV(sv, len) ;
2087 for (i = 0 ; i < len ; ++i) {
2088 accumulate |= ptr[i] ;
2089 any_fatals |= (ptr[i] & 0xAA) ;
2092 PL_compiling.cop_warnings = pWARN_NONE;
2093 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
2094 PL_compiling.cop_warnings = pWARN_ALL;
2095 PL_dowarn |= G_WARN_ONCE ;
2098 if (specialWARN(PL_compiling.cop_warnings))
2099 PL_compiling.cop_warnings = newSVsv(sv) ;
2101 sv_setsv(PL_compiling.cop_warnings, sv);
2102 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2103 PL_dowarn |= G_WARN_ONCE ;
2111 if (PL_localizing) {
2112 if (PL_localizing == 1)
2113 SAVESPTR(PL_last_in_gv);
2115 else if (SvOK(sv) && GvIO(PL_last_in_gv))
2116 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
2119 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2120 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
2121 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
2124 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2125 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
2126 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
2129 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2132 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2133 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2134 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
2137 IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2141 IO *io = GvIOp(PL_defoutgv);
2144 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
2145 IoFLAGS(io) &= ~IOf_FLUSH;
2147 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2148 PerlIO *ofp = IoOFP(io);
2150 (void)PerlIO_flush(ofp);
2151 IoFLAGS(io) |= IOf_FLUSH;
2157 SvREFCNT_dec(PL_rs);
2158 PL_rs = newSVsv(sv);
2162 SvREFCNT_dec(PL_ors_sv);
2163 if (SvOK(sv) || SvGMAGICAL(sv)) {
2164 PL_ors_sv = newSVsv(sv);
2172 SvREFCNT_dec(PL_ofs_sv);
2173 if (SvOK(sv) || SvGMAGICAL(sv)) {
2174 PL_ofs_sv = newSVsv(sv);
2183 PL_ofmt = savepv(SvPV(sv,len));
2186 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2189 #ifdef COMPLEX_STATUS
2190 if (PL_localizing == 2) {
2191 PL_statusvalue = LvTARGOFF(sv);
2192 PL_statusvalue_vms = LvTARGLEN(sv);
2196 #ifdef VMSISH_STATUS
2198 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
2201 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
2206 # define PERL_VMS_BANG vaxc$errno
2208 # define PERL_VMS_BANG 0
2210 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
2211 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
2215 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2216 if (PL_delaymagic) {
2217 PL_delaymagic |= DM_RUID;
2218 break; /* don't do magic till later */
2221 (void)setruid((Uid_t)PL_uid);
2224 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
2226 #ifdef HAS_SETRESUID
2227 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
2229 if (PL_uid == PL_euid) /* special case $< = $> */
2230 (void)PerlProc_setuid(PL_uid);
2232 PL_uid = PerlProc_getuid();
2233 Perl_croak(aTHX_ "setruid() not implemented");
2238 PL_uid = PerlProc_getuid();
2239 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2242 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2243 if (PL_delaymagic) {
2244 PL_delaymagic |= DM_EUID;
2245 break; /* don't do magic till later */
2248 (void)seteuid((Uid_t)PL_euid);
2251 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
2253 #ifdef HAS_SETRESUID
2254 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
2256 if (PL_euid == PL_uid) /* special case $> = $< */
2257 PerlProc_setuid(PL_euid);
2259 PL_euid = PerlProc_geteuid();
2260 Perl_croak(aTHX_ "seteuid() not implemented");
2265 PL_euid = PerlProc_geteuid();
2266 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2269 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2270 if (PL_delaymagic) {
2271 PL_delaymagic |= DM_RGID;
2272 break; /* don't do magic till later */
2275 (void)setrgid((Gid_t)PL_gid);
2278 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
2280 #ifdef HAS_SETRESGID
2281 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2283 if (PL_gid == PL_egid) /* special case $( = $) */
2284 (void)PerlProc_setgid(PL_gid);
2286 PL_gid = PerlProc_getgid();
2287 Perl_croak(aTHX_ "setrgid() not implemented");
2292 PL_gid = PerlProc_getgid();
2293 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2296 #ifdef HAS_SETGROUPS
2298 char *p = SvPV(sv, len);
2299 Groups_t gary[NGROUPS];
2304 for (i = 0; i < NGROUPS; ++i) {
2305 while (*p && !isSPACE(*p))
2314 (void)setgroups(i, gary);
2316 #else /* HAS_SETGROUPS */
2317 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2318 #endif /* HAS_SETGROUPS */
2319 if (PL_delaymagic) {
2320 PL_delaymagic |= DM_EGID;
2321 break; /* don't do magic till later */
2324 (void)setegid((Gid_t)PL_egid);
2327 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2329 #ifdef HAS_SETRESGID
2330 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2332 if (PL_egid == PL_gid) /* special case $) = $( */
2333 (void)PerlProc_setgid(PL_egid);
2335 PL_egid = PerlProc_getegid();
2336 Perl_croak(aTHX_ "setegid() not implemented");
2341 PL_egid = PerlProc_getegid();
2342 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2345 PL_chopset = SvPV_force(sv,len);
2347 #ifndef MACOS_TRADITIONAL
2349 LOCK_DOLLARZERO_MUTEX;
2350 #ifdef HAS_SETPROCTITLE
2351 /* The BSDs don't show the argv[] in ps(1) output, they
2352 * show a string from the process struct and provide
2353 * the setproctitle() routine to manipulate that. */
2356 # if __FreeBSD_version > 410001
2357 /* The leading "-" removes the "perl: " prefix,
2358 * but not the "(perl) suffix from the ps(1)
2359 * output, because that's what ps(1) shows if the
2360 * argv[] is modified. */
2361 setproctitle("-%s", s);
2362 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2363 /* This doesn't really work if you assume that
2364 * $0 = 'foobar'; will wipe out 'perl' from the $0
2365 * because in ps(1) output the result will be like
2366 * sprintf("perl: %s (perl)", s)
2367 * I guess this is a security feature:
2368 * one (a user process) cannot get rid of the original name.
2370 setproctitle("%s", s);
2374 #if defined(__hpux) && defined(PSTAT_SETCMD)
2379 pstat(PSTAT_SETCMD, un, len, 0, 0);
2382 /* PL_origalen is set in perl_parse(). */
2383 s = SvPV_force(sv,len);
2384 if (len >= (STRLEN)PL_origalen) {
2385 /* Longer than original, will be truncated. */
2386 Copy(s, PL_origargv[0], PL_origalen, char);
2387 PL_origargv[0][PL_origalen - 1] = 0;
2390 /* Shorter than original, will be padded. */
2391 Copy(s, PL_origargv[0], len, char);
2392 PL_origargv[0][len] = 0;
2393 memset(PL_origargv[0] + len + 1,
2394 /* Is the space counterintuitive? Yes.
2395 * (You were expecting \0?)
2396 * Does it work? Seems to. (In Linux 2.4.20 at least.)
2399 PL_origalen - len - 1);
2400 for (i = 1; i < PL_origargc; i++)
2403 UNLOCK_DOLLARZERO_MUTEX;
2411 Perl_whichsig(pTHX_ char *sig)
2413 register char **sigv;
2415 for (sigv = PL_sig_name; *sigv; sigv++)
2416 if (strEQ(sig,*sigv))
2417 return PL_sig_num[sigv - PL_sig_name];
2419 if (strEQ(sig,"CHLD"))
2423 if (strEQ(sig,"CLD"))
2429 #if !defined(PERL_IMPLICIT_CONTEXT)
2434 Perl_sighandler(int sig)
2436 #ifdef PERL_GET_SIG_CONTEXT
2437 dTHXa(PERL_GET_SIG_CONTEXT);
2444 SV *sv = Nullsv, *tSv = PL_Sv;
2450 if (PL_savestack_ix + 15 <= PL_savestack_max)
2452 if (PL_markstack_ptr < PL_markstack_max - 2)
2454 if (PL_retstack_ix < PL_retstack_max - 2)
2456 if (PL_scopestack_ix < PL_scopestack_max - 3)
2459 if (!PL_psig_ptr[sig]) {
2460 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
2465 /* Max number of items pushed there is 3*n or 4. We cannot fix
2466 infinity, so we fix 4 (in fact 5): */
2468 PL_savestack_ix += 5; /* Protect save in progress. */
2469 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2472 PL_markstack_ptr++; /* Protect mark. */
2475 PL_retstack[PL_retstack_ix] = NULL;
2478 PL_scopestack_ix += 1;
2479 /* sv_2cv is too complicated, try a simpler variant first: */
2480 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2481 || SvTYPE(cv) != SVt_PVCV)
2482 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2484 if (!cv || !CvROOT(cv)) {
2485 if (ckWARN(WARN_SIGNAL))
2486 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
2487 PL_sig_name[sig], (gv ? GvENAME(gv)
2494 if(PL_psig_name[sig]) {
2495 sv = SvREFCNT_inc(PL_psig_name[sig]);
2497 #if !defined(PERL_IMPLICIT_CONTEXT)
2501 sv = sv_newmortal();
2502 sv_setpv(sv,PL_sig_name[sig]);
2505 PUSHSTACKi(PERLSI_SIGNAL);
2510 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2513 if (SvTRUE(ERRSV)) {
2515 #ifdef HAS_SIGPROCMASK
2516 /* Handler "died", for example to get out of a restart-able read().
2517 * Before we re-do that on its behalf re-enable the signal which was
2518 * blocked by the system when we entered.
2522 sigaddset(&set,sig);
2523 sigprocmask(SIG_UNBLOCK, &set, NULL);
2525 /* Not clear if this will work */
2526 (void)rsignal(sig, SIG_IGN);
2527 (void)rsignal(sig, PL_csighandlerp);
2529 #endif /* !PERL_MICRO */
2530 Perl_die(aTHX_ Nullformat);
2534 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2540 PL_scopestack_ix -= 1;
2543 PL_op = myop; /* Apparently not needed... */
2545 PL_Sv = tSv; /* Restore global temporaries. */
2552 restore_magic(pTHX_ void *p)
2554 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2555 SV* sv = mgs->mgs_sv;
2560 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2563 SvFLAGS(sv) |= mgs->mgs_flags;
2567 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2570 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2572 /* If we're still on top of the stack, pop us off. (That condition
2573 * will be satisfied if restore_magic was called explicitly, but *not*
2574 * if it's being called via leave_scope.)
2575 * The reason for doing this is that otherwise, things like sv_2cv()
2576 * may leave alloc gunk on the savestack, and some code
2577 * (e.g. sighandler) doesn't expect that...
2579 if (PL_savestack_ix == mgs->mgs_ss_ix)
2581 I32 popval = SSPOPINT;
2582 assert(popval == SAVEt_DESTRUCTOR_X);
2583 PL_savestack_ix -= 2;
2585 assert(popval == SAVEt_ALLOC);
2587 PL_savestack_ix -= popval;
2593 unwind_handler_stack(pTHX_ void *p)
2595 U32 flags = *(U32*)p;
2598 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2599 /* cxstack_ix-- Not needed, die already unwound it. */
2600 #if !defined(PERL_IMPLICIT_CONTEXT)
2602 SvREFCNT_dec(sig_sv);