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.
35 #define VTBL this->*vtbl
42 typedef struct magic_state MGS;
44 static void restore_magic _((void *p));
50 save_magic(MGS *mgs, SV *sv)
52 assert(SvMAGICAL(sv));
55 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
56 SAVEDESTRUCTOR(restore_magic, mgs);
60 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
64 restore_magic(void *p)
69 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
72 SvFLAGS(sv) |= mgs->mgs_flags;
76 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
84 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
85 MGVTBL* vtbl = mg->mg_virtual;
87 if ((vtbl->svt_get != NULL) && !(mg->mg_flags & MGf_GSKIP))
91 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || (vtbl->svt_clear != NULL))
106 save_magic(&mgs, sv);
109 while ((mg = *mgp) != 0) {
110 MGVTBL* vtbl = mg->mg_virtual;
111 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && (vtbl->svt_get != NULL)) {
112 (VTBL->svt_get)(sv, mg);
113 /* Ignore this magic if it's been deleted */
114 if ((mg == (mgp_valid ? *mgp : SvMAGIC(sv))) &&
115 (mg->mg_flags & MGf_GSKIP))
118 /* Advance to next magic (complicated by possible deletion) */
119 if (mg == (mgp_valid ? *mgp : SvMAGIC(sv))) {
120 mgp = &mg->mg_moremagic;
124 mgp = &SvMAGIC(sv); /* Re-establish pointer after sv_upgrade */
139 save_magic(&mgs, sv);
141 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
142 MGVTBL* vtbl = mg->mg_virtual;
143 nextmg = mg->mg_moremagic; /* it may delete itself */
144 if (mg->mg_flags & MGf_GSKIP) {
145 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
148 if (vtbl && (vtbl->svt_set != NULL))
149 (VTBL->svt_set)(sv, mg);
163 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
164 MGVTBL* vtbl = mg->mg_virtual;
165 if (vtbl && (vtbl->svt_len != NULL)) {
169 save_magic(&mgs, sv);
170 /* omit MGf_GSKIP -- not changed here */
171 len = (VTBL->svt_len)(sv, mg);
177 junk = SvPV(sv, len);
187 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
188 MGVTBL* vtbl = mg->mg_virtual;
189 if (vtbl && (vtbl->svt_len != NULL)) {
192 /* omit MGf_GSKIP -- not changed here */
193 len = (VTBL->svt_len)(sv, mg);
201 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
206 croak("Size magic not implemented");
219 save_magic(&mgs, sv);
221 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
222 MGVTBL* vtbl = mg->mg_virtual;
223 /* omit GSKIP -- never set here */
225 if (vtbl && (vtbl->svt_clear != NULL))
226 (VTBL->svt_clear)(sv, mg);
234 mg_find(SV *sv, int type)
237 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
238 if (mg->mg_type == type)
245 mg_copy(SV *sv, SV *nsv, char *key, I32 klen)
249 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
250 if (isUPPER(mg->mg_type)) {
251 sv_magic(nsv, mg->mg_obj, toLOWER(mg->mg_type), key, klen);
263 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
264 MGVTBL* vtbl = mg->mg_virtual;
265 moremagic = mg->mg_moremagic;
266 if (vtbl && (vtbl->svt_free != NULL))
267 (VTBL->svt_free)(sv, mg);
268 if (mg->mg_ptr && mg->mg_type != 'g')
270 Safefree(mg->mg_ptr);
271 else if (mg->mg_len == HEf_SVKEY)
272 SvREFCNT_dec((SV*)mg->mg_ptr);
273 if (mg->mg_flags & MGf_REFCOUNTED)
274 SvREFCNT_dec(mg->mg_obj);
281 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
286 magic_len(SV *sv, MAGIC *mg)
295 switch (*mg->mg_ptr) {
296 case '1': case '2': case '3': case '4':
297 case '5': case '6': case '7': case '8': case '9': case '&':
298 if (curpm && (rx = curpm->op_pmregexp)) {
299 paren = atoi(mg->mg_ptr);
301 if (paren <= rx->nparens &&
302 (s = rx->startp[paren]) &&
303 (t = rx->endp[paren]))
312 if (curpm && (rx = curpm->op_pmregexp)) {
313 paren = rx->lastparen;
319 if (curpm && (rx = curpm->op_pmregexp)) {
320 if ((s = rx->subbeg) && rx->startp[0]) {
321 i = rx->startp[0] - s;
328 if (curpm && (rx = curpm->op_pmregexp)) {
329 if (rx->subend && (s = rx->endp[0])) {
337 return (STRLEN)ofslen;
339 return (STRLEN)orslen;
342 if (!SvPOK(sv) && SvNIOK(sv))
350 magic_get(SV *sv, MAGIC *mg)
359 switch (*mg->mg_ptr) {
360 case '\001': /* ^A */
361 sv_setsv(sv, bodytarget);
363 case '\004': /* ^D */
364 sv_setiv(sv, (IV)(debug & 32767));
366 case '\005': /* ^E */
369 # include <descrip.h>
370 # include <starlet.h>
372 $DESCRIPTOR(msgdsc,msg);
373 sv_setnv(sv,(double) vaxc$errno);
374 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
375 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
381 if (!(_emx_env & 0x200)) { /* Under DOS */
382 sv_setnv(sv, (double)errno);
383 sv_setpv(sv, errno ? Strerror(errno) : "");
385 if (errno != errno_isOS2)
386 Perl_rc = _syserrno();
387 sv_setnv(sv, (double)Perl_rc);
388 sv_setpv(sv, os2error(Perl_rc));
393 DWORD dwErr = GetLastError();
394 sv_setnv(sv, (double)dwErr);
400 PerlProc_GetSysMsg(sMsg, dwLen, dwErr);
401 sv_setpvn(sv, sMsg, dwLen);
402 PerlProc_FreeBuf(sMsg);
404 win32_str_os_error(sv, dwErr);
412 sv_setnv(sv, (double)errno);
413 sv_setpv(sv, errno ? Strerror(errno) : "");
417 SvNOK_on(sv); /* what a wonderful hack! */
419 case '\006': /* ^F */
420 sv_setiv(sv, (IV)maxsysfd);
422 case '\010': /* ^H */
423 sv_setiv(sv, (IV)hints);
427 sv_setpv(sv, inplace);
429 sv_setsv(sv, &sv_undef);
431 case '\017': /* ^O */
432 sv_setpv(sv, osname);
434 case '\020': /* ^P */
435 sv_setiv(sv, (IV)perldb);
437 case '\023': /* ^S */
440 if (lex_state != LEX_NOTPARSING)
448 case '\024': /* ^T */
450 sv_setnv(sv, basetime);
452 sv_setiv(sv, (IV)basetime);
455 case '\027': /* ^W */
456 sv_setiv(sv, (IV)dowarn);
458 case '1': case '2': case '3': case '4':
459 case '5': case '6': case '7': case '8': case '9': case '&':
460 if (curpm && (rx = curpm->op_pmregexp)) {
462 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
463 * XXX Does the new way break anything?
465 paren = atoi(mg->mg_ptr);
467 if (paren <= rx->nparens &&
468 (s = rx->startp[paren]) &&
469 (t = rx->endp[paren]))
476 was_tainted = tainted;
481 tainted = (was_tainted || RX_MATCH_TAINTED(rx) ||
482 (curpm->op_pmflags & PMf_TAINTMEM));
487 sv_setsv(sv,&sv_undef);
490 if (curpm && (rx = curpm->op_pmregexp)) {
491 paren = rx->lastparen;
495 sv_setsv(sv,&sv_undef);
498 if (curpm && (rx = curpm->op_pmregexp)) {
499 if ((s = rx->subbeg) && rx->startp[0]) {
500 i = rx->startp[0] - s;
504 sv_setsv(sv,&sv_undef);
507 if (curpm && (rx = curpm->op_pmregexp)) {
508 if (rx->subend && (s = rx->endp[0])) {
513 sv_setsv(sv,&sv_undef);
517 if (GvIO(last_in_gv)) {
518 sv_setiv(sv, (IV)IoLINES(GvIO(last_in_gv)));
525 sv_setiv(sv, (IV)STATUS_CURRENT);
526 #ifdef COMPLEX_STATUS
527 LvTARGOFF(sv) = statusvalue;
528 LvTARGLEN(sv) = statusvalue_vms;
533 s = IoTOP_NAME(GvIOp(defoutgv));
537 sv_setpv(sv,GvENAME(defoutgv));
542 s = IoFMT_NAME(GvIOp(defoutgv));
544 s = GvENAME(defoutgv);
549 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(defoutgv)));
552 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(defoutgv)));
555 sv_setiv(sv, (IV)IoPAGE(GvIOp(defoutgv)));
563 WITH_THR(sv_setiv(sv, (IV)curcop->cop_arybase));
566 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(defoutgv)) & IOf_FLUSH) != 0 );
569 sv_setpvn(sv,ofs,ofslen);
572 sv_setpvn(sv,ors,orslen);
579 sv_setnv(sv, (double)((errno == EVMSERR) ? vaxc$errno : errno));
580 sv_setpv(sv, errno ? Strerror(errno) : "");
583 int saveerrno = errno;
584 sv_setnv(sv, (double)errno);
586 if (errno == errno_isOS2) sv_setpv(sv, os2error(Perl_rc));
589 sv_setpv(sv, errno ? Strerror(errno) : "");
593 SvNOK_on(sv); /* what a wonderful hack! */
596 sv_setiv(sv, (IV)uid);
599 sv_setiv(sv, (IV)euid);
602 sv_setiv(sv, (IV)gid);
603 sv_setpvf(sv, "%Vd", (IV)gid);
606 sv_setiv(sv, (IV)egid);
607 sv_setpvf(sv, "%Vd", (IV)egid);
611 Groups_t gary[NGROUPS];
612 i = getgroups(NGROUPS,gary);
614 sv_catpvf(sv, " %Vd", (IV)gary[i]);
617 SvIOK_on(sv); /* what a wonderful hack! */
625 sv_setsv(sv, thr->errsv);
627 #endif /* USE_THREADS */
633 magic_getuvar(SV *sv, MAGIC *mg)
635 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
637 if (uf && uf->uf_val)
638 (*uf->uf_val)(uf->uf_index, sv);
643 magic_setenv(SV *sv, MAGIC *mg)
654 #ifdef DYNAMIC_ENV_FETCH
655 /* We just undefd an environment var. Is a replacement */
656 /* waiting in the wings? */
659 if ((valp = hv_fetch(GvHVn(envgv), ptr, klen, FALSE)))
660 s = SvPV(*valp, len);
664 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
665 /* And you'll never guess what the dog had */
666 /* in its mouth... */
668 MgTAINTEDDIR_off(mg);
670 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
671 char pathbuf[256], eltbuf[256], *cp, *elt = s;
675 do { /* DCL$PATH may be a search list */
676 while (1) { /* as may dev portion of any element */
677 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
678 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
679 cando_by_name(S_IWUSR,0,elt) ) {
684 if ((cp = strchr(elt, ':')) != Nullch)
686 if (my_trnlnm(elt, eltbuf, j++))
692 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
695 if (s && klen == 4 && strEQ(ptr,"PATH")) {
696 char *strend = s + len;
701 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
704 if (i >= sizeof tmpbuf /* too long -- assume the worst */
706 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
713 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
719 magic_clearenv(SV *sv, MAGIC *mg)
721 my_setenv(MgPV(mg,na),Nullch);
726 magic_set_all_env(SV *sv, MAGIC *mg)
729 die("Can't make list assignment to %%ENV on this system");
734 magic_clear_all_env(sv,mg);
735 hv_iterinit((HV*)sv);
736 while (entry = hv_iternext((HV*)sv)) {
738 my_setenv(hv_iterkey(entry, &keylen),
739 SvPV(hv_iterval((HV*)sv, entry), na));
747 magic_clear_all_env(SV *sv, MAGIC *mg)
750 die("Can't make list assignment to %%ENV on this system");
753 char *envv = GetEnvironmentStrings();
757 char *end = strchr(cur,'=');
758 if (end && end != cur) {
760 my_setenv(cur,Nullch);
762 cur += strlen(end+1)+1;
764 else if ((len = strlen(cur)))
767 FreeEnvironmentStrings(envv);
771 if (environ == origenviron)
772 New(901, environ, 1, char*);
774 for (i = 0; environ[i]; i++)
775 Safefree(environ[i]);
784 magic_getsig(SV *sv, MAGIC *mg)
787 /* Are we fetching a signal entry? */
788 i = whichsig(MgPV(mg,na));
791 sv_setsv(sv,psig_ptr[i]);
793 Sighandler_t sigstate = rsignal_state(i);
795 /* cache state so we don't fetch it again */
796 if(sigstate == SIG_IGN)
797 sv_setpv(sv,"IGNORE");
799 sv_setsv(sv,&sv_undef);
800 psig_ptr[i] = SvREFCNT_inc(sv);
807 magic_clearsig(SV *sv, MAGIC *mg)
810 /* Are we clearing a signal entry? */
811 i = whichsig(MgPV(mg,na));
814 SvREFCNT_dec(psig_ptr[i]);
818 SvREFCNT_dec(psig_name[i]);
826 magic_setsig(SV *sv, MAGIC *mg)
835 if (strEQ(s,"__DIE__"))
837 else if (strEQ(s,"__WARN__"))
839 else if (strEQ(s,"__PARSE__"))
842 croak("No such hook: %s", s);
850 i = whichsig(s); /* ...no, a brick */
852 if (dowarn || strEQ(s,"ALARM"))
853 warn("No such signal: SIG%s", s);
856 SvREFCNT_dec(psig_name[i]);
857 SvREFCNT_dec(psig_ptr[i]);
858 psig_ptr[i] = SvREFCNT_inc(sv);
859 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
860 psig_name[i] = newSVpv(s, strlen(s));
861 SvREADONLY_on(psig_name[i]);
863 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
865 (void)rsignal(i, sighandlerp);
867 *svp = SvREFCNT_inc(sv);
870 s = SvPV_force(sv,na);
871 if (strEQ(s,"IGNORE")) {
873 (void)rsignal(i, SIG_IGN);
877 else if (strEQ(s,"DEFAULT") || !*s) {
879 (void)rsignal(i, SIG_DFL);
885 * We should warn if HINT_STRICT_REFS, but without
886 * access to a known hint bit in a known OP, we can't
887 * tell whether HINT_STRICT_REFS is in force or not.
889 if (!strchr(s,':') && !strchr(s,'\''))
890 sv_setpv(sv, form("main::%s", s));
892 (void)rsignal(i, sighandlerp);
894 *svp = SvREFCNT_inc(sv);
900 magic_setisa(SV *sv, MAGIC *mg)
905 HV *basefields = Nullhv;
909 static char *FIELDS = "FIELDS";
913 if (mg->mg_type == 'i')
914 return 0; /* Ignore lower-case version of the magic */
916 stash = GvSTASH(mg->mg_obj);
917 svp = AvARRAY((AV*)sv);
919 /* NOTE: No support for tied ISA */
920 for (fill = AvFILLp((AV*)sv); fill >= 0; fill--, svp++) {
921 HV *basestash = gv_stashsv(*svp, FALSE);
925 warn("No such package \"%_\" in @ISA assignment", *svp);
928 gvp = (GV**)hv_fetch(basestash, FIELDS, 6, FALSE);
929 if (gvp && *gvp && GvHV(*gvp)) {
931 croak("Can't multiply inherit %%FIELDS");
932 basefields = GvHV(*gvp);
939 gv = (GV*)*hv_fetch(stash, FIELDS, 6, TRUE);
941 gv_init(gv, stash, FIELDS, 6, TRUE);
944 if (HvKEYS(GvHV(gv)))
945 croak("Inherited %%FIELDS can't override existing %%FIELDS");
947 hv_iterinit(GvHV(gv));
948 while ((he = hv_iternext(basefields)))
949 hv_store(GvHV(gv), HeKEY(he), HeKLEN(he), HeVAL(he), HeHASH(he));
957 magic_setamagic(SV *sv, MAGIC *mg)
959 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
964 #endif /* OVERLOAD */
967 magic_getnkeys(SV *sv, MAGIC *mg)
969 HV *hv = (HV*)LvTARG(sv);
974 (void) hv_iterinit(hv);
975 if (!SvRMAGICAL(hv) || !mg_find((SV*)hv,'P'))
979 while (entry = hv_iternext(hv)) {
990 magic_setnkeys(SV *sv, MAGIC *mg)
993 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
998 /* caller is responsible for stack switching/cleanup */
1000 magic_methcall(MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1009 if (mg->mg_len >= 0)
1010 PUSHs(sv_2mortal(newSVpv(mg->mg_ptr, mg->mg_len)));
1011 else if (mg->mg_len == HEf_SVKEY)
1012 PUSHs((SV*)mg->mg_ptr);
1014 else if (mg->mg_type == 'p') {
1015 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1023 return perl_call_method(meth, flags);
1027 magic_methpack(SV *sv, MAGIC *mg, char *meth)
1033 PUSHSTACK(SI_MAGIC);
1035 if (magic_methcall(mg, meth, G_SCALAR, 2, NULL)) {
1036 sv_setsv(sv, *stack_sp--);
1046 magic_getpack(SV *sv, MAGIC *mg)
1048 magic_methpack(sv,mg,"FETCH");
1050 mg->mg_flags |= MGf_GSKIP;
1055 magic_setpack(SV *sv, MAGIC *mg)
1059 PUSHSTACK(SI_MAGIC);
1060 magic_methcall(mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1067 magic_clearpack(SV *sv, MAGIC *mg)
1069 return magic_methpack(sv,mg,"DELETE");
1074 magic_sizepack(SV *sv, MAGIC *mg)
1081 PUSHSTACK(SI_MAGIC);
1082 if (magic_methcall(mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1084 retval = (U32) SvIV(sv)-1;
1092 int magic_wipepack(SV *sv, MAGIC *mg)
1097 PUSHSTACK(SI_MAGIC);
1101 perl_call_method("CLEAR", G_SCALAR|G_DISCARD);
1108 magic_nextpack(SV *sv, MAGIC *mg, SV *key)
1111 char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1115 PUSHSTACK(SI_MAGIC);
1123 if (perl_call_method(meth, G_SCALAR))
1124 sv_setsv(key, *stack_sp--);
1133 magic_existspack(SV *sv, MAGIC *mg)
1135 return magic_methpack(sv,mg,"EXISTS");
1139 magic_setdbline(SV *sv, MAGIC *mg)
1149 svp = av_fetch(GvAV(gv),
1150 atoi(MgPV(mg,na)), FALSE);
1151 if (svp && SvIOKp(*svp) && (o = (OP*)SvSTASH(*svp)))
1154 warn("Can't break at that line\n");
1159 magic_getarylen(SV *sv, MAGIC *mg)
1162 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + curcop->cop_arybase);
1167 magic_setarylen(SV *sv, MAGIC *mg)
1170 av_fill((AV*)mg->mg_obj, SvIV(sv) - curcop->cop_arybase);
1175 magic_getpos(SV *sv, MAGIC *mg)
1177 SV* lsv = LvTARG(sv);
1179 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1180 mg = mg_find(lsv, 'g');
1181 if (mg && mg->mg_len >= 0) {
1183 sv_setiv(sv, mg->mg_len + curcop->cop_arybase);
1192 magic_setpos(SV *sv, MAGIC *mg)
1194 SV* lsv = LvTARG(sv);
1200 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1201 mg = mg_find(lsv, 'g');
1205 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
1206 mg = mg_find(lsv, 'g');
1208 else if (!SvOK(sv)) {
1212 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1214 WITH_THR(pos = SvIV(sv) - curcop->cop_arybase);
1223 mg->mg_flags &= ~MGf_MINMATCH;
1229 magic_getglob(SV *sv, MAGIC *mg)
1231 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1233 gv_efullname3(sv,((GV*)sv), "*");
1237 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1242 magic_setglob(SV *sv, MAGIC *mg)
1250 if (*s == '*' && s[1])
1252 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1257 GvGP(sv) = gp_ref(GvGP(gv));
1262 magic_getsubstr(SV *sv, MAGIC *mg)
1265 SV *lsv = LvTARG(sv);
1266 char *tmps = SvPV(lsv,len);
1267 I32 offs = LvTARGOFF(sv);
1268 I32 rem = LvTARGLEN(sv);
1272 if (rem + offs > len)
1274 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1279 magic_setsubstr(SV *sv, MAGIC *mg)
1282 char *tmps = SvPV(sv,len);
1283 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
1288 magic_gettaint(SV *sv, MAGIC *mg)
1291 TAINT_IF((mg->mg_len & 1) ||
1292 (mg->mg_len & 2) && mg->mg_obj == sv); /* kludge */
1297 magic_settaint(SV *sv, MAGIC *mg)
1301 if (localizing == 1)
1314 magic_getvec(SV *sv, MAGIC *mg)
1316 SV *lsv = LvTARG(sv);
1318 unsigned long retnum;
1328 s = (unsigned char *) SvPV(lsv, lsvlen);
1329 offset = LvTARGOFF(sv);
1330 size = LvTARGLEN(sv);
1331 len = (offset + size + 7) / 8;
1333 /* Copied from pp_vec() */
1341 if (offset >= lsvlen)
1344 retnum = (unsigned long) s[offset] << 8;
1346 else if (size == 32) {
1347 if (offset >= lsvlen)
1349 else if (offset + 1 >= lsvlen)
1350 retnum = (unsigned long) s[offset] << 24;
1351 else if (offset + 2 >= lsvlen)
1352 retnum = ((unsigned long) s[offset] << 24) +
1353 ((unsigned long) s[offset + 1] << 16);
1355 retnum = ((unsigned long) s[offset] << 24) +
1356 ((unsigned long) s[offset + 1] << 16) +
1357 (s[offset + 2] << 8);
1362 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
1367 else if (size == 16)
1368 retnum = ((unsigned long) s[offset] << 8) + s[offset+1];
1369 else if (size == 32)
1370 retnum = ((unsigned long) s[offset] << 24) +
1371 ((unsigned long) s[offset + 1] << 16) +
1372 (s[offset + 2] << 8) + s[offset+3];
1375 sv_setuv(sv, (UV)retnum);
1380 magic_setvec(SV *sv, MAGIC *mg)
1382 do_vecset(sv); /* XXX slurp this routine */
1387 magic_getdefelem(SV *sv, MAGIC *mg)
1390 if (LvTARGLEN(sv)) {
1392 HV* hv = (HV*)LvTARG(sv);
1393 HE* he = hv_fetch_ent(hv, mg->mg_obj, FALSE, 0);
1398 AV* av = (AV*)LvTARG(sv);
1399 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1400 targ = AvARRAY(av)[LvTARGOFF(sv)];
1402 if (targ && targ != &sv_undef) {
1403 dTHR; /* just for SvREFCNT_dec */
1404 /* somebody else defined it for us */
1405 SvREFCNT_dec(LvTARG(sv));
1406 LvTARG(sv) = SvREFCNT_inc(targ);
1408 SvREFCNT_dec(mg->mg_obj);
1409 mg->mg_obj = Nullsv;
1410 mg->mg_flags &= ~MGf_REFCOUNTED;
1415 sv_setsv(sv, targ ? targ : &sv_undef);
1420 magic_setdefelem(SV *sv, MAGIC *mg)
1425 sv_setsv(LvTARG(sv), sv);
1426 SvSETMAGIC(LvTARG(sv));
1432 magic_freedefelem(SV *sv, MAGIC *mg)
1434 SvREFCNT_dec(LvTARG(sv));
1439 vivify_defelem(SV *sv)
1441 dTHR; /* just for SvREFCNT_inc and SvREFCNT_dec*/
1445 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, 'y')))
1448 HV* hv = (HV*)LvTARG(sv);
1449 HE* he = hv_fetch_ent(hv, mg->mg_obj, TRUE, 0);
1450 if (!he || (value = HeVAL(he)) == &sv_undef)
1451 croak(no_helem, SvPV(mg->mg_obj, na));
1454 AV* av = (AV*)LvTARG(sv);
1455 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1456 LvTARG(sv) = Nullsv; /* array can't be extended */
1458 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1459 if (!svp || (value = *svp) == &sv_undef)
1460 croak(no_aelem, (I32)LvTARGOFF(sv));
1463 (void)SvREFCNT_inc(value);
1464 SvREFCNT_dec(LvTARG(sv));
1467 SvREFCNT_dec(mg->mg_obj);
1468 mg->mg_obj = Nullsv;
1469 mg->mg_flags &= ~MGf_REFCOUNTED;
1473 magic_setmglob(SV *sv, MAGIC *mg)
1481 magic_setbm(SV *sv, MAGIC *mg)
1483 sv_unmagic(sv, 'B');
1489 magic_setfm(SV *sv, MAGIC *mg)
1491 sv_unmagic(sv, 'f');
1497 magic_setuvar(SV *sv, MAGIC *mg)
1499 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1501 if (uf && uf->uf_set)
1502 (*uf->uf_set)(uf->uf_index, sv);
1507 magic_freeregexp(SV *sv, MAGIC *mg)
1509 regexp *re = (regexp *)mg->mg_obj;
1515 magic_unchain(SV *sv, MAGIC *mg)
1517 sv_unmagic(sv, mg->mg_type);
1521 #ifdef USE_LOCALE_COLLATE
1523 magic_setcollxfrm(SV *sv, MAGIC *mg)
1526 * René Descartes said "I think not."
1527 * and vanished with a faint plop.
1530 Safefree(mg->mg_ptr);
1536 #endif /* USE_LOCALE_COLLATE */
1539 magic_set(SV *sv, MAGIC *mg)
1545 switch (*mg->mg_ptr) {
1546 case '\001': /* ^A */
1547 sv_setsv(bodytarget, sv);
1549 case '\004': /* ^D */
1550 debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
1551 DEBUG_x(dump_all());
1553 case '\005': /* ^E */
1555 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1558 SetLastError( SvIV(sv) );
1560 /* will anyone ever use this? */
1561 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1565 case '\006': /* ^F */
1566 maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1568 case '\010': /* ^H */
1569 hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1575 inplace = savepv(SvPV(sv,na));
1579 case '\017': /* ^O */
1583 osname = savepv(SvPV(sv,na));
1587 case '\020': /* ^P */
1588 perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1590 case '\024': /* ^T */
1592 basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1594 basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1597 case '\027': /* ^W */
1598 dowarn = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1602 if (localizing == 1)
1603 save_sptr((SV**)&last_in_gv);
1605 else if (SvOK(sv) && GvIO(last_in_gv))
1606 IoLINES(GvIOp(last_in_gv)) = (long)SvIV(sv);
1609 Safefree(IoTOP_NAME(GvIOp(defoutgv)));
1610 IoTOP_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1611 IoTOP_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1614 Safefree(IoFMT_NAME(GvIOp(defoutgv)));
1615 IoFMT_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1616 IoFMT_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1619 IoPAGE_LEN(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1622 IoLINES_LEFT(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1623 if (IoLINES_LEFT(GvIOp(defoutgv)) < 0L)
1624 IoLINES_LEFT(GvIOp(defoutgv)) = 0L;
1627 IoPAGE(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1631 IO *io = GvIOp(defoutgv);
1632 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1633 IoFLAGS(io) &= ~IOf_FLUSH;
1635 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1636 PerlIO *ofp = IoOFP(io);
1638 (void)PerlIO_flush(ofp);
1639 IoFLAGS(io) |= IOf_FLUSH;
1645 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1646 multiline = (i != 0);
1652 rs = SvREFCNT_inc(nrs);
1657 if (SvOK(sv) || SvGMAGICAL(sv))
1658 ors = savepv(SvPV(sv,orslen));
1667 ofs = savepv(SvPV(sv, ofslen));
1672 ofmt = savepv(SvPV(sv,na));
1675 compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1678 #ifdef COMPLEX_STATUS
1679 if (localizing == 2) {
1680 statusvalue = LvTARGOFF(sv);
1681 statusvalue_vms = LvTARGLEN(sv);
1685 #ifdef VMSISH_STATUS
1687 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1690 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1693 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
1694 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
1697 uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1699 delaymagic |= DM_RUID;
1700 break; /* don't do magic till later */
1703 (void)setruid((Uid_t)uid);
1706 (void)setreuid((Uid_t)uid, (Uid_t)-1);
1708 #ifdef HAS_SETRESUID
1709 (void)setresuid((Uid_t)uid, (Uid_t)-1, (Uid_t)-1);
1711 if (uid == euid) /* special case $< = $> */
1712 (void)PerlProc_setuid(uid);
1714 uid = (I32)PerlProc_getuid();
1715 croak("setruid() not implemented");
1720 uid = (I32)PerlProc_getuid();
1721 tainting |= (uid && (euid != uid || egid != gid));
1724 euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1726 delaymagic |= DM_EUID;
1727 break; /* don't do magic till later */
1730 (void)seteuid((Uid_t)euid);
1733 (void)setreuid((Uid_t)-1, (Uid_t)euid);
1735 #ifdef HAS_SETRESUID
1736 (void)setresuid((Uid_t)-1, (Uid_t)euid, (Uid_t)-1);
1738 if (euid == uid) /* special case $> = $< */
1739 PerlProc_setuid(euid);
1741 euid = (I32)PerlProc_geteuid();
1742 croak("seteuid() not implemented");
1747 euid = (I32)PerlProc_geteuid();
1748 tainting |= (uid && (euid != uid || egid != gid));
1751 gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1753 delaymagic |= DM_RGID;
1754 break; /* don't do magic till later */
1757 (void)setrgid((Gid_t)gid);
1760 (void)setregid((Gid_t)gid, (Gid_t)-1);
1762 #ifdef HAS_SETRESGID
1763 (void)setresgid((Gid_t)gid, (Gid_t)-1, (Gid_t) 1);
1765 if (gid == egid) /* special case $( = $) */
1766 (void)PerlProc_setgid(gid);
1768 gid = (I32)PerlProc_getgid();
1769 croak("setrgid() not implemented");
1774 gid = (I32)PerlProc_getgid();
1775 tainting |= (uid && (euid != uid || egid != gid));
1778 #ifdef HAS_SETGROUPS
1780 char *p = SvPV(sv, na);
1781 Groups_t gary[NGROUPS];
1783 SET_NUMERIC_STANDARD();
1786 egid = I_V(atof(p));
1787 for (i = 0; i < NGROUPS; ++i) {
1788 while (*p && !isSPACE(*p))
1794 gary[i] = I_V(atof(p));
1797 (void)setgroups(i, gary);
1799 #else /* HAS_SETGROUPS */
1800 egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1801 #endif /* HAS_SETGROUPS */
1803 delaymagic |= DM_EGID;
1804 break; /* don't do magic till later */
1807 (void)setegid((Gid_t)egid);
1810 (void)setregid((Gid_t)-1, (Gid_t)egid);
1812 #ifdef HAS_SETRESGID
1813 (void)setresgid((Gid_t)-1, (Gid_t)egid, (Gid_t)-1);
1815 if (egid == gid) /* special case $) = $( */
1816 (void)PerlProc_setgid(egid);
1818 egid = (I32)PerlProc_getegid();
1819 croak("setegid() not implemented");
1824 egid = (I32)PerlProc_getegid();
1825 tainting |= (uid && (euid != uid || egid != gid));
1828 chopset = SvPV_force(sv,na);
1834 /* See if all the arguments are contiguous in memory */
1835 for (i = 1; i < origargc; i++) {
1836 if (origargv[i] == s + 1
1838 || origargv[i] == s + 2
1841 s += strlen(++s); /* this one is ok too */
1845 /* can grab env area too? */
1846 if (origenviron && (origenviron[0] == s + 1
1848 || (origenviron[0] == s + 9 && (s += 8))
1851 my_setenv("NoNe SuCh", Nullch);
1852 /* force copy of environment */
1853 for (i = 0; origenviron[i]; i++)
1854 if (origenviron[i] == s + 1)
1859 origalen = s - origargv[0];
1861 s = SvPV_force(sv,len);
1863 if (i >= origalen) {
1865 /* don't allow system to limit $0 seen by script */
1866 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
1867 Copy(s, origargv[0], i, char);
1872 Copy(s, origargv[0], i, char);
1875 while (++i < origalen)
1878 for (i = 1; i < origargc; i++)
1879 origargv[i] = Nullch;
1884 sv_setsv(thr->errsv, sv);
1886 #endif /* USE_THREADS */
1893 magic_mutexfree(SV *sv, MAGIC *mg)
1896 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: magic_mutexfree 0x%lx\n",
1897 (unsigned long)thr, (unsigned long)sv);)
1899 croak("panic: magic_mutexfree");
1900 MUTEX_DESTROY(MgMUTEXP(mg));
1901 COND_DESTROY(MgCONDP(mg));
1905 #endif /* USE_THREADS */
1910 register char **sigv;
1912 for (sigv = sig_name+1; *sigv; sigv++)
1913 if (strEQ(sig,*sigv))
1914 return sig_num[sigv - sig_name];
1916 if (strEQ(sig,"CHLD"))
1920 if (strEQ(sig,"CLD"))
1929 unwind_handler_stack(void *p)
1932 U32 flags = *(U32*)p;
1935 savestack_ix -= 5; /* Unprotect save in progress. */
1936 /* cxstack_ix-- Not needed, die already unwound it. */
1938 SvREFCNT_dec(sig_sv);
1951 I32 o_save_i = savestack_ix, type;
1954 if (savestack_ix + 15 <= savestack_max)
1956 if (markstack_ptr < markstack_max - 2)
1958 if (retstack_ix < retstack_max - 2)
1960 if (scopestack_ix < scopestack_max - 3)
1964 die("Signal SIG%s received, but no signal handler set.\n",
1967 /* Max number of items pushed there is 3*n or 4. We cannot fix
1968 infinity, so we fix 4 (in fact 5): */
1970 savestack_ix += 5; /* Protect save in progress. */
1971 o_save_i = savestack_ix;
1972 SAVEDESTRUCTOR(unwind_handler_stack, (void*)&flags);
1975 markstack_ptr++; /* Protect mark. */
1978 retstack[retstack_ix] = NULL;
1982 /* sv_2cv is too complicated, try a simpler variant first: */
1983 if (!SvROK(psig_ptr[sig]) || !(cv = (CV*)SvRV(psig_ptr[sig]))
1984 || SvTYPE(cv) != SVt_PVCV)
1985 cv = sv_2cv(psig_ptr[sig],&st,&gv,TRUE);
1987 if (!cv || !CvROOT(cv)) {
1989 warn("SIG%s handler \"%s\" not defined.\n",
1990 sig_name[sig], (gv ? GvENAME(gv)
1997 if(psig_name[sig]) {
1998 sv = SvREFCNT_inc(psig_name[sig]);
2002 sv = sv_newmortal();
2003 sv_setpv(sv,sig_name[sig]);
2006 PUSHSTACK(SI_SIGNAL);
2011 perl_call_sv((SV*)cv, G_DISCARD);
2016 savestack_ix -= 8; /* Unprotect save in progress. */
2025 op = myop; /* Apparently not needed... */
2027 Sv = tSv; /* Restore global temporaries. */