3 * Copyright (c) 1991-2001, 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."
19 #if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
25 static void restore_magic(pTHXo_ void *p);
26 static void unwind_handler_stack(pTHXo_ void *p);
29 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
37 /* MGS is typedef'ed to struct magic_state in perl.h */
40 S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
43 assert(SvMAGICAL(sv));
45 SAVEDESTRUCTOR_X(restore_magic, (void*)mgs_ix);
47 mgs = SSPTR(mgs_ix, MGS*);
49 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
50 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
54 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
58 =for apidoc mg_magical
60 Turns on the magical status of an SV. See C<sv_magic>.
66 Perl_mg_magical(pTHX_ SV *sv)
69 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
70 MGVTBL* vtbl = mg->mg_virtual;
72 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
76 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
85 Do magic after a value is retrieved from the SV. See C<sv_magic>.
91 Perl_mg_get(pTHX_ SV *sv)
98 mgs_ix = SSNEW(sizeof(MGS));
99 save_magic(mgs_ix, sv);
102 while ((mg = *mgp) != 0) {
103 MGVTBL* vtbl = mg->mg_virtual;
104 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
105 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
106 /* Ignore this magic if it's been deleted */
107 if ((mg == (mgp_valid ? *mgp : SvMAGIC(sv))) &&
108 (mg->mg_flags & MGf_GSKIP))
109 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
111 /* Advance to next magic (complicated by possible deletion) */
112 if (mg == (mgp_valid ? *mgp : SvMAGIC(sv))) {
113 mgp = &mg->mg_moremagic;
117 mgp = &SvMAGIC(sv); /* Re-establish pointer after sv_upgrade */
120 restore_magic(aTHXo_ (void*)mgs_ix);
127 Do magic after a value is assigned to the SV. See C<sv_magic>.
133 Perl_mg_set(pTHX_ SV *sv)
139 mgs_ix = SSNEW(sizeof(MGS));
140 save_magic(mgs_ix, sv);
142 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
143 MGVTBL* vtbl = mg->mg_virtual;
144 nextmg = mg->mg_moremagic; /* it may delete itself */
145 if (mg->mg_flags & MGf_GSKIP) {
146 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
147 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
149 if (vtbl && vtbl->svt_set)
150 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
153 restore_magic(aTHXo_ (void*)mgs_ix);
158 =for apidoc mg_length
160 Report on the SV's length. See C<sv_magic>.
166 Perl_mg_length(pTHX_ SV *sv)
172 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
173 MGVTBL* vtbl = mg->mg_virtual;
174 if (vtbl && vtbl->svt_len) {
177 mgs_ix = SSNEW(sizeof(MGS));
178 save_magic(mgs_ix, sv);
179 /* omit MGf_GSKIP -- not changed here */
180 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
181 restore_magic(aTHXo_ (void*)mgs_ix);
186 junk = SvPV(sv, len);
191 Perl_mg_size(pTHX_ SV *sv)
196 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
197 MGVTBL* vtbl = mg->mg_virtual;
198 if (vtbl && vtbl->svt_len) {
201 mgs_ix = SSNEW(sizeof(MGS));
202 save_magic(mgs_ix, sv);
203 /* omit MGf_GSKIP -- not changed here */
204 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
205 restore_magic(aTHXo_ (void*)mgs_ix);
212 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
217 Perl_croak(aTHX_ "Size magic not implemented");
226 Clear something magical that the SV represents. See C<sv_magic>.
232 Perl_mg_clear(pTHX_ SV *sv)
237 mgs_ix = SSNEW(sizeof(MGS));
238 save_magic(mgs_ix, sv);
240 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
241 MGVTBL* vtbl = mg->mg_virtual;
242 /* omit GSKIP -- never set here */
244 if (vtbl && vtbl->svt_clear)
245 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
248 restore_magic(aTHXo_ (void*)mgs_ix);
255 Finds the magic pointer for type matching the SV. See C<sv_magic>.
261 Perl_mg_find(pTHX_ SV *sv, int type)
266 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
267 if (mg->mg_type == type)
276 Copies the magic from one SV to another. See C<sv_magic>.
282 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
286 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
287 if (isUPPER(mg->mg_type)) {
289 mg->mg_type == 'P' ? SvTIED_obj(sv, mg) :
290 (mg->mg_type == 'D' && mg->mg_obj) ? sv : mg->mg_obj,
291 toLOWER(mg->mg_type), key, klen);
301 Free any magic storage used by the SV. See C<sv_magic>.
307 Perl_mg_free(pTHX_ SV *sv)
311 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
312 MGVTBL* vtbl = mg->mg_virtual;
313 moremagic = mg->mg_moremagic;
314 if (vtbl && vtbl->svt_free)
315 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
316 if (mg->mg_ptr && mg->mg_type != 'g')
318 Safefree(mg->mg_ptr);
319 else if (mg->mg_len == HEf_SVKEY)
320 SvREFCNT_dec((SV*)mg->mg_ptr);
321 if (mg->mg_flags & MGf_REFCOUNTED)
322 SvREFCNT_dec(mg->mg_obj);
329 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
334 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
338 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
339 if (mg->mg_obj) /* @+ */
342 return rx->lastparen;
349 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
357 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
361 if (paren <= rx->nparens &&
362 (s = rx->startp[paren]) != -1 &&
363 (t = rx->endp[paren]) != -1)
365 if (mg->mg_obj) /* @+ */
370 if (i > 0 && DO_UTF8(PL_reg_sv)) {
371 char *b = rx->subbeg;
372 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
381 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
383 Perl_croak(aTHX_ PL_no_modify);
389 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
396 switch (*mg->mg_ptr) {
397 case '1': case '2': case '3': case '4':
398 case '5': case '6': case '7': case '8': case '9': case '&':
399 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
401 paren = atoi(mg->mg_ptr); /* $& is in [0] */
403 if (paren <= rx->nparens &&
404 (s1 = rx->startp[paren]) != -1 &&
405 (t1 = rx->endp[paren]) != -1)
409 if (i > 0 && DO_UTF8(PL_reg_sv)) {
410 char *s = rx->subbeg + s1;
411 char *send = rx->subbeg + t1;
413 i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send);
416 Perl_croak(aTHX_ "panic: magic_len: %d", i);
422 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
423 paren = rx->lastparen;
429 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
430 if (rx->startp[0] != -1) {
441 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
442 if (rx->endp[0] != -1) {
443 i = rx->sublen - rx->endp[0];
454 if (!SvPOK(sv) && SvNIOK(sv)) {
464 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
471 switch (*mg->mg_ptr) {
472 case '\001': /* ^A */
473 sv_setsv(sv, PL_bodytarget);
475 case '\003': /* ^C */
476 sv_setiv(sv, (IV)PL_minus_c);
479 case '\004': /* ^D */
480 sv_setiv(sv, (IV)(PL_debug & 32767));
481 #if defined(YYDEBUG) && defined(DEBUGGING)
482 PL_yydebug = (PL_debug & 1);
485 case '\005': /* ^E */
486 #ifdef MACOS_TRADITIONAL
490 sv_setnv(sv,(double)gMacPerl_OSErr);
491 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
496 # include <descrip.h>
497 # include <starlet.h>
499 $DESCRIPTOR(msgdsc,msg);
500 sv_setnv(sv,(NV) vaxc$errno);
501 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
502 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
508 if (!(_emx_env & 0x200)) { /* Under DOS */
509 sv_setnv(sv, (NV)errno);
510 sv_setpv(sv, errno ? Strerror(errno) : "");
512 if (errno != errno_isOS2) {
513 int tmp = _syserrno();
514 if (tmp) /* 2nd call to _syserrno() makes it 0 */
517 sv_setnv(sv, (NV)Perl_rc);
518 sv_setpv(sv, os2error(Perl_rc));
523 DWORD dwErr = GetLastError();
524 sv_setnv(sv, (NV)dwErr);
527 PerlProc_GetOSError(sv, dwErr);
534 sv_setnv(sv, (NV)errno);
535 sv_setpv(sv, errno ? Strerror(errno) : "");
540 SvNOK_on(sv); /* what a wonderful hack! */
542 case '\006': /* ^F */
543 sv_setiv(sv, (IV)PL_maxsysfd);
545 case '\010': /* ^H */
546 sv_setiv(sv, (IV)PL_hints);
548 case '\011': /* ^I */ /* NOT \t in EBCDIC */
550 sv_setpv(sv, PL_inplace);
552 sv_setsv(sv, &PL_sv_undef);
554 case '\017': /* ^O & ^OPEN */
555 if (*(mg->mg_ptr+1) == '\0')
556 sv_setpv(sv, PL_osname);
557 else if (strEQ(mg->mg_ptr, "\017PEN")) {
558 if (!PL_compiling.cop_io)
559 sv_setsv(sv, &PL_sv_undef);
561 sv_setsv(sv, PL_compiling.cop_io);
565 case '\020': /* ^P */
566 sv_setiv(sv, (IV)PL_perldb);
568 case '\023': /* ^S */
570 if (PL_lex_state != LEX_NOTPARSING)
573 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
576 case '\024': /* ^T */
578 sv_setnv(sv, PL_basetime);
580 sv_setiv(sv, (IV)PL_basetime);
583 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
584 if (*(mg->mg_ptr+1) == '\0')
585 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
586 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
587 if (PL_compiling.cop_warnings == pWARN_NONE ||
588 PL_compiling.cop_warnings == pWARN_STD)
590 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
592 else if (PL_compiling.cop_warnings == pWARN_ALL) {
593 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
596 sv_setsv(sv, PL_compiling.cop_warnings);
600 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
601 sv_setiv(sv, (IV)PL_widesyscalls);
603 case '1': case '2': case '3': case '4':
604 case '5': case '6': case '7': case '8': case '9': case '&':
605 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
609 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
610 * XXX Does the new way break anything?
612 paren = atoi(mg->mg_ptr); /* $& is in [0] */
614 if (paren <= rx->nparens &&
615 (s1 = rx->startp[paren]) != -1 &&
616 (t1 = rx->endp[paren]) != -1)
627 was_tainted = PL_tainted;
631 if (DO_UTF8(PL_reg_sv))
636 PL_tainted = (was_tainted || RX_MATCH_TAINTED(rx));
641 sv_setsv(sv,&PL_sv_undef);
644 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
645 paren = rx->lastparen;
649 sv_setsv(sv,&PL_sv_undef);
652 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
653 if ((s = rx->subbeg) && rx->startp[0] != -1) {
658 sv_setsv(sv,&PL_sv_undef);
661 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
662 if (rx->subbeg && rx->endp[0] != -1) {
663 s = rx->subbeg + rx->endp[0];
664 i = rx->sublen - rx->endp[0];
668 sv_setsv(sv,&PL_sv_undef);
672 if (GvIO(PL_last_in_gv)) {
673 sv_setiv(sv, (IV)IoLINES(GvIO(PL_last_in_gv)));
679 sv_setiv(sv, (IV)STATUS_CURRENT);
680 #ifdef COMPLEX_STATUS
681 LvTARGOFF(sv) = PL_statusvalue;
682 LvTARGLEN(sv) = PL_statusvalue_vms;
687 s = IoTOP_NAME(GvIOp(PL_defoutgv));
691 sv_setpv(sv,GvENAME(PL_defoutgv));
696 s = IoFMT_NAME(GvIOp(PL_defoutgv));
698 s = GvENAME(PL_defoutgv);
703 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
706 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
709 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
717 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
720 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
727 sv_setpv(sv,PL_ofmt);
731 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
732 sv_setpv(sv, errno ? Strerror(errno) : "");
735 int saveerrno = errno;
736 sv_setnv(sv, (NV)errno);
738 if (errno == errno_isOS2 || errno == errno_isOS2_set)
739 sv_setpv(sv, os2error(Perl_rc));
742 sv_setpv(sv, errno ? Strerror(errno) : "");
746 SvNOK_on(sv); /* what a wonderful hack! */
749 sv_setiv(sv, (IV)PL_uid);
752 sv_setiv(sv, (IV)PL_euid);
755 sv_setiv(sv, (IV)PL_gid);
757 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
761 sv_setiv(sv, (IV)PL_egid);
763 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
768 Groups_t gary[NGROUPS];
769 i = getgroups(NGROUPS,gary);
771 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
774 (void)SvIOK_on(sv); /* what a wonderful hack! */
778 #ifndef MACOS_TRADITIONAL
784 sv_setsv(sv, thr->errsv);
786 #endif /* USE_THREADS */
792 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
794 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
796 if (uf && uf->uf_val)
797 (*uf->uf_val)(uf->uf_index, sv);
802 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
813 #ifdef DYNAMIC_ENV_FETCH
814 /* We just undefd an environment var. Is a replacement */
815 /* waiting in the wings? */
818 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
819 s = SvPV(*valp, len);
823 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
824 /* And you'll never guess what the dog had */
825 /* in its mouth... */
827 MgTAINTEDDIR_off(mg);
829 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
830 char pathbuf[256], eltbuf[256], *cp, *elt = s;
834 do { /* DCL$PATH may be a search list */
835 while (1) { /* as may dev portion of any element */
836 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
837 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
838 cando_by_name(S_IWUSR,0,elt) ) {
843 if ((cp = strchr(elt, ':')) != Nullch)
845 if (my_trnlnm(elt, eltbuf, j++))
851 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
854 if (s && klen == 4 && strEQ(ptr,"PATH")) {
855 char *strend = s + len;
860 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
863 if (i >= sizeof tmpbuf /* too long -- assume the worst */
865 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
872 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
878 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
881 my_setenv(MgPV(mg,n_a),Nullch);
886 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
889 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
894 magic_clear_all_env(sv,mg);
895 hv_iterinit((HV*)sv);
896 while ((entry = hv_iternext((HV*)sv))) {
898 my_setenv(hv_iterkey(entry, &keylen),
899 SvPV(hv_iterval((HV*)sv, entry), n_a));
907 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
909 #if defined(VMS) || defined(EPOC)
910 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
912 # ifdef PERL_IMPLICIT_SYS
916 char *envv = GetEnvironmentStrings();
920 char *end = strchr(cur,'=');
921 if (end && end != cur) {
923 my_setenv(cur,Nullch);
925 cur = end + strlen(end+1)+2;
927 else if ((len = strlen(cur)))
930 FreeEnvironmentStrings(envv);
932 # ifndef PERL_USE_SAFE_PUTENV
935 if (environ == PL_origenviron)
936 environ = (char**)safesysmalloc(sizeof(char*));
938 for (i = 0; environ[i]; i++)
939 safesysfree(environ[i]);
940 # endif /* PERL_USE_SAFE_PUTENV */
945 # endif /* PERL_IMPLICIT_SYS */
952 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
956 /* Are we fetching a signal entry? */
957 i = whichsig(MgPV(mg,n_a));
960 sv_setsv(sv,PL_psig_ptr[i]);
962 Sighandler_t sigstate = rsignal_state(i);
964 /* cache state so we don't fetch it again */
965 if(sigstate == SIG_IGN)
966 sv_setpv(sv,"IGNORE");
968 sv_setsv(sv,&PL_sv_undef);
969 PL_psig_ptr[i] = SvREFCNT_inc(sv);
976 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
980 /* Are we clearing a signal entry? */
981 i = whichsig(MgPV(mg,n_a));
984 SvREFCNT_dec(PL_psig_ptr[i]);
987 if(PL_psig_name[i]) {
988 SvREFCNT_dec(PL_psig_name[i]);
996 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1005 if (strEQ(s,"__DIE__"))
1007 else if (strEQ(s,"__WARN__"))
1010 Perl_croak(aTHX_ "No such hook: %s", s);
1018 i = whichsig(s); /* ...no, a brick */
1020 if (ckWARN(WARN_SIGNAL))
1021 Perl_warner(aTHX_ WARN_SIGNAL, "No such signal: SIG%s", s);
1024 SvREFCNT_dec(PL_psig_name[i]);
1025 SvREFCNT_dec(PL_psig_ptr[i]);
1026 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1027 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1028 PL_psig_name[i] = newSVpvn(s, len);
1029 SvREADONLY_on(PL_psig_name[i]);
1031 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1033 (void)rsignal(i, PL_sighandlerp);
1035 *svp = SvREFCNT_inc(sv);
1038 s = SvPV_force(sv,len);
1039 if (strEQ(s,"IGNORE")) {
1041 (void)rsignal(i, SIG_IGN);
1045 else if (strEQ(s,"DEFAULT") || !*s) {
1047 (void)rsignal(i, SIG_DFL);
1053 * We should warn if HINT_STRICT_REFS, but without
1054 * access to a known hint bit in a known OP, we can't
1055 * tell whether HINT_STRICT_REFS is in force or not.
1057 if (!strchr(s,':') && !strchr(s,'\''))
1058 sv_insert(sv, 0, 0, "main::", 6);
1060 (void)rsignal(i, PL_sighandlerp);
1062 *svp = SvREFCNT_inc(sv);
1066 #endif /* !PERL_MICRO */
1069 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1071 PL_sub_generation++;
1076 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1078 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1079 PL_amagic_generation++;
1085 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1087 HV *hv = (HV*)LvTARG(sv);
1092 (void) hv_iterinit(hv);
1093 if (! SvTIED_mg((SV*)hv, 'P'))
1097 while ((entry = hv_iternext(hv))) {
1103 sv_setiv(sv, (IV)i);
1108 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1111 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1116 /* caller is responsible for stack switching/cleanup */
1118 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1124 PUSHs(SvTIED_obj(sv, mg));
1127 if (mg->mg_len >= 0)
1128 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1129 else if (mg->mg_len == HEf_SVKEY)
1130 PUSHs((SV*)mg->mg_ptr);
1132 else if (mg->mg_type == 'p') {
1133 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1141 return call_method(meth, flags);
1145 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1151 PUSHSTACKi(PERLSI_MAGIC);
1153 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1154 sv_setsv(sv, *PL_stack_sp--);
1164 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1166 magic_methpack(sv,mg,"FETCH");
1168 mg->mg_flags |= MGf_GSKIP;
1173 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1177 PUSHSTACKi(PERLSI_MAGIC);
1178 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1185 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1187 return magic_methpack(sv,mg,"DELETE");
1192 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1199 PUSHSTACKi(PERLSI_MAGIC);
1200 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1201 sv = *PL_stack_sp--;
1202 retval = (U32) SvIV(sv)-1;
1211 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1216 PUSHSTACKi(PERLSI_MAGIC);
1218 XPUSHs(SvTIED_obj(sv, mg));
1220 call_method("CLEAR", G_SCALAR|G_DISCARD);
1227 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1230 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1234 PUSHSTACKi(PERLSI_MAGIC);
1237 PUSHs(SvTIED_obj(sv, mg));
1242 if (call_method(meth, G_SCALAR))
1243 sv_setsv(key, *PL_stack_sp--);
1252 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1254 return magic_methpack(sv,mg,"EXISTS");
1258 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1268 svp = av_fetch(GvAV(gv),
1269 atoi(MgPV(mg,n_a)), FALSE);
1270 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
1276 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1278 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1283 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1285 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1290 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1292 SV* lsv = LvTARG(sv);
1294 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1295 mg = mg_find(lsv, 'g');
1296 if (mg && mg->mg_len >= 0) {
1299 sv_pos_b2u(lsv, &i);
1300 sv_setiv(sv, i + PL_curcop->cop_arybase);
1309 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1311 SV* lsv = LvTARG(sv);
1318 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1319 mg = mg_find(lsv, 'g');
1323 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
1324 mg = mg_find(lsv, 'g');
1326 else if (!SvOK(sv)) {
1330 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1332 pos = SvIV(sv) - PL_curcop->cop_arybase;
1335 ulen = sv_len_utf8(lsv);
1350 sv_pos_u2b(lsv, &p, 0);
1355 mg->mg_flags &= ~MGf_MINMATCH;
1361 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1363 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1365 gv_efullname3(sv,((GV*)sv), "*");
1369 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1374 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1383 if (*s == '*' && s[1])
1385 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1390 GvGP(sv) = gp_ref(GvGP(gv));
1395 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1398 SV *lsv = LvTARG(sv);
1399 char *tmps = SvPV(lsv,len);
1400 I32 offs = LvTARGOFF(sv);
1401 I32 rem = LvTARGLEN(sv);
1405 if (rem + offs > len)
1407 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1414 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1417 char *tmps = SvPV(sv,len);
1419 sv_utf8_upgrade(LvTARG(sv));
1420 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
1421 SvUTF8_on(LvTARG(sv));
1424 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
1430 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1432 TAINT_IF((mg->mg_len & 1) ||
1433 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1438 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1440 if (PL_localizing) {
1441 if (PL_localizing == 1)
1446 else if (PL_tainted)
1454 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1456 SV *lsv = LvTARG(sv);
1463 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1468 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1470 do_vecset(sv); /* XXX slurp this routine */
1475 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1478 if (LvTARGLEN(sv)) {
1480 SV *ahv = LvTARG(sv);
1481 if (SvTYPE(ahv) == SVt_PVHV) {
1482 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1487 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1493 AV* av = (AV*)LvTARG(sv);
1494 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1495 targ = AvARRAY(av)[LvTARGOFF(sv)];
1497 if (targ && targ != &PL_sv_undef) {
1498 /* somebody else defined it for us */
1499 SvREFCNT_dec(LvTARG(sv));
1500 LvTARG(sv) = SvREFCNT_inc(targ);
1502 SvREFCNT_dec(mg->mg_obj);
1503 mg->mg_obj = Nullsv;
1504 mg->mg_flags &= ~MGf_REFCOUNTED;
1509 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1514 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1519 sv_setsv(LvTARG(sv), sv);
1520 SvSETMAGIC(LvTARG(sv));
1526 Perl_vivify_defelem(pTHX_ SV *sv)
1531 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, 'y')))
1534 SV *ahv = LvTARG(sv);
1536 if (SvTYPE(ahv) == SVt_PVHV) {
1537 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1542 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
1546 if (!value || value == &PL_sv_undef)
1547 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1550 AV* av = (AV*)LvTARG(sv);
1551 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1552 LvTARG(sv) = Nullsv; /* array can't be extended */
1554 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1555 if (!svp || (value = *svp) == &PL_sv_undef)
1556 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1559 (void)SvREFCNT_inc(value);
1560 SvREFCNT_dec(LvTARG(sv));
1563 SvREFCNT_dec(mg->mg_obj);
1564 mg->mg_obj = Nullsv;
1565 mg->mg_flags &= ~MGf_REFCOUNTED;
1569 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1571 AV *av = (AV*)mg->mg_obj;
1572 SV **svp = AvARRAY(av);
1573 I32 i = AvFILLp(av);
1575 if (svp[i] && svp[i] != &PL_sv_undef) {
1576 if (!SvWEAKREF(svp[i]))
1577 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1578 /* XXX Should we check that it hasn't changed? */
1580 (void)SvOK_off(svp[i]);
1581 SvWEAKREF_off(svp[i]);
1582 svp[i] = &PL_sv_undef;
1590 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1598 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1600 sv_unmagic(sv, 'B');
1606 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1608 sv_unmagic(sv, 'f');
1614 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1616 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1618 if (uf && uf->uf_set)
1619 (*uf->uf_set)(uf->uf_index, sv);
1624 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1626 regexp *re = (regexp *)mg->mg_obj;
1631 #ifdef USE_LOCALE_COLLATE
1633 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1636 * RenE<eacute> Descartes said "I think not."
1637 * and vanished with a faint plop.
1640 Safefree(mg->mg_ptr);
1646 #endif /* USE_LOCALE_COLLATE */
1649 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1654 switch (*mg->mg_ptr) {
1655 case '\001': /* ^A */
1656 sv_setsv(PL_bodytarget, sv);
1658 case '\003': /* ^C */
1659 PL_minus_c = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1662 case '\004': /* ^D */
1663 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
1664 DEBUG_x(dump_all());
1666 case '\005': /* ^E */
1667 #ifdef MACOS_TRADITIONAL
1668 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1671 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1674 SetLastError( SvIV(sv) );
1677 /* will anyone ever use this? */
1678 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1684 case '\006': /* ^F */
1685 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1687 case '\010': /* ^H */
1688 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1690 case '\011': /* ^I */ /* NOT \t in EBCDIC */
1692 Safefree(PL_inplace);
1694 PL_inplace = savepv(SvPV(sv,len));
1696 PL_inplace = Nullch;
1698 case '\017': /* ^O */
1699 if (*(mg->mg_ptr+1) == '\0') {
1701 Safefree(PL_osname);
1703 PL_osname = savepv(SvPV(sv,len));
1707 else if (strEQ(mg->mg_ptr, "\017PEN")) {
1708 if (!PL_compiling.cop_io)
1709 PL_compiling.cop_io = newSVsv(sv);
1711 sv_setsv(PL_compiling.cop_io,sv);
1714 case '\020': /* ^P */
1715 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1716 if (PL_perldb && !PL_DBsingle)
1719 case '\024': /* ^T */
1721 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1723 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1726 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
1727 if (*(mg->mg_ptr+1) == '\0') {
1728 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1729 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1730 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1731 | (i ? G_WARN_ON : G_WARN_OFF) ;
1734 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
1735 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1736 if (!SvPOK(sv) && PL_localizing) {
1737 sv_setpvn(sv, WARN_NONEstring, WARNsize);
1738 PL_compiling.cop_warnings = pWARN_NONE;
1743 int accumulate = 0 ;
1744 int any_fatals = 0 ;
1745 char * ptr = (char*)SvPV(sv, len) ;
1746 for (i = 0 ; i < len ; ++i) {
1747 accumulate |= ptr[i] ;
1748 any_fatals |= (ptr[i] & 0xAA) ;
1751 PL_compiling.cop_warnings = pWARN_NONE;
1752 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
1753 PL_compiling.cop_warnings = pWARN_ALL;
1754 PL_dowarn |= G_WARN_ONCE ;
1757 if (specialWARN(PL_compiling.cop_warnings))
1758 PL_compiling.cop_warnings = newSVsv(sv) ;
1760 sv_setsv(PL_compiling.cop_warnings, sv);
1761 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1762 PL_dowarn |= G_WARN_ONCE ;
1768 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
1769 PL_widesyscalls = SvTRUE(sv);
1772 if (PL_localizing) {
1773 if (PL_localizing == 1)
1774 SAVESPTR(PL_last_in_gv);
1776 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1777 IoLINES(GvIOp(PL_last_in_gv)) = (long)SvIV(sv);
1780 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
1781 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1782 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1785 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
1786 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1787 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1790 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1793 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1794 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1795 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
1798 IoPAGE(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1802 IO *io = GvIOp(PL_defoutgv);
1803 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1804 IoFLAGS(io) &= ~IOf_FLUSH;
1806 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1807 PerlIO *ofp = IoOFP(io);
1809 (void)PerlIO_flush(ofp);
1810 IoFLAGS(io) |= IOf_FLUSH;
1816 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1817 PL_multiline = (i != 0);
1820 SvREFCNT_dec(PL_nrs);
1821 PL_nrs = newSVsv(sv);
1822 SvREFCNT_dec(PL_rs);
1823 PL_rs = SvREFCNT_inc(PL_nrs);
1827 SvREFCNT_dec(PL_ors_sv);
1828 if (SvOK(sv) || SvGMAGICAL(sv)) {
1829 PL_ors_sv = newSVsv(sv);
1837 SvREFCNT_dec(PL_ofs_sv);
1838 if (SvOK(sv) || SvGMAGICAL(sv)) {
1839 PL_ofs_sv = newSVsv(sv);
1848 PL_ofmt = savepv(SvPV(sv,len));
1851 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1854 #ifdef COMPLEX_STATUS
1855 if (PL_localizing == 2) {
1856 PL_statusvalue = LvTARGOFF(sv);
1857 PL_statusvalue_vms = LvTARGLEN(sv);
1861 #ifdef VMSISH_STATUS
1863 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1866 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1869 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
1870 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
1873 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1874 if (PL_delaymagic) {
1875 PL_delaymagic |= DM_RUID;
1876 break; /* don't do magic till later */
1879 (void)setruid((Uid_t)PL_uid);
1882 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
1884 #ifdef HAS_SETRESUID
1885 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
1887 if (PL_uid == PL_euid) /* special case $< = $> */
1888 (void)PerlProc_setuid(PL_uid);
1890 PL_uid = PerlProc_getuid();
1891 Perl_croak(aTHX_ "setruid() not implemented");
1896 PL_uid = PerlProc_getuid();
1897 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1900 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1901 if (PL_delaymagic) {
1902 PL_delaymagic |= DM_EUID;
1903 break; /* don't do magic till later */
1906 (void)seteuid((Uid_t)PL_euid);
1909 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
1911 #ifdef HAS_SETRESUID
1912 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
1914 if (PL_euid == PL_uid) /* special case $> = $< */
1915 PerlProc_setuid(PL_euid);
1917 PL_euid = PerlProc_geteuid();
1918 Perl_croak(aTHX_ "seteuid() not implemented");
1923 PL_euid = PerlProc_geteuid();
1924 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1927 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1928 if (PL_delaymagic) {
1929 PL_delaymagic |= DM_RGID;
1930 break; /* don't do magic till later */
1933 (void)setrgid((Gid_t)PL_gid);
1936 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
1938 #ifdef HAS_SETRESGID
1939 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
1941 if (PL_gid == PL_egid) /* special case $( = $) */
1942 (void)PerlProc_setgid(PL_gid);
1944 PL_gid = PerlProc_getgid();
1945 Perl_croak(aTHX_ "setrgid() not implemented");
1950 PL_gid = PerlProc_getgid();
1951 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1954 #ifdef HAS_SETGROUPS
1956 char *p = SvPV(sv, len);
1957 Groups_t gary[NGROUPS];
1962 for (i = 0; i < NGROUPS; ++i) {
1963 while (*p && !isSPACE(*p))
1972 (void)setgroups(i, gary);
1974 #else /* HAS_SETGROUPS */
1975 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1976 #endif /* HAS_SETGROUPS */
1977 if (PL_delaymagic) {
1978 PL_delaymagic |= DM_EGID;
1979 break; /* don't do magic till later */
1982 (void)setegid((Gid_t)PL_egid);
1985 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
1987 #ifdef HAS_SETRESGID
1988 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
1990 if (PL_egid == PL_gid) /* special case $) = $( */
1991 (void)PerlProc_setgid(PL_egid);
1993 PL_egid = PerlProc_getegid();
1994 Perl_croak(aTHX_ "setegid() not implemented");
1999 PL_egid = PerlProc_getegid();
2000 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2003 PL_chopset = SvPV_force(sv,len);
2005 #ifndef MACOS_TRADITIONAL
2007 #ifdef HAS_SETPROCTITLE
2008 /* The BSDs don't show the argv[] in ps(1) output, they
2009 * show a string from the process struct and provide
2010 * the setproctitle() routine to manipulate that. */
2013 # if __FreeBSD_version >= 410001
2014 /* The leading "-" removes the "perl: " prefix,
2015 * but not the "(perl) suffix from the ps(1)
2016 * output, because that's what ps(1) shows if the
2017 * argv[] is modified. */
2018 setproctitle("-%s", s, len + 1);
2019 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2020 /* This doesn't really work if you assume that
2021 * $0 = 'foobar'; will wipe out 'perl' from the $0
2022 * because in ps(1) output the result will be like
2023 * sprintf("perl: %s (perl)", s)
2024 * I guess this is a security feature:
2025 * one (a user process) cannot get rid of the original name.
2027 setproctitle("%s", s);
2034 /* See if all the arguments are contiguous in memory */
2035 for (i = 1; i < PL_origargc; i++) {
2036 if (PL_origargv[i] == s + 1
2038 || PL_origargv[i] == s + 2
2043 s += strlen(s); /* this one is ok too */
2048 /* can grab env area too? */
2049 if (PL_origenviron && (PL_origenviron[0] == s + 1
2051 || (PL_origenviron[0] == s + 9 && (s += 8))
2054 my_setenv("NoNe SuCh", Nullch);
2055 /* force copy of environment */
2056 for (i = 0; PL_origenviron[i]; i++)
2057 if (PL_origenviron[i] == s + 1) {
2064 PL_origalen = s - PL_origargv[0];
2066 s = SvPV_force(sv,len);
2068 if (i >= PL_origalen) {
2070 /* don't allow system to limit $0 seen by script */
2071 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2072 Copy(s, PL_origargv[0], i, char);
2073 s = PL_origargv[0]+i;
2077 Copy(s, PL_origargv[0], i, char);
2078 s = PL_origargv[0]+i;
2080 while (++i < PL_origalen)
2082 s = PL_origargv[0]+i;
2083 for (i = 1; i < PL_origargc; i++)
2084 PL_origargv[i] = Nullch;
2090 sv_setsv(thr->errsv, sv);
2092 #endif /* USE_THREADS */
2099 Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
2101 DEBUG_S(PerlIO_printf(Perl_debug_log,
2102 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2103 PTR2UV(thr), PTR2UV(sv));)
2105 Perl_croak(aTHX_ "panic: magic_mutexfree");
2106 MUTEX_DESTROY(MgMUTEXP(mg));
2107 COND_DESTROY(MgCONDP(mg));
2110 #endif /* USE_THREADS */
2113 Perl_whichsig(pTHX_ char *sig)
2115 register char **sigv;
2117 for (sigv = PL_sig_name+1; *sigv; sigv++)
2118 if (strEQ(sig,*sigv))
2119 return PL_sig_num[sigv - PL_sig_name];
2121 if (strEQ(sig,"CHLD"))
2125 if (strEQ(sig,"CLD"))
2134 Perl_sighandler(int sig)
2136 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2137 dTHXoa(PL_curinterp); /* fake TLS, because signals don't do TLS */
2144 SV *sv, *tSv = PL_Sv;
2148 I32 o_save_i = PL_savestack_ix;
2151 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2152 PERL_SET_THX(aTHXo); /* fake TLS, see above */
2155 if (PL_savestack_ix + 15 <= PL_savestack_max)
2157 if (PL_markstack_ptr < PL_markstack_max - 2)
2159 if (PL_retstack_ix < PL_retstack_max - 2)
2161 if (PL_scopestack_ix < PL_scopestack_max - 3)
2164 if (!PL_psig_ptr[sig])
2165 Perl_die(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
2168 /* Max number of items pushed there is 3*n or 4. We cannot fix
2169 infinity, so we fix 4 (in fact 5): */
2171 PL_savestack_ix += 5; /* Protect save in progress. */
2172 o_save_i = PL_savestack_ix;
2173 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2176 PL_markstack_ptr++; /* Protect mark. */
2179 PL_retstack[PL_retstack_ix] = NULL;
2182 PL_scopestack_ix += 1;
2183 /* sv_2cv is too complicated, try a simpler variant first: */
2184 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2185 || SvTYPE(cv) != SVt_PVCV)
2186 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2188 if (!cv || !CvROOT(cv)) {
2189 if (ckWARN(WARN_SIGNAL))
2190 Perl_warner(aTHX_ WARN_SIGNAL, "SIG%s handler \"%s\" not defined.\n",
2191 PL_sig_name[sig], (gv ? GvENAME(gv)
2198 if(PL_psig_name[sig]) {
2199 sv = SvREFCNT_inc(PL_psig_name[sig]);
2203 sv = sv_newmortal();
2204 sv_setpv(sv,PL_sig_name[sig]);
2207 PUSHSTACKi(PERLSI_SIGNAL);
2212 call_sv((SV*)cv, G_DISCARD);
2217 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2223 PL_scopestack_ix -= 1;
2226 PL_op = myop; /* Apparently not needed... */
2228 PL_Sv = tSv; /* Restore global temporaries. */
2239 restore_magic(pTHXo_ void *p)
2241 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2242 SV* sv = mgs->mgs_sv;
2247 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2250 SvFLAGS(sv) |= mgs->mgs_flags;
2254 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2257 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2259 /* If we're still on top of the stack, pop us off. (That condition
2260 * will be satisfied if restore_magic was called explicitly, but *not*
2261 * if it's being called via leave_scope.)
2262 * The reason for doing this is that otherwise, things like sv_2cv()
2263 * may leave alloc gunk on the savestack, and some code
2264 * (e.g. sighandler) doesn't expect that...
2266 if (PL_savestack_ix == mgs->mgs_ss_ix)
2268 I32 popval = SSPOPINT;
2269 assert(popval == SAVEt_DESTRUCTOR_X);
2270 PL_savestack_ix -= 2;
2272 assert(popval == SAVEt_ALLOC);
2274 PL_savestack_ix -= popval;
2280 unwind_handler_stack(pTHXo_ void *p)
2282 U32 flags = *(U32*)p;
2285 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2286 /* cxstack_ix-- Not needed, die already unwound it. */
2288 SvREFCNT_dec(sig_sv);