3 * Copyright (c) 1991-1997, 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."
18 /* XXX If this causes problems, set i_unistd=undef in the hint file. */
23 #if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
30 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
34 static void UnwindHandler(void *pPerl, void *ptr)
36 ((CPerlObj*)pPerl)->unwind_handler_stack(ptr);
39 static void RestoreMagic(void *pPerl, void *ptr)
41 ((CPerlObj*)pPerl)->restore_magic(ptr);
43 #define UNWINDHANDLER UnwindHandler
44 #define RESTOREMAGIC RestoreMagic
45 #define VTBL this->*vtbl
52 typedef struct magic_state MGS;
54 static void restore_magic _((void *p));
55 #define UNWINDHANDLER unwind_handler_stack
56 #define RESTOREMAGIC restore_magic
62 save_magic(MGS *mgs, SV *sv)
64 assert(SvMAGICAL(sv));
67 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
68 SAVEDESTRUCTOR(RESTOREMAGIC, mgs);
72 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
76 restore_magic(void *p)
81 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
84 SvFLAGS(sv) |= mgs->mgs_flags;
88 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
96 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
97 MGVTBL* vtbl = mg->mg_virtual;
99 if ((vtbl->svt_get != NULL) && !(mg->mg_flags & MGf_GSKIP))
103 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || (vtbl->svt_clear != NULL))
118 save_magic(&mgs, sv);
121 while ((mg = *mgp) != 0) {
122 MGVTBL* vtbl = mg->mg_virtual;
123 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && (vtbl->svt_get != NULL)) {
124 (VTBL->svt_get)(sv, mg);
125 /* Ignore this magic if it's been deleted */
126 if ((mg == (mgp_valid ? *mgp : SvMAGIC(sv))) &&
127 (mg->mg_flags & MGf_GSKIP))
130 /* Advance to next magic (complicated by possible deletion) */
131 if (mg == (mgp_valid ? *mgp : SvMAGIC(sv))) {
132 mgp = &mg->mg_moremagic;
136 mgp = &SvMAGIC(sv); /* Re-establish pointer after sv_upgrade */
151 save_magic(&mgs, sv);
153 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
154 MGVTBL* vtbl = mg->mg_virtual;
155 nextmg = mg->mg_moremagic; /* it may delete itself */
156 if (mg->mg_flags & MGf_GSKIP) {
157 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
160 if (vtbl && (vtbl->svt_set != NULL))
161 (VTBL->svt_set)(sv, mg);
175 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
176 MGVTBL* vtbl = mg->mg_virtual;
177 if (vtbl && (vtbl->svt_len != NULL)) {
181 save_magic(&mgs, sv);
182 /* omit MGf_GSKIP -- not changed here */
183 len = (VTBL->svt_len)(sv, mg);
189 junk = SvPV(sv, len);
200 save_magic(&mgs, sv);
202 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
203 MGVTBL* vtbl = mg->mg_virtual;
204 /* omit GSKIP -- never set here */
206 if (vtbl && (vtbl->svt_clear != NULL))
207 (VTBL->svt_clear)(sv, mg);
215 mg_find(SV *sv, int type)
218 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
219 if (mg->mg_type == type)
226 mg_copy(SV *sv, SV *nsv, char *key, I32 klen)
230 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
231 if (isUPPER(mg->mg_type)) {
232 sv_magic(nsv, mg->mg_obj, toLOWER(mg->mg_type), key, klen);
244 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
245 MGVTBL* vtbl = mg->mg_virtual;
246 moremagic = mg->mg_moremagic;
247 if (vtbl && (vtbl->svt_free != NULL))
248 (VTBL->svt_free)(sv, mg);
249 if (mg->mg_ptr && mg->mg_type != 'g')
250 if (mg->mg_length >= 0)
251 Safefree(mg->mg_ptr);
252 else if (mg->mg_length == HEf_SVKEY)
253 SvREFCNT_dec((SV*)mg->mg_ptr);
254 if (mg->mg_flags & MGf_REFCOUNTED)
255 SvREFCNT_dec(mg->mg_obj);
262 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
267 magic_len(SV *sv, MAGIC *mg)
276 switch (*mg->mg_ptr) {
277 case '1': case '2': case '3': case '4':
278 case '5': case '6': case '7': case '8': case '9': case '&':
279 if (curpm && (rx = curpm->op_pmregexp)) {
280 paren = atoi(mg->mg_ptr);
282 if (paren <= rx->nparens &&
283 (s = rx->startp[paren]) &&
284 (t = rx->endp[paren]))
293 if (curpm && (rx = curpm->op_pmregexp)) {
294 paren = rx->lastparen;
300 if (curpm && (rx = curpm->op_pmregexp)) {
301 if ((s = rx->subbeg) && rx->startp[0]) {
302 i = rx->startp[0] - s;
309 if (curpm && (rx = curpm->op_pmregexp)) {
310 if (rx->subend && (s = rx->endp[0])) {
318 return (STRLEN)ofslen;
320 return (STRLEN)orslen;
323 if (!SvPOK(sv) && SvNIOK(sv))
331 magic_get(SV *sv, MAGIC *mg)
340 switch (*mg->mg_ptr) {
341 case '\001': /* ^A */
342 sv_setsv(sv, bodytarget);
344 case '\004': /* ^D */
345 sv_setiv(sv, (IV)(debug & 32767));
347 case '\005': /* ^E */
350 # include <descrip.h>
351 # include <starlet.h>
353 $DESCRIPTOR(msgdsc,msg);
354 sv_setnv(sv,(double) vaxc$errno);
355 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
356 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
362 if (!(_emx_env & 0x200)) { /* Under DOS */
363 sv_setnv(sv, (double)errno);
364 sv_setpv(sv, errno ? Strerror(errno) : "");
366 if (errno != errno_isOS2)
367 Perl_rc = _syserrno();
368 sv_setnv(sv, (double)Perl_rc);
369 sv_setpv(sv, os2error(Perl_rc));
374 DWORD dwErr = GetLastError();
375 sv_setnv(sv, (double)dwErr);
381 PerlProc_GetSysMsg(sMsg, dwLen, dwErr);
382 sv_setpvn(sv, sMsg, dwLen);
383 PerlProc_FreeBuf(sMsg);
385 win32_str_os_error(sv, dwErr);
393 sv_setnv(sv, (double)errno);
394 sv_setpv(sv, errno ? Strerror(errno) : "");
398 SvNOK_on(sv); /* what a wonderful hack! */
400 case '\006': /* ^F */
401 sv_setiv(sv, (IV)maxsysfd);
403 case '\010': /* ^H */
404 sv_setiv(sv, (IV)hints);
408 sv_setpv(sv, inplace);
410 sv_setsv(sv, &sv_undef);
412 case '\017': /* ^O */
413 sv_setpv(sv, osname);
415 case '\020': /* ^P */
416 sv_setiv(sv, (IV)perldb);
418 case '\023': /* ^S */
421 if (lex_state != LEX_NOTPARSING)
429 case '\024': /* ^T */
431 sv_setnv(sv, basetime);
433 sv_setiv(sv, (IV)basetime);
436 case '\027': /* ^W */
437 sv_setiv(sv, (IV)dowarn);
439 case '1': case '2': case '3': case '4':
440 case '5': case '6': case '7': case '8': case '9': case '&':
441 if (curpm && (rx = curpm->op_pmregexp)) {
443 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
444 * XXX Does the new way break anything?
446 paren = atoi(mg->mg_ptr);
448 if (paren <= rx->nparens &&
449 (s = rx->startp[paren]) &&
450 (t = rx->endp[paren]))
457 was_tainted = tainted;
462 tainted = was_tainted || RX_MATCH_TAINTED(rx);
467 sv_setsv(sv,&sv_undef);
470 if (curpm && (rx = curpm->op_pmregexp)) {
471 paren = rx->lastparen;
475 sv_setsv(sv,&sv_undef);
478 if (curpm && (rx = curpm->op_pmregexp)) {
479 if ((s = rx->subbeg) && rx->startp[0]) {
480 i = rx->startp[0] - s;
484 sv_setsv(sv,&sv_undef);
487 if (curpm && (rx = curpm->op_pmregexp)) {
488 if (rx->subend && (s = rx->endp[0])) {
493 sv_setsv(sv,&sv_undef);
497 if (GvIO(last_in_gv)) {
498 sv_setiv(sv, (IV)IoLINES(GvIO(last_in_gv)));
505 sv_setiv(sv, (IV)STATUS_CURRENT);
506 #ifdef COMPLEX_STATUS
507 LvTARGOFF(sv) = statusvalue;
508 LvTARGLEN(sv) = statusvalue_vms;
513 s = IoTOP_NAME(GvIOp(defoutgv));
517 sv_setpv(sv,GvENAME(defoutgv));
522 s = IoFMT_NAME(GvIOp(defoutgv));
524 s = GvENAME(defoutgv);
529 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(defoutgv)));
532 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(defoutgv)));
535 sv_setiv(sv, (IV)IoPAGE(GvIOp(defoutgv)));
543 WITH_THR(sv_setiv(sv, (IV)curcop->cop_arybase));
546 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(defoutgv)) & IOf_FLUSH) != 0 );
549 sv_setpvn(sv,ofs,ofslen);
552 sv_setpvn(sv,ors,orslen);
559 sv_setnv(sv, (double)((errno == EVMSERR) ? vaxc$errno : errno));
560 sv_setpv(sv, errno ? Strerror(errno) : "");
563 int saveerrno = errno;
564 sv_setnv(sv, (double)errno);
566 if (errno == errno_isOS2) sv_setpv(sv, os2error(Perl_rc));
569 sv_setpv(sv, errno ? Strerror(errno) : "");
573 SvNOK_on(sv); /* what a wonderful hack! */
576 sv_setiv(sv, (IV)uid);
579 sv_setiv(sv, (IV)euid);
582 sv_setiv(sv, (IV)gid);
583 sv_setpvf(sv, "%Vd", (IV)gid);
586 sv_setiv(sv, (IV)egid);
587 sv_setpvf(sv, "%Vd", (IV)egid);
591 Groups_t gary[NGROUPS];
592 i = getgroups(NGROUPS,gary);
594 sv_catpvf(sv, " %Vd", (IV)gary[i]);
597 SvIOK_on(sv); /* what a wonderful hack! */
605 sv_setsv(sv, thr->errsv);
607 #endif /* USE_THREADS */
613 magic_getuvar(SV *sv, MAGIC *mg)
615 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
617 if (uf && uf->uf_val)
618 (*uf->uf_val)(uf->uf_index, sv);
623 magic_setenv(SV *sv, MAGIC *mg)
634 #ifdef DYNAMIC_ENV_FETCH
635 /* We just undefd an environment var. Is a replacement */
636 /* waiting in the wings? */
639 if ((valp = hv_fetch(GvHVn(envgv), ptr, klen, FALSE)))
640 s = SvPV(*valp, len);
644 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
645 /* And you'll never guess what the dog had */
646 /* in its mouth... */
648 MgTAINTEDDIR_off(mg);
650 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
651 char pathbuf[256], eltbuf[256], *cp, *elt = s;
655 do { /* DCL$PATH may be a search list */
656 while (1) { /* as may dev portion of any element */
657 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
658 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
659 cando_by_name(S_IWUSR,0,elt) ) {
664 if ((cp = strchr(elt, ':')) != Nullch)
666 if (my_trnlnm(elt, eltbuf, j++))
672 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
675 if (s && klen == 4 && strEQ(ptr,"PATH")) {
676 char *strend = s + len;
681 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
684 if (i >= sizeof tmpbuf /* too long -- assume the worst */
686 || (Stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
693 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
699 magic_clearenv(SV *sv, MAGIC *mg)
701 my_setenv(MgPV(mg,na),Nullch);
706 magic_set_all_env(SV *sv, MAGIC *mg)
709 die("Can't make list assignment to %%ENV on this system");
714 magic_clear_all_env(sv,mg);
715 hv_iterinit((HV*)sv);
716 while (entry = hv_iternext((HV*)sv)) {
718 my_setenv(hv_iterkey(entry, &keylen),
719 SvPV(hv_iterval((HV*)sv, entry), na));
727 magic_clear_all_env(SV *sv, MAGIC *mg)
730 die("Can't make list assignment to %%ENV on this system");
733 char *envv = GetEnvironmentStrings();
737 char *end = strchr(cur,'=');
738 if (end && end != cur) {
740 my_setenv(cur,Nullch);
742 cur += strlen(end+1)+1;
744 else if ((len = strlen(cur)))
747 FreeEnvironmentStrings(envv);
751 if (environ == origenviron)
752 New(901, environ, 1, char*);
754 for (i = 0; environ[i]; i++)
755 Safefree(environ[i]);
764 magic_getsig(SV *sv, MAGIC *mg)
767 /* Are we fetching a signal entry? */
768 i = whichsig(MgPV(mg,na));
771 sv_setsv(sv,psig_ptr[i]);
773 Sighandler_t sigstate = rsignal_state(i);
775 /* cache state so we don't fetch it again */
776 if(sigstate == SIG_IGN)
777 sv_setpv(sv,"IGNORE");
779 sv_setsv(sv,&sv_undef);
780 psig_ptr[i] = SvREFCNT_inc(sv);
787 magic_clearsig(SV *sv, MAGIC *mg)
790 /* Are we clearing a signal entry? */
791 i = whichsig(MgPV(mg,na));
794 SvREFCNT_dec(psig_ptr[i]);
798 SvREFCNT_dec(psig_name[i]);
806 magic_setsig(SV *sv, MAGIC *mg)
815 if (strEQ(s,"__DIE__"))
817 else if (strEQ(s,"__WARN__"))
819 else if (strEQ(s,"__PARSE__"))
822 croak("No such hook: %s", s);
830 i = whichsig(s); /* ...no, a brick */
832 if (dowarn || strEQ(s,"ALARM"))
833 warn("No such signal: SIG%s", s);
836 SvREFCNT_dec(psig_name[i]);
837 SvREFCNT_dec(psig_ptr[i]);
838 psig_ptr[i] = SvREFCNT_inc(sv);
839 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
840 psig_name[i] = newSVpv(s, strlen(s));
841 SvREADONLY_on(psig_name[i]);
843 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
845 (void)rsignal(i, sighandlerp);
847 *svp = SvREFCNT_inc(sv);
850 s = SvPV_force(sv,na);
851 if (strEQ(s,"IGNORE")) {
853 (void)rsignal(i, SIG_IGN);
857 else if (strEQ(s,"DEFAULT") || !*s) {
859 (void)rsignal(i, SIG_DFL);
865 * We should warn if HINT_STRICT_REFS, but without
866 * access to a known hint bit in a known OP, we can't
867 * tell whether HINT_STRICT_REFS is in force or not.
869 if (!strchr(s,':') && !strchr(s,'\''))
870 sv_setpv(sv, form("main::%s", s));
872 (void)rsignal(i, sighandlerp);
874 *svp = SvREFCNT_inc(sv);
880 magic_setisa(SV *sv, MAGIC *mg)
885 HV *basefields = Nullhv;
889 static char *FIELDS = "FIELDS";
893 if (mg->mg_type == 'i')
894 return 0; /* Ignore lower-case version of the magic */
896 stash = GvSTASH(mg->mg_obj);
897 svp = AvARRAY((AV*)sv);
899 for (fill = AvFILL((AV*)sv); fill >= 0; fill--, svp++) {
900 HV *basestash = gv_stashsv(*svp, FALSE);
904 warn("No such package \"%_\" in @ISA assignment", *svp);
907 gvp = (GV**)hv_fetch(basestash, FIELDS, 6, FALSE);
908 if (gvp && *gvp && GvHV(*gvp)) {
910 croak("Can't multiply inherit %%FIELDS");
911 basefields = GvHV(*gvp);
918 gv = (GV*)*hv_fetch(stash, FIELDS, 6, TRUE);
920 gv_init(gv, stash, FIELDS, 6, TRUE);
923 if (HvKEYS(GvHV(gv)))
924 croak("Inherited %%FIELDS can't override existing %%FIELDS");
926 hv_iterinit(GvHV(gv));
927 while ((he = hv_iternext(basefields)))
928 hv_store(GvHV(gv), HeKEY(he), HeKLEN(he), HeVAL(he), HeHASH(he));
936 magic_setamagic(SV *sv, MAGIC *mg)
938 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
943 #endif /* OVERLOAD */
946 magic_setnkeys(SV *sv, MAGIC *mg)
949 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
950 LvTARG(sv) = Nullsv; /* Don't allow a ref to reassign this. */
956 magic_methpack(SV *sv, MAGIC *mg, char *meth)
966 if (mg->mg_length >= 0)
967 PUSHs(sv_2mortal(newSVpv(mg->mg_ptr, mg->mg_length)));
968 else if (mg->mg_length == HEf_SVKEY)
969 PUSHs((SV*)mg->mg_ptr);
971 else if (mg->mg_type == 'p')
972 PUSHs(sv_2mortal(newSViv(mg->mg_length)));
975 if (perl_call_method(meth, G_SCALAR))
976 sv_setsv(sv, *stack_sp--);
984 magic_getpack(SV *sv, MAGIC *mg)
986 magic_methpack(sv,mg,"FETCH");
988 mg->mg_flags |= MGf_GSKIP;
993 magic_setpack(SV *sv, MAGIC *mg)
1001 if (mg->mg_length >= 0)
1002 PUSHs(sv_2mortal(newSVpv(mg->mg_ptr, mg->mg_length)));
1003 else if (mg->mg_length == HEf_SVKEY)
1004 PUSHs((SV*)mg->mg_ptr);
1006 else if (mg->mg_type == 'p')
1007 PUSHs(sv_2mortal(newSViv(mg->mg_length)));
1011 perl_call_method("STORE", G_SCALAR|G_DISCARD);
1017 magic_clearpack(SV *sv, MAGIC *mg)
1019 return magic_methpack(sv,mg,"DELETE");
1022 int magic_wipepack(SV *sv, MAGIC *mg)
1030 perl_call_method("CLEAR", G_SCALAR|G_DISCARD);
1036 magic_nextpack(SV *sv, MAGIC *mg, SV *key)
1039 char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1050 if (perl_call_method(meth, G_SCALAR))
1051 sv_setsv(key, *stack_sp--);
1059 magic_existspack(SV *sv, MAGIC *mg)
1061 return magic_methpack(sv,mg,"EXISTS");
1065 magic_setdbline(SV *sv, MAGIC *mg)
1075 svp = av_fetch(GvAV(gv),
1076 atoi(MgPV(mg,na)), FALSE);
1077 if (svp && SvIOKp(*svp) && (o = (OP*)SvSTASH(*svp)))
1080 warn("Can't break at that line\n");
1085 magic_getarylen(SV *sv, MAGIC *mg)
1088 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + curcop->cop_arybase);
1093 magic_setarylen(SV *sv, MAGIC *mg)
1096 av_fill((AV*)mg->mg_obj, SvIV(sv) - curcop->cop_arybase);
1101 magic_getpos(SV *sv, MAGIC *mg)
1103 SV* lsv = LvTARG(sv);
1105 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1106 mg = mg_find(lsv, 'g');
1107 if (mg && mg->mg_length >= 0) {
1109 sv_setiv(sv, mg->mg_length + curcop->cop_arybase);
1118 magic_setpos(SV *sv, MAGIC *mg)
1120 SV* lsv = LvTARG(sv);
1126 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1127 mg = mg_find(lsv, 'g');
1131 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
1132 mg = mg_find(lsv, 'g');
1134 else if (!SvOK(sv)) {
1138 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1140 WITH_THR(pos = SvIV(sv) - curcop->cop_arybase);
1148 mg->mg_length = pos;
1149 mg->mg_flags &= ~MGf_MINMATCH;
1155 magic_getglob(SV *sv, MAGIC *mg)
1157 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1159 gv_efullname3(sv,((GV*)sv), "*");
1163 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1168 magic_setglob(SV *sv, MAGIC *mg)
1176 if (*s == '*' && s[1])
1178 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1183 GvGP(sv) = gp_ref(GvGP(gv));
1188 magic_setsubstr(SV *sv, MAGIC *mg)
1191 char *tmps = SvPV(sv,len);
1192 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
1197 magic_gettaint(SV *sv, MAGIC *mg)
1200 TAINT_IF((mg->mg_length & 1) ||
1201 (mg->mg_length & 2) && mg->mg_obj == sv); /* kludge */
1206 magic_settaint(SV *sv, MAGIC *mg)
1210 if (localizing == 1)
1211 mg->mg_length <<= 1;
1213 mg->mg_length >>= 1;
1218 mg->mg_length &= ~1;
1223 magic_setvec(SV *sv, MAGIC *mg)
1225 do_vecset(sv); /* XXX slurp this routine */
1230 magic_getdefelem(SV *sv, MAGIC *mg)
1233 if (LvTARGLEN(sv)) {
1235 HV* hv = (HV*)LvTARG(sv);
1236 HE* he = hv_fetch_ent(hv, mg->mg_obj, FALSE, 0);
1241 AV* av = (AV*)LvTARG(sv);
1242 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1243 targ = AvARRAY(av)[LvTARGOFF(sv)];
1245 if (targ && targ != &sv_undef) {
1246 dTHR; /* just for SvREFCNT_dec */
1247 /* somebody else defined it for us */
1248 SvREFCNT_dec(LvTARG(sv));
1249 LvTARG(sv) = SvREFCNT_inc(targ);
1251 SvREFCNT_dec(mg->mg_obj);
1252 mg->mg_obj = Nullsv;
1253 mg->mg_flags &= ~MGf_REFCOUNTED;
1258 sv_setsv(sv, targ ? targ : &sv_undef);
1263 magic_setdefelem(SV *sv, MAGIC *mg)
1268 sv_setsv(LvTARG(sv), sv);
1269 SvSETMAGIC(LvTARG(sv));
1275 magic_freedefelem(SV *sv, MAGIC *mg)
1277 SvREFCNT_dec(LvTARG(sv));
1282 vivify_defelem(SV *sv)
1284 dTHR; /* just for SvREFCNT_inc and SvREFCNT_dec*/
1288 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, 'y')))
1291 HV* hv = (HV*)LvTARG(sv);
1292 HE* he = hv_fetch_ent(hv, mg->mg_obj, TRUE, 0);
1293 if (!he || (value = HeVAL(he)) == &sv_undef)
1294 croak(no_helem, SvPV(mg->mg_obj, na));
1297 AV* av = (AV*)LvTARG(sv);
1298 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1299 LvTARG(sv) = Nullsv; /* array can't be extended */
1301 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1302 if (!svp || (value = *svp) == &sv_undef)
1303 croak(no_aelem, (I32)LvTARGOFF(sv));
1306 (void)SvREFCNT_inc(value);
1307 SvREFCNT_dec(LvTARG(sv));
1310 SvREFCNT_dec(mg->mg_obj);
1311 mg->mg_obj = Nullsv;
1312 mg->mg_flags &= ~MGf_REFCOUNTED;
1316 magic_setmglob(SV *sv, MAGIC *mg)
1324 magic_setbm(SV *sv, MAGIC *mg)
1326 sv_unmagic(sv, 'B');
1332 magic_setfm(SV *sv, MAGIC *mg)
1334 sv_unmagic(sv, 'f');
1340 magic_setuvar(SV *sv, MAGIC *mg)
1342 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1344 if (uf && uf->uf_set)
1345 (*uf->uf_set)(uf->uf_index, sv);
1350 magic_freeregexp(SV *sv, MAGIC *mg)
1352 regexp *re = (regexp *)mg->mg_obj;
1357 #ifdef USE_LOCALE_COLLATE
1359 magic_setcollxfrm(SV *sv, MAGIC *mg)
1362 * René Descartes said "I think not."
1363 * and vanished with a faint plop.
1366 Safefree(mg->mg_ptr);
1372 #endif /* USE_LOCALE_COLLATE */
1375 magic_set(SV *sv, MAGIC *mg)
1381 switch (*mg->mg_ptr) {
1382 case '\001': /* ^A */
1383 sv_setsv(bodytarget, sv);
1385 case '\004': /* ^D */
1386 debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
1387 DEBUG_x(dump_all());
1389 case '\005': /* ^E */
1391 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1394 SetLastError( SvIV(sv) );
1396 /* will anyone ever use this? */
1397 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1401 case '\006': /* ^F */
1402 maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1404 case '\010': /* ^H */
1405 hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1411 inplace = savepv(SvPV(sv,na));
1415 case '\017': /* ^O */
1419 osname = savepv(SvPV(sv,na));
1423 case '\020': /* ^P */
1424 perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1426 case '\024': /* ^T */
1428 basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1430 basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1433 case '\027': /* ^W */
1434 dowarn = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1438 if (localizing == 1)
1439 save_sptr((SV**)&last_in_gv);
1441 else if (SvOK(sv) && GvIO(last_in_gv))
1442 IoLINES(GvIOp(last_in_gv)) = (long)SvIV(sv);
1445 Safefree(IoTOP_NAME(GvIOp(defoutgv)));
1446 IoTOP_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1447 IoTOP_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1450 Safefree(IoFMT_NAME(GvIOp(defoutgv)));
1451 IoFMT_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1452 IoFMT_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1455 IoPAGE_LEN(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1458 IoLINES_LEFT(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1459 if (IoLINES_LEFT(GvIOp(defoutgv)) < 0L)
1460 IoLINES_LEFT(GvIOp(defoutgv)) = 0L;
1463 IoPAGE(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1467 IO *io = GvIOp(defoutgv);
1468 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1469 IoFLAGS(io) &= ~IOf_FLUSH;
1471 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1472 PerlIO *ofp = IoOFP(io);
1474 (void)PerlIO_flush(ofp);
1475 IoFLAGS(io) |= IOf_FLUSH;
1481 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1482 multiline = (i != 0);
1488 rs = SvREFCNT_inc(nrs);
1493 if (SvOK(sv) || SvGMAGICAL(sv))
1494 ors = savepv(SvPV(sv,orslen));
1503 ofs = savepv(SvPV(sv, ofslen));
1508 ofmt = savepv(SvPV(sv,na));
1511 compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1514 #ifdef COMPLEX_STATUS
1515 if (localizing == 2) {
1516 statusvalue = LvTARGOFF(sv);
1517 statusvalue_vms = LvTARGLEN(sv);
1521 #ifdef VMSISH_STATUS
1523 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1526 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1529 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv),
1530 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
1533 uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1535 delaymagic |= DM_RUID;
1536 break; /* don't do magic till later */
1539 (void)setruid((Uid_t)uid);
1542 (void)setreuid((Uid_t)uid, (Uid_t)-1);
1544 #ifdef HAS_SETRESUID
1545 (void)setresuid((Uid_t)uid, (Uid_t)-1, (Uid_t)-1);
1547 if (uid == euid) /* special case $< = $> */
1548 (void)PerlProc_setuid(uid);
1550 uid = (I32)PerlProc_getuid();
1551 croak("setruid() not implemented");
1556 uid = (I32)PerlProc_getuid();
1557 tainting |= (uid && (euid != uid || egid != gid));
1560 euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1562 delaymagic |= DM_EUID;
1563 break; /* don't do magic till later */
1566 (void)seteuid((Uid_t)euid);
1569 (void)setreuid((Uid_t)-1, (Uid_t)euid);
1571 #ifdef HAS_SETRESUID
1572 (void)setresuid((Uid_t)-1, (Uid_t)euid, (Uid_t)-1);
1574 if (euid == uid) /* special case $> = $< */
1575 PerlProc_setuid(euid);
1577 euid = (I32)PerlProc_geteuid();
1578 croak("seteuid() not implemented");
1583 euid = (I32)PerlProc_geteuid();
1584 tainting |= (uid && (euid != uid || egid != gid));
1587 gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1589 delaymagic |= DM_RGID;
1590 break; /* don't do magic till later */
1593 (void)setrgid((Gid_t)gid);
1596 (void)setregid((Gid_t)gid, (Gid_t)-1);
1598 #ifdef HAS_SETRESGID
1599 (void)setresgid((Gid_t)gid, (Gid_t)-1, (Gid_t) 1);
1601 if (gid == egid) /* special case $( = $) */
1602 (void)PerlProc_setgid(gid);
1604 gid = (I32)PerlProc_getgid();
1605 croak("setrgid() not implemented");
1610 gid = (I32)PerlProc_getgid();
1611 tainting |= (uid && (euid != uid || egid != gid));
1614 #ifdef HAS_SETGROUPS
1616 char *p = SvPV(sv, na);
1617 Groups_t gary[NGROUPS];
1619 SET_NUMERIC_STANDARD();
1622 egid = I_V(atof(p));
1623 for (i = 0; i < NGROUPS; ++i) {
1624 while (*p && !isSPACE(*p))
1630 gary[i] = I_V(atof(p));
1633 (void)setgroups(i, gary);
1635 #else /* HAS_SETGROUPS */
1636 egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1637 #endif /* HAS_SETGROUPS */
1639 delaymagic |= DM_EGID;
1640 break; /* don't do magic till later */
1643 (void)setegid((Gid_t)egid);
1646 (void)setregid((Gid_t)-1, (Gid_t)egid);
1648 #ifdef HAS_SETRESGID
1649 (void)setresgid((Gid_t)-1, (Gid_t)egid, (Gid_t)-1);
1651 if (egid == gid) /* special case $) = $( */
1652 (void)PerlProc_setgid(egid);
1654 egid = (I32)PerlProc_getegid();
1655 croak("setegid() not implemented");
1660 egid = (I32)PerlProc_getegid();
1661 tainting |= (uid && (euid != uid || egid != gid));
1664 chopset = SvPV_force(sv,na);
1670 /* See if all the arguments are contiguous in memory */
1671 for (i = 1; i < origargc; i++) {
1672 if (origargv[i] == s + 1
1674 || origargv[i] == s + 2
1677 s += strlen(++s); /* this one is ok too */
1681 /* can grab env area too? */
1682 if (origenviron && (origenviron[0] == s + 1
1684 || (origenviron[0] == s + 9 && (s += 8))
1687 my_setenv("NoNe SuCh", Nullch);
1688 /* force copy of environment */
1689 for (i = 0; origenviron[i]; i++)
1690 if (origenviron[i] == s + 1)
1695 origalen = s - origargv[0];
1697 s = SvPV_force(sv,len);
1699 if (i >= origalen) {
1701 /* don't allow system to limit $0 seen by script */
1702 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
1703 Copy(s, origargv[0], i, char);
1708 Copy(s, origargv[0], i, char);
1711 while (++i < origalen)
1714 for (i = 1; i < origargc; i++)
1715 origargv[i] = Nullch;
1720 sv_setsv(thr->errsv, sv);
1722 #endif /* USE_THREADS */
1729 magic_mutexfree(SV *sv, MAGIC *mg)
1732 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: magic_mutexfree 0x%lx\n",
1733 (unsigned long)thr, (unsigned long)sv);)
1735 croak("panic: magic_mutexfree");
1736 MUTEX_DESTROY(MgMUTEXP(mg));
1737 COND_DESTROY(MgCONDP(mg));
1741 #endif /* USE_THREADS */
1746 register char **sigv;
1748 for (sigv = sig_name+1; *sigv; sigv++)
1749 if (strEQ(sig,*sigv))
1750 return sig_num[sigv - sig_name];
1752 if (strEQ(sig,"CHLD"))
1756 if (strEQ(sig,"CLD"))
1765 unwind_handler_stack(void *p)
1768 U32 flags = *(U32*)p;
1771 savestack_ix -= 5; /* Unprotect save in progress. */
1772 /* cxstack_ix-- Not needed, die already unwound it. */
1774 SvREFCNT_dec(sig_sv);
1788 I32 o_save_i = savestack_ix, type;
1792 if (savestack_ix + 15 <= savestack_max)
1794 if (cxstack_ix < cxstack_max - 2)
1796 if (markstack_ptr < markstack_max - 2)
1798 if (retstack_ix < retstack_max - 2)
1800 if (scopestack_ix < scopestack_max - 3)
1803 if (flags & 2) { /* POPBLOCK may decrease cxstack too early. */
1804 cxstack_ix++; /* Protect from overwrite. */
1805 cx = &cxstack[cxstack_ix];
1806 type = cx->cx_type; /* Can be during partial write. */
1807 cx->cx_type = CXt_NULL; /* Make it safe for unwind. */
1810 die("Signal SIG%s received, but no signal handler set.\n",
1813 /* Max number of items pushed there is 3*n or 4. We cannot fix
1814 infinity, so we fix 4 (in fact 5): */
1816 savestack_ix += 5; /* Protect save in progress. */
1817 o_save_i = savestack_ix;
1818 SAVEDESTRUCTOR(UNWINDHANDLER, (void*)&flags);
1821 markstack_ptr++; /* Protect mark. */
1824 retstack[retstack_ix] = NULL;
1828 /* sv_2cv is too complicated, try a simpler variant first: */
1829 if (!SvROK(psig_ptr[sig]) || !(cv = (CV*)SvRV(psig_ptr[sig]))
1830 || SvTYPE(cv) != SVt_PVCV)
1831 cv = sv_2cv(psig_ptr[sig],&st,&gv,TRUE);
1833 if (!cv || !CvROOT(cv)) {
1835 warn("SIG%s handler \"%s\" not defined.\n",
1836 sig_name[sig], (gv ? GvENAME(gv)
1843 oldstack = curstack;
1844 if (curstack != signalstack)
1845 AvFILL(signalstack) = 0;
1846 SWITCHSTACK(curstack, signalstack);
1848 if(psig_name[sig]) {
1849 sv = SvREFCNT_inc(psig_name[sig]);
1853 sv = sv_newmortal();
1854 sv_setpv(sv,sig_name[sig]);
1860 perl_call_sv((SV*)cv, G_DISCARD);
1862 SWITCHSTACK(signalstack, oldstack);
1865 savestack_ix -= 8; /* Unprotect save in progress. */
1867 cxstack[cxstack_ix].cx_type = type;
1878 op = myop; /* Apparently not needed... */
1880 Sv = tSv; /* Restore global temporaries. */