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.
37 typedef struct magic_state MGS;
39 static void restore_magic _((void *p));
42 save_magic(MGS *mgs, SV *sv)
44 assert(SvMAGICAL(sv));
47 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
48 SAVEDESTRUCTOR(restore_magic, mgs);
52 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
56 restore_magic(void *p)
61 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
64 SvFLAGS(sv) |= mgs->mgs_flags;
68 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
76 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
77 MGVTBL* vtbl = mg->mg_virtual;
79 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
83 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
101 while ((mg = *mgp) != 0) {
102 MGVTBL* vtbl = mg->mg_virtual;
103 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
104 (*vtbl->svt_get)(sv, mg);
105 /* Ignore this magic if it's been deleted */
106 if ((mg == (mgp_valid ? *mgp : SvMAGIC(sv))) &&
107 (mg->mg_flags & MGf_GSKIP))
110 /* Advance to next magic (complicated by possible deletion) */
111 if (mg == (mgp_valid ? *mgp : SvMAGIC(sv))) {
112 mgp = &mg->mg_moremagic;
116 mgp = &SvMAGIC(sv); /* Re-establish pointer after sv_upgrade */
131 save_magic(&mgs, sv);
133 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
134 MGVTBL* vtbl = mg->mg_virtual;
135 nextmg = mg->mg_moremagic; /* it may delete itself */
136 if (mg->mg_flags & MGf_GSKIP) {
137 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
140 if (vtbl && vtbl->svt_set)
141 (*vtbl->svt_set)(sv, mg);
155 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
156 MGVTBL* vtbl = mg->mg_virtual;
157 if (vtbl && vtbl->svt_len) {
161 save_magic(&mgs, sv);
162 /* omit MGf_GSKIP -- not changed here */
163 len = (*vtbl->svt_len)(sv, mg);
169 junk = SvPV(sv, len);
179 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
180 MGVTBL* vtbl = mg->mg_virtual;
181 if (vtbl && vtbl->svt_len) {
184 /* omit MGf_GSKIP -- not changed here */
185 len = (*vtbl->svt_len)(sv, mg);
193 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
198 croak("Size magic not implemented");
211 save_magic(&mgs, sv);
213 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
214 MGVTBL* vtbl = mg->mg_virtual;
215 /* omit GSKIP -- never set here */
217 if (vtbl && vtbl->svt_clear)
218 (*vtbl->svt_clear)(sv, mg);
226 mg_find(SV *sv, int type)
229 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
230 if (mg->mg_type == type)
237 mg_copy(SV *sv, SV *nsv, char *key, I32 klen)
241 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
242 if (isUPPER(mg->mg_type)) {
243 sv_magic(nsv, mg->mg_obj, toLOWER(mg->mg_type), key, klen);
255 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
256 MGVTBL* vtbl = mg->mg_virtual;
257 moremagic = mg->mg_moremagic;
258 if (vtbl && vtbl->svt_free)
259 (*vtbl->svt_free)(sv, mg);
260 if (mg->mg_ptr && mg->mg_type != 'g')
262 Safefree(mg->mg_ptr);
263 else if (mg->mg_len == HEf_SVKEY)
264 SvREFCNT_dec((SV*)mg->mg_ptr);
265 if (mg->mg_flags & MGf_REFCOUNTED)
266 SvREFCNT_dec(mg->mg_obj);
273 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
278 magic_len(SV *sv, MAGIC *mg)
287 switch (*mg->mg_ptr) {
288 case '1': case '2': case '3': case '4':
289 case '5': case '6': case '7': case '8': case '9': case '&':
290 if (curpm && (rx = curpm->op_pmregexp)) {
291 paren = atoi(mg->mg_ptr);
293 if (paren <= rx->nparens &&
294 (s = rx->startp[paren]) &&
295 (t = rx->endp[paren]))
304 if (curpm && (rx = curpm->op_pmregexp)) {
305 paren = rx->lastparen;
311 if (curpm && (rx = curpm->op_pmregexp)) {
312 if ((s = rx->subbeg) && rx->startp[0]) {
313 i = rx->startp[0] - s;
320 if (curpm && (rx = curpm->op_pmregexp)) {
321 if (rx->subend && (s = rx->endp[0])) {
329 return (STRLEN)ofslen;
331 return (STRLEN)orslen;
334 if (!SvPOK(sv) && SvNIOK(sv))
342 magic_get(SV *sv, MAGIC *mg)
351 switch (*mg->mg_ptr) {
352 case '\001': /* ^A */
353 sv_setsv(sv, bodytarget);
355 case '\004': /* ^D */
356 sv_setiv(sv, (IV)(debug & 32767));
358 case '\005': /* ^E */
361 # include <descrip.h>
362 # include <starlet.h>
364 $DESCRIPTOR(msgdsc,msg);
365 sv_setnv(sv,(double) vaxc$errno);
366 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
367 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
373 if (!(_emx_env & 0x200)) { /* Under DOS */
374 sv_setnv(sv, (double)errno);
375 sv_setpv(sv, errno ? Strerror(errno) : "");
377 if (errno != errno_isOS2)
378 Perl_rc = _syserrno();
379 sv_setnv(sv, (double)Perl_rc);
380 sv_setpv(sv, os2error(Perl_rc));
385 DWORD dwErr = GetLastError();
386 sv_setnv(sv, (double)dwErr);
388 win32_str_os_error(sv, dwErr);
394 sv_setnv(sv, (double)errno);
395 sv_setpv(sv, errno ? Strerror(errno) : "");
399 SvNOK_on(sv); /* what a wonderful hack! */
401 case '\006': /* ^F */
402 sv_setiv(sv, (IV)maxsysfd);
404 case '\010': /* ^H */
405 sv_setiv(sv, (IV)hints);
409 sv_setpv(sv, inplace);
411 sv_setsv(sv, &sv_undef);
413 case '\017': /* ^O */
414 sv_setpv(sv, osname);
416 case '\020': /* ^P */
417 sv_setiv(sv, (IV)perldb);
419 case '\023': /* ^S */
422 if (lex_state != LEX_NOTPARSING)
430 case '\024': /* ^T */
432 sv_setnv(sv, basetime);
434 sv_setiv(sv, (IV)basetime);
437 case '\027': /* ^W */
438 sv_setiv(sv, (IV)dowarn);
440 case '1': case '2': case '3': case '4':
441 case '5': case '6': case '7': case '8': case '9': case '&':
442 if (curpm && (rx = curpm->op_pmregexp)) {
444 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
445 * XXX Does the new way break anything?
447 paren = atoi(mg->mg_ptr);
449 if (paren <= rx->nparens &&
450 (s = rx->startp[paren]) &&
451 (t = rx->endp[paren]))
458 was_tainted = tainted;
463 tainted = was_tainted || RX_MATCH_TAINTED(rx);
468 sv_setsv(sv,&sv_undef);
471 if (curpm && (rx = curpm->op_pmregexp)) {
472 paren = rx->lastparen;
476 sv_setsv(sv,&sv_undef);
479 if (curpm && (rx = curpm->op_pmregexp)) {
480 if ((s = rx->subbeg) && rx->startp[0]) {
481 i = rx->startp[0] - s;
485 sv_setsv(sv,&sv_undef);
488 if (curpm && (rx = curpm->op_pmregexp)) {
489 if (rx->subend && (s = rx->endp[0])) {
494 sv_setsv(sv,&sv_undef);
498 if (GvIO(last_in_gv)) {
499 sv_setiv(sv, (IV)IoLINES(GvIO(last_in_gv)));
506 sv_setiv(sv, (IV)STATUS_CURRENT);
507 #ifdef COMPLEX_STATUS
508 LvTARGOFF(sv) = statusvalue;
509 LvTARGLEN(sv) = statusvalue_vms;
514 s = IoTOP_NAME(GvIOp(defoutgv));
518 sv_setpv(sv,GvENAME(defoutgv));
523 s = IoFMT_NAME(GvIOp(defoutgv));
525 s = GvENAME(defoutgv);
530 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(defoutgv)));
533 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(defoutgv)));
536 sv_setiv(sv, (IV)IoPAGE(GvIOp(defoutgv)));
544 WITH_THR(sv_setiv(sv, (IV)curcop->cop_arybase));
547 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(defoutgv)) & IOf_FLUSH) != 0 );
550 sv_setpvn(sv,ofs,ofslen);
553 sv_setpvn(sv,ors,orslen);
560 sv_setnv(sv, (double)((errno == EVMSERR) ? vaxc$errno : errno));
561 sv_setpv(sv, errno ? Strerror(errno) : "");
564 int saveerrno = errno;
565 sv_setnv(sv, (double)errno);
567 if (errno == errno_isOS2) sv_setpv(sv, os2error(Perl_rc));
570 sv_setpv(sv, errno ? Strerror(errno) : "");
574 SvNOK_on(sv); /* what a wonderful hack! */
577 sv_setiv(sv, (IV)uid);
580 sv_setiv(sv, (IV)euid);
583 sv_setiv(sv, (IV)gid);
584 sv_setpvf(sv, "%Vd", (IV)gid);
587 sv_setiv(sv, (IV)egid);
588 sv_setpvf(sv, "%Vd", (IV)egid);
592 Groups_t gary[NGROUPS];
593 i = getgroups(NGROUPS,gary);
595 sv_catpvf(sv, " %Vd", (IV)gary[i]);
598 SvIOK_on(sv); /* what a wonderful hack! */
606 sv_setsv(sv, thr->errsv);
608 #endif /* USE_THREADS */
614 magic_getuvar(SV *sv, MAGIC *mg)
616 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
618 if (uf && uf->uf_val)
619 (*uf->uf_val)(uf->uf_index, sv);
624 magic_setenv(SV *sv, MAGIC *mg)
635 #ifdef DYNAMIC_ENV_FETCH
636 /* We just undefd an environment var. Is a replacement */
637 /* waiting in the wings? */
640 if ((valp = hv_fetch(GvHVn(envgv), ptr, klen, FALSE)))
641 s = SvPV(*valp, len);
645 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
646 /* And you'll never guess what the dog had */
647 /* in its mouth... */
649 MgTAINTEDDIR_off(mg);
651 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
652 char pathbuf[256], eltbuf[256], *cp, *elt = s;
656 do { /* DCL$PATH may be a search list */
657 while (1) { /* as may dev portion of any element */
658 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
659 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
660 cando_by_name(S_IWUSR,0,elt) ) {
665 if ((cp = strchr(elt, ':')) != Nullch)
667 if (my_trnlnm(elt, eltbuf, j++))
673 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
676 if (s && klen == 4 && strEQ(ptr,"PATH")) {
677 char *strend = s + len;
682 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
685 if (i >= sizeof tmpbuf /* too long -- assume the worst */
687 || (Stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
694 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
700 magic_clearenv(SV *sv, MAGIC *mg)
702 my_setenv(MgPV(mg,na),Nullch);
707 magic_set_all_env(SV *sv, MAGIC *mg)
710 die("Can't make list assignment to %%ENV on this system");
715 magic_clear_all_env(sv,mg);
716 hv_iterinit((HV*)sv);
717 while (entry = hv_iternext((HV*)sv)) {
719 my_setenv(hv_iterkey(entry, &keylen),
720 SvPV(hv_iterval((HV*)sv, entry), na));
728 magic_clear_all_env(SV *sv, MAGIC *mg)
731 die("Can't make list assignment to %%ENV on this system");
734 char *envv = GetEnvironmentStrings();
738 char *end = strchr(cur,'=');
739 if (end && end != cur) {
741 my_setenv(cur,Nullch);
743 cur += strlen(end+1)+1;
745 else if ((len = strlen(cur)))
748 FreeEnvironmentStrings(envv);
752 if (environ == origenviron)
753 New(901, environ, 1, char*);
755 for (i = 0; environ[i]; i++)
756 Safefree(environ[i]);
765 magic_getsig(SV *sv, MAGIC *mg)
768 /* Are we fetching a signal entry? */
769 i = whichsig(MgPV(mg,na));
772 sv_setsv(sv,psig_ptr[i]);
774 Sighandler_t sigstate = rsignal_state(i);
776 /* cache state so we don't fetch it again */
777 if(sigstate == SIG_IGN)
778 sv_setpv(sv,"IGNORE");
780 sv_setsv(sv,&sv_undef);
781 psig_ptr[i] = SvREFCNT_inc(sv);
788 magic_clearsig(SV *sv, MAGIC *mg)
791 /* Are we clearing a signal entry? */
792 i = whichsig(MgPV(mg,na));
795 SvREFCNT_dec(psig_ptr[i]);
799 SvREFCNT_dec(psig_name[i]);
807 magic_setsig(SV *sv, MAGIC *mg)
816 if (strEQ(s,"__DIE__"))
818 else if (strEQ(s,"__WARN__"))
820 else if (strEQ(s,"__PARSE__"))
823 croak("No such hook: %s", s);
831 i = whichsig(s); /* ...no, a brick */
833 if (dowarn || strEQ(s,"ALARM"))
834 warn("No such signal: SIG%s", s);
837 SvREFCNT_dec(psig_name[i]);
838 SvREFCNT_dec(psig_ptr[i]);
839 psig_ptr[i] = SvREFCNT_inc(sv);
840 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
841 psig_name[i] = newSVpv(s, strlen(s));
842 SvREADONLY_on(psig_name[i]);
844 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
846 (void)rsignal(i, sighandlerp);
848 *svp = SvREFCNT_inc(sv);
851 s = SvPV_force(sv,na);
852 if (strEQ(s,"IGNORE")) {
854 (void)rsignal(i, SIG_IGN);
858 else if (strEQ(s,"DEFAULT") || !*s) {
860 (void)rsignal(i, SIG_DFL);
866 * We should warn if HINT_STRICT_REFS, but without
867 * access to a known hint bit in a known OP, we can't
868 * tell whether HINT_STRICT_REFS is in force or not.
870 if (!strchr(s,':') && !strchr(s,'\''))
871 sv_setpv(sv, form("main::%s", s));
873 (void)rsignal(i, sighandlerp);
875 *svp = SvREFCNT_inc(sv);
881 magic_setisa(SV *sv, MAGIC *mg)
886 HV *basefields = Nullhv;
890 static char *FIELDS = "FIELDS";
894 if (mg->mg_type == 'i')
895 return 0; /* Ignore lower-case version of the magic */
897 stash = GvSTASH(mg->mg_obj);
898 svp = AvARRAY((AV*)sv);
900 /* NOTE: No support for tied ISA */
901 for (fill = AvFILLp((AV*)sv); fill >= 0; fill--, svp++) {
902 HV *basestash = gv_stashsv(*svp, FALSE);
906 warn("No such package \"%_\" in @ISA assignment", *svp);
909 gvp = (GV**)hv_fetch(basestash, FIELDS, 6, FALSE);
910 if (gvp && *gvp && GvHV(*gvp)) {
912 croak("Can't multiply inherit %%FIELDS");
913 basefields = GvHV(*gvp);
920 gv = (GV*)*hv_fetch(stash, FIELDS, 6, TRUE);
922 gv_init(gv, stash, FIELDS, 6, TRUE);
925 if (HvKEYS(GvHV(gv)))
926 croak("Inherited %%FIELDS can't override existing %%FIELDS");
928 hv_iterinit(GvHV(gv));
929 while ((he = hv_iternext(basefields)))
930 hv_store(GvHV(gv), HeKEY(he), HeKLEN(he), HeVAL(he), HeHASH(he));
938 magic_setamagic(SV *sv, MAGIC *mg)
940 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
945 #endif /* OVERLOAD */
948 magic_setnkeys(SV *sv, MAGIC *mg)
951 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
952 LvTARG(sv) = Nullsv; /* Don't allow a ref to reassign this. */
958 magic_methcall(MAGIC *mg, char *meth, I32 flags, int n, SV *val)
968 PUSHs(sv_2mortal(newSVpv(mg->mg_ptr, mg->mg_len)));
969 else if (mg->mg_len == HEf_SVKEY)
970 PUSHs((SV*)mg->mg_ptr);
972 else if (mg->mg_type == 'p') {
973 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
981 return perl_call_method(meth, flags);
985 magic_methpack(SV *sv, MAGIC *mg, char *meth)
992 if (magic_methcall(mg, meth, G_SCALAR, 2, NULL)) {
993 sv_setsv(sv, *stack_sp--);
1002 magic_getpack(SV *sv, MAGIC *mg)
1004 magic_methpack(sv,mg,"FETCH");
1006 mg->mg_flags |= MGf_GSKIP;
1011 magic_setpack(SV *sv, MAGIC *mg)
1014 magic_methcall(mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1020 magic_clearpack(SV *sv, MAGIC *mg)
1022 return magic_methpack(sv,mg,"DELETE");
1027 magic_sizepack(SV *sv, MAGIC *mg)
1034 if (magic_methcall(mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1036 retval = (U32) SvIV(sv)-1;
1043 int magic_wipepack(SV *sv, MAGIC *mg)
1051 perl_call_method("CLEAR", G_SCALAR|G_DISCARD);
1057 magic_nextpack(SV *sv, MAGIC *mg, SV *key)
1060 char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1071 if (perl_call_method(meth, G_SCALAR))
1072 sv_setsv(key, *stack_sp--);
1080 magic_existspack(SV *sv, MAGIC *mg)
1082 return magic_methpack(sv,mg,"EXISTS");
1086 magic_setdbline(SV *sv, MAGIC *mg)
1096 svp = av_fetch(GvAV(gv),
1097 atoi(MgPV(mg,na)), FALSE);
1098 if (svp && SvIOKp(*svp) && (o = (OP*)SvSTASH(*svp)))
1101 warn("Can't break at that line\n");
1106 magic_getarylen(SV *sv, MAGIC *mg)
1109 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + curcop->cop_arybase);
1114 magic_setarylen(SV *sv, MAGIC *mg)
1117 av_fill((AV*)mg->mg_obj, SvIV(sv) - curcop->cop_arybase);
1122 magic_getpos(SV *sv, MAGIC *mg)
1124 SV* lsv = LvTARG(sv);
1126 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1127 mg = mg_find(lsv, 'g');
1128 if (mg && mg->mg_len >= 0) {
1130 sv_setiv(sv, mg->mg_len + curcop->cop_arybase);
1139 magic_setpos(SV *sv, MAGIC *mg)
1141 SV* lsv = LvTARG(sv);
1147 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1148 mg = mg_find(lsv, 'g');
1152 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
1153 mg = mg_find(lsv, 'g');
1155 else if (!SvOK(sv)) {
1159 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1161 WITH_THR(pos = SvIV(sv) - curcop->cop_arybase);
1170 mg->mg_flags &= ~MGf_MINMATCH;
1176 magic_getglob(SV *sv, MAGIC *mg)
1178 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1180 gv_efullname3(sv,((GV*)sv), "*");
1184 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1189 magic_setglob(SV *sv, MAGIC *mg)
1197 if (*s == '*' && s[1])
1199 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1204 GvGP(sv) = gp_ref(GvGP(gv));
1209 magic_setsubstr(SV *sv, MAGIC *mg)
1212 char *tmps = SvPV(sv,len);
1213 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
1218 magic_gettaint(SV *sv, MAGIC *mg)
1221 TAINT_IF((mg->mg_len & 1) ||
1222 (mg->mg_len & 2) && mg->mg_obj == sv); /* kludge */
1227 magic_settaint(SV *sv, MAGIC *mg)
1231 if (localizing == 1)
1244 magic_setvec(SV *sv, MAGIC *mg)
1246 do_vecset(sv); /* XXX slurp this routine */
1251 magic_getdefelem(SV *sv, MAGIC *mg)
1254 if (LvTARGLEN(sv)) {
1256 HV* hv = (HV*)LvTARG(sv);
1257 HE* he = hv_fetch_ent(hv, mg->mg_obj, FALSE, 0);
1262 AV* av = (AV*)LvTARG(sv);
1263 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1264 targ = AvARRAY(av)[LvTARGOFF(sv)];
1266 if (targ && targ != &sv_undef) {
1267 dTHR; /* just for SvREFCNT_dec */
1268 /* somebody else defined it for us */
1269 SvREFCNT_dec(LvTARG(sv));
1270 LvTARG(sv) = SvREFCNT_inc(targ);
1272 SvREFCNT_dec(mg->mg_obj);
1273 mg->mg_obj = Nullsv;
1274 mg->mg_flags &= ~MGf_REFCOUNTED;
1279 sv_setsv(sv, targ ? targ : &sv_undef);
1284 magic_setdefelem(SV *sv, MAGIC *mg)
1289 sv_setsv(LvTARG(sv), sv);
1290 SvSETMAGIC(LvTARG(sv));
1296 magic_freedefelem(SV *sv, MAGIC *mg)
1298 SvREFCNT_dec(LvTARG(sv));
1303 vivify_defelem(SV *sv)
1305 dTHR; /* just for SvREFCNT_inc and SvREFCNT_dec*/
1309 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, 'y')))
1312 HV* hv = (HV*)LvTARG(sv);
1313 HE* he = hv_fetch_ent(hv, mg->mg_obj, TRUE, 0);
1314 if (!he || (value = HeVAL(he)) == &sv_undef)
1315 croak(no_helem, SvPV(mg->mg_obj, na));
1318 AV* av = (AV*)LvTARG(sv);
1319 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1320 LvTARG(sv) = Nullsv; /* array can't be extended */
1322 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1323 if (!svp || (value = *svp) == &sv_undef)
1324 croak(no_aelem, (I32)LvTARGOFF(sv));
1327 (void)SvREFCNT_inc(value);
1328 SvREFCNT_dec(LvTARG(sv));
1331 SvREFCNT_dec(mg->mg_obj);
1332 mg->mg_obj = Nullsv;
1333 mg->mg_flags &= ~MGf_REFCOUNTED;
1337 magic_setmglob(SV *sv, MAGIC *mg)
1345 magic_setbm(SV *sv, MAGIC *mg)
1347 sv_unmagic(sv, 'B');
1353 magic_setfm(SV *sv, MAGIC *mg)
1355 sv_unmagic(sv, 'f');
1361 magic_setuvar(SV *sv, MAGIC *mg)
1363 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1365 if (uf && uf->uf_set)
1366 (*uf->uf_set)(uf->uf_index, sv);
1371 magic_freeregexp(SV *sv, MAGIC *mg)
1373 regexp *re = (regexp *)mg->mg_obj;
1378 #ifdef USE_LOCALE_COLLATE
1380 magic_setcollxfrm(SV *sv, MAGIC *mg)
1383 * René Descartes said "I think not."
1384 * and vanished with a faint plop.
1387 Safefree(mg->mg_ptr);
1393 #endif /* USE_LOCALE_COLLATE */
1396 magic_set(SV *sv, MAGIC *mg)
1402 switch (*mg->mg_ptr) {
1403 case '\001': /* ^A */
1404 sv_setsv(bodytarget, sv);
1406 case '\004': /* ^D */
1407 debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
1408 DEBUG_x(dump_all());
1410 case '\005': /* ^E */
1412 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1415 SetLastError( SvIV(sv) );
1417 /* will anyone ever use this? */
1418 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1422 case '\006': /* ^F */
1423 maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1425 case '\010': /* ^H */
1426 hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1432 inplace = savepv(SvPV(sv,na));
1436 case '\017': /* ^O */
1440 osname = savepv(SvPV(sv,na));
1444 case '\020': /* ^P */
1445 perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1447 case '\024': /* ^T */
1449 basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1451 basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1454 case '\027': /* ^W */
1455 dowarn = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1459 if (localizing == 1)
1460 save_sptr((SV**)&last_in_gv);
1462 else if (SvOK(sv) && GvIO(last_in_gv))
1463 IoLINES(GvIOp(last_in_gv)) = (long)SvIV(sv);
1466 Safefree(IoTOP_NAME(GvIOp(defoutgv)));
1467 IoTOP_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1468 IoTOP_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1471 Safefree(IoFMT_NAME(GvIOp(defoutgv)));
1472 IoFMT_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1473 IoFMT_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1476 IoPAGE_LEN(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1479 IoLINES_LEFT(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1480 if (IoLINES_LEFT(GvIOp(defoutgv)) < 0L)
1481 IoLINES_LEFT(GvIOp(defoutgv)) = 0L;
1484 IoPAGE(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1488 IO *io = GvIOp(defoutgv);
1489 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1490 IoFLAGS(io) &= ~IOf_FLUSH;
1492 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1493 PerlIO *ofp = IoOFP(io);
1495 (void)PerlIO_flush(ofp);
1496 IoFLAGS(io) |= IOf_FLUSH;
1502 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1503 multiline = (i != 0);
1509 rs = SvREFCNT_inc(nrs);
1514 if (SvOK(sv) || SvGMAGICAL(sv))
1515 ors = savepv(SvPV(sv,orslen));
1524 ofs = savepv(SvPV(sv, ofslen));
1529 ofmt = savepv(SvPV(sv,na));
1532 compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1535 #ifdef COMPLEX_STATUS
1536 if (localizing == 2) {
1537 statusvalue = LvTARGOFF(sv);
1538 statusvalue_vms = LvTARGLEN(sv);
1542 #ifdef VMSISH_STATUS
1544 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1547 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1550 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv),
1551 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
1554 uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1556 delaymagic |= DM_RUID;
1557 break; /* don't do magic till later */
1560 (void)setruid((Uid_t)uid);
1563 (void)setreuid((Uid_t)uid, (Uid_t)-1);
1565 #ifdef HAS_SETRESUID
1566 (void)setresuid((Uid_t)uid, (Uid_t)-1, (Uid_t)-1);
1568 if (uid == euid) /* special case $< = $> */
1571 uid = (I32)getuid();
1572 croak("setruid() not implemented");
1577 uid = (I32)getuid();
1578 tainting |= (uid && (euid != uid || egid != gid));
1581 euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1583 delaymagic |= DM_EUID;
1584 break; /* don't do magic till later */
1587 (void)seteuid((Uid_t)euid);
1590 (void)setreuid((Uid_t)-1, (Uid_t)euid);
1592 #ifdef HAS_SETRESUID
1593 (void)setresuid((Uid_t)-1, (Uid_t)euid, (Uid_t)-1);
1595 if (euid == uid) /* special case $> = $< */
1598 euid = (I32)geteuid();
1599 croak("seteuid() not implemented");
1604 euid = (I32)geteuid();
1605 tainting |= (uid && (euid != uid || egid != gid));
1608 gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1610 delaymagic |= DM_RGID;
1611 break; /* don't do magic till later */
1614 (void)setrgid((Gid_t)gid);
1617 (void)setregid((Gid_t)gid, (Gid_t)-1);
1619 #ifdef HAS_SETRESGID
1620 (void)setresgid((Gid_t)gid, (Gid_t)-1, (Gid_t) 1);
1622 if (gid == egid) /* special case $( = $) */
1625 gid = (I32)getgid();
1626 croak("setrgid() not implemented");
1631 gid = (I32)getgid();
1632 tainting |= (uid && (euid != uid || egid != gid));
1635 #ifdef HAS_SETGROUPS
1637 char *p = SvPV(sv, na);
1638 Groups_t gary[NGROUPS];
1640 SET_NUMERIC_STANDARD();
1643 egid = I_V(atof(p));
1644 for (i = 0; i < NGROUPS; ++i) {
1645 while (*p && !isSPACE(*p))
1651 gary[i] = I_V(atof(p));
1654 (void)setgroups(i, gary);
1656 #else /* HAS_SETGROUPS */
1657 egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1658 #endif /* HAS_SETGROUPS */
1660 delaymagic |= DM_EGID;
1661 break; /* don't do magic till later */
1664 (void)setegid((Gid_t)egid);
1667 (void)setregid((Gid_t)-1, (Gid_t)egid);
1669 #ifdef HAS_SETRESGID
1670 (void)setresgid((Gid_t)-1, (Gid_t)egid, (Gid_t)-1);
1672 if (egid == gid) /* special case $) = $( */
1675 egid = (I32)getegid();
1676 croak("setegid() not implemented");
1681 egid = (I32)getegid();
1682 tainting |= (uid && (euid != uid || egid != gid));
1685 chopset = SvPV_force(sv,na);
1691 /* See if all the arguments are contiguous in memory */
1692 for (i = 1; i < origargc; i++) {
1693 if (origargv[i] == s + 1
1695 || origargv[i] == s + 2
1698 s += strlen(++s); /* this one is ok too */
1702 /* can grab env area too? */
1703 if (origenviron && (origenviron[0] == s + 1
1705 || (origenviron[0] == s + 9 && (s += 8))
1708 my_setenv("NoNe SuCh", Nullch);
1709 /* force copy of environment */
1710 for (i = 0; origenviron[i]; i++)
1711 if (origenviron[i] == s + 1)
1716 origalen = s - origargv[0];
1718 s = SvPV_force(sv,len);
1720 if (i >= origalen) {
1722 /* don't allow system to limit $0 seen by script */
1723 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
1724 Copy(s, origargv[0], i, char);
1729 Copy(s, origargv[0], i, char);
1732 while (++i < origalen)
1735 for (i = 1; i < origargc; i++)
1736 origargv[i] = Nullch;
1741 sv_setsv(thr->errsv, sv);
1743 #endif /* USE_THREADS */
1750 magic_mutexfree(SV *sv, MAGIC *mg)
1753 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: magic_mutexfree 0x%lx\n",
1754 (unsigned long)thr, (unsigned long)sv);)
1756 croak("panic: magic_mutexfree");
1757 MUTEX_DESTROY(MgMUTEXP(mg));
1758 COND_DESTROY(MgCONDP(mg));
1762 #endif /* USE_THREADS */
1767 register char **sigv;
1769 for (sigv = sig_name+1; *sigv; sigv++)
1770 if (strEQ(sig,*sigv))
1771 return sig_num[sigv - sig_name];
1773 if (strEQ(sig,"CHLD"))
1777 if (strEQ(sig,"CLD"))
1786 unwind_handler_stack(void *p)
1789 U32 flags = *(U32*)p;
1792 savestack_ix -= 5; /* Unprotect save in progress. */
1793 /* cxstack_ix-- Not needed, die already unwound it. */
1795 SvREFCNT_dec(sig_sv);
1809 I32 o_save_i = savestack_ix, type;
1813 if (savestack_ix + 15 <= savestack_max)
1815 if (cxstack_ix < cxstack_max - 2)
1817 if (markstack_ptr < markstack_max - 2)
1819 if (retstack_ix < retstack_max - 2)
1821 if (scopestack_ix < scopestack_max - 3)
1824 if (flags & 2) { /* POPBLOCK may decrease cxstack too early. */
1825 cxstack_ix++; /* Protect from overwrite. */
1826 cx = &cxstack[cxstack_ix];
1827 type = cx->cx_type; /* Can be during partial write. */
1828 cx->cx_type = CXt_NULL; /* Make it safe for unwind. */
1831 die("Signal SIG%s received, but no signal handler set.\n",
1834 /* Max number of items pushed there is 3*n or 4. We cannot fix
1835 infinity, so we fix 4 (in fact 5): */
1837 savestack_ix += 5; /* Protect save in progress. */
1838 o_save_i = savestack_ix;
1839 SAVEDESTRUCTOR(unwind_handler_stack, (void*)&flags);
1842 markstack_ptr++; /* Protect mark. */
1845 retstack[retstack_ix] = NULL;
1849 /* sv_2cv is too complicated, try a simpler variant first: */
1850 if (!SvROK(psig_ptr[sig]) || !(cv = (CV*)SvRV(psig_ptr[sig]))
1851 || SvTYPE(cv) != SVt_PVCV)
1852 cv = sv_2cv(psig_ptr[sig],&st,&gv,TRUE);
1854 if (!cv || !CvROOT(cv)) {
1856 warn("SIG%s handler \"%s\" not defined.\n",
1857 sig_name[sig], (gv ? GvENAME(gv)
1864 oldstack = curstack;
1865 if (curstack != signalstack)
1866 AvFILLp(signalstack) = 0;
1867 SWITCHSTACK(curstack, signalstack);
1869 if(psig_name[sig]) {
1870 sv = SvREFCNT_inc(psig_name[sig]);
1874 sv = sv_newmortal();
1875 sv_setpv(sv,sig_name[sig]);
1881 perl_call_sv((SV*)cv, G_DISCARD);
1883 SWITCHSTACK(signalstack, oldstack);
1886 savestack_ix -= 8; /* Unprotect save in progress. */
1888 cxstack[cxstack_ix].cx_type = type;
1899 op = myop; /* Apparently not needed... */
1901 Sv = tSv; /* Restore global temporaries. */