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));
486 sv_setsv(sv,&sv_undef);
489 if (curpm && (rx = curpm->op_pmregexp)) {
490 paren = rx->lastparen;
494 sv_setsv(sv,&sv_undef);
497 if (curpm && (rx = curpm->op_pmregexp)) {
498 if ((s = rx->subbeg) && rx->startp[0]) {
499 i = rx->startp[0] - s;
503 sv_setsv(sv,&sv_undef);
506 if (curpm && (rx = curpm->op_pmregexp)) {
507 if (rx->subend && (s = rx->endp[0])) {
512 sv_setsv(sv,&sv_undef);
516 if (GvIO(last_in_gv)) {
517 sv_setiv(sv, (IV)IoLINES(GvIO(last_in_gv)));
524 sv_setiv(sv, (IV)STATUS_CURRENT);
525 #ifdef COMPLEX_STATUS
526 LvTARGOFF(sv) = statusvalue;
527 LvTARGLEN(sv) = statusvalue_vms;
532 s = IoTOP_NAME(GvIOp(defoutgv));
536 sv_setpv(sv,GvENAME(defoutgv));
541 s = IoFMT_NAME(GvIOp(defoutgv));
543 s = GvENAME(defoutgv);
548 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(defoutgv)));
551 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(defoutgv)));
554 sv_setiv(sv, (IV)IoPAGE(GvIOp(defoutgv)));
562 WITH_THR(sv_setiv(sv, (IV)curcop->cop_arybase));
565 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(defoutgv)) & IOf_FLUSH) != 0 );
568 sv_setpvn(sv,ofs,ofslen);
571 sv_setpvn(sv,ors,orslen);
578 sv_setnv(sv, (double)((errno == EVMSERR) ? vaxc$errno : errno));
579 sv_setpv(sv, errno ? Strerror(errno) : "");
582 int saveerrno = errno;
583 sv_setnv(sv, (double)errno);
585 if (errno == errno_isOS2) sv_setpv(sv, os2error(Perl_rc));
588 sv_setpv(sv, errno ? Strerror(errno) : "");
592 SvNOK_on(sv); /* what a wonderful hack! */
595 sv_setiv(sv, (IV)uid);
598 sv_setiv(sv, (IV)euid);
601 sv_setiv(sv, (IV)gid);
602 sv_setpvf(sv, "%Vd", (IV)gid);
605 sv_setiv(sv, (IV)egid);
606 sv_setpvf(sv, "%Vd", (IV)egid);
610 Groups_t gary[NGROUPS];
611 i = getgroups(NGROUPS,gary);
613 sv_catpvf(sv, " %Vd", (IV)gary[i]);
616 SvIOK_on(sv); /* what a wonderful hack! */
624 sv_setsv(sv, thr->errsv);
626 #endif /* USE_THREADS */
632 magic_getuvar(SV *sv, MAGIC *mg)
634 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
636 if (uf && uf->uf_val)
637 (*uf->uf_val)(uf->uf_index, sv);
642 magic_setenv(SV *sv, MAGIC *mg)
653 #ifdef DYNAMIC_ENV_FETCH
654 /* We just undefd an environment var. Is a replacement */
655 /* waiting in the wings? */
658 if ((valp = hv_fetch(GvHVn(envgv), ptr, klen, FALSE)))
659 s = SvPV(*valp, len);
663 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
664 /* And you'll never guess what the dog had */
665 /* in its mouth... */
667 MgTAINTEDDIR_off(mg);
669 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
670 char pathbuf[256], eltbuf[256], *cp, *elt = s;
674 do { /* DCL$PATH may be a search list */
675 while (1) { /* as may dev portion of any element */
676 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
677 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
678 cando_by_name(S_IWUSR,0,elt) ) {
683 if ((cp = strchr(elt, ':')) != Nullch)
685 if (my_trnlnm(elt, eltbuf, j++))
691 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
694 if (s && klen == 4 && strEQ(ptr,"PATH")) {
695 char *strend = s + len;
700 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
703 if (i >= sizeof tmpbuf /* too long -- assume the worst */
705 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
712 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
718 magic_clearenv(SV *sv, MAGIC *mg)
720 my_setenv(MgPV(mg,na),Nullch);
725 magic_set_all_env(SV *sv, MAGIC *mg)
728 die("Can't make list assignment to %%ENV on this system");
733 magic_clear_all_env(sv,mg);
734 hv_iterinit((HV*)sv);
735 while (entry = hv_iternext((HV*)sv)) {
737 my_setenv(hv_iterkey(entry, &keylen),
738 SvPV(hv_iterval((HV*)sv, entry), na));
746 magic_clear_all_env(SV *sv, MAGIC *mg)
749 die("Can't make list assignment to %%ENV on this system");
752 char *envv = GetEnvironmentStrings();
756 char *end = strchr(cur,'=');
757 if (end && end != cur) {
759 my_setenv(cur,Nullch);
761 cur += strlen(end+1)+1;
763 else if ((len = strlen(cur)))
766 FreeEnvironmentStrings(envv);
770 if (environ == origenviron)
771 New(901, environ, 1, char*);
773 for (i = 0; environ[i]; i++)
774 Safefree(environ[i]);
783 magic_getsig(SV *sv, MAGIC *mg)
786 /* Are we fetching a signal entry? */
787 i = whichsig(MgPV(mg,na));
790 sv_setsv(sv,psig_ptr[i]);
792 Sighandler_t sigstate = rsignal_state(i);
794 /* cache state so we don't fetch it again */
795 if(sigstate == SIG_IGN)
796 sv_setpv(sv,"IGNORE");
798 sv_setsv(sv,&sv_undef);
799 psig_ptr[i] = SvREFCNT_inc(sv);
806 magic_clearsig(SV *sv, MAGIC *mg)
809 /* Are we clearing a signal entry? */
810 i = whichsig(MgPV(mg,na));
813 SvREFCNT_dec(psig_ptr[i]);
817 SvREFCNT_dec(psig_name[i]);
825 magic_setsig(SV *sv, MAGIC *mg)
834 if (strEQ(s,"__DIE__"))
836 else if (strEQ(s,"__WARN__"))
838 else if (strEQ(s,"__PARSE__"))
841 croak("No such hook: %s", s);
849 i = whichsig(s); /* ...no, a brick */
851 if (dowarn || strEQ(s,"ALARM"))
852 warn("No such signal: SIG%s", s);
855 SvREFCNT_dec(psig_name[i]);
856 SvREFCNT_dec(psig_ptr[i]);
857 psig_ptr[i] = SvREFCNT_inc(sv);
858 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
859 psig_name[i] = newSVpv(s, strlen(s));
860 SvREADONLY_on(psig_name[i]);
862 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
864 (void)rsignal(i, sighandlerp);
866 *svp = SvREFCNT_inc(sv);
869 s = SvPV_force(sv,na);
870 if (strEQ(s,"IGNORE")) {
872 (void)rsignal(i, SIG_IGN);
876 else if (strEQ(s,"DEFAULT") || !*s) {
878 (void)rsignal(i, SIG_DFL);
884 * We should warn if HINT_STRICT_REFS, but without
885 * access to a known hint bit in a known OP, we can't
886 * tell whether HINT_STRICT_REFS is in force or not.
888 if (!strchr(s,':') && !strchr(s,'\''))
889 sv_setpv(sv, form("main::%s", s));
891 (void)rsignal(i, sighandlerp);
893 *svp = SvREFCNT_inc(sv);
899 magic_setisa(SV *sv, MAGIC *mg)
908 magic_setamagic(SV *sv, MAGIC *mg)
910 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
915 #endif /* OVERLOAD */
918 magic_getnkeys(SV *sv, MAGIC *mg)
920 HV *hv = (HV*)LvTARG(sv);
925 (void) hv_iterinit(hv);
926 if (!SvRMAGICAL(hv) || !mg_find((SV*)hv,'P'))
930 while (entry = hv_iternext(hv)) {
941 magic_setnkeys(SV *sv, MAGIC *mg)
944 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
949 /* caller is responsible for stack switching/cleanup */
951 magic_methcall(MAGIC *mg, char *meth, I32 flags, int n, SV *val)
961 PUSHs(sv_2mortal(newSVpv(mg->mg_ptr, mg->mg_len)));
962 else if (mg->mg_len == HEf_SVKEY)
963 PUSHs((SV*)mg->mg_ptr);
965 else if (mg->mg_type == 'p') {
966 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
974 return perl_call_method(meth, flags);
978 magic_methpack(SV *sv, MAGIC *mg, char *meth)
984 PUSHSTACKi(PERLSI_MAGIC);
986 if (magic_methcall(mg, meth, G_SCALAR, 2, NULL)) {
987 sv_setsv(sv, *stack_sp--);
997 magic_getpack(SV *sv, MAGIC *mg)
999 magic_methpack(sv,mg,"FETCH");
1001 mg->mg_flags |= MGf_GSKIP;
1006 magic_setpack(SV *sv, MAGIC *mg)
1010 PUSHSTACKi(PERLSI_MAGIC);
1011 magic_methcall(mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1018 magic_clearpack(SV *sv, MAGIC *mg)
1020 return magic_methpack(sv,mg,"DELETE");
1025 magic_sizepack(SV *sv, MAGIC *mg)
1032 PUSHSTACKi(PERLSI_MAGIC);
1033 if (magic_methcall(mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1035 retval = (U32) SvIV(sv)-1;
1043 int magic_wipepack(SV *sv, MAGIC *mg)
1048 PUSHSTACKi(PERLSI_MAGIC);
1052 perl_call_method("CLEAR", G_SCALAR|G_DISCARD);
1059 magic_nextpack(SV *sv, MAGIC *mg, SV *key)
1062 char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1066 PUSHSTACKi(PERLSI_MAGIC);
1074 if (perl_call_method(meth, G_SCALAR))
1075 sv_setsv(key, *stack_sp--);
1084 magic_existspack(SV *sv, MAGIC *mg)
1086 return magic_methpack(sv,mg,"EXISTS");
1090 magic_setdbline(SV *sv, MAGIC *mg)
1100 svp = av_fetch(GvAV(gv),
1101 atoi(MgPV(mg,na)), FALSE);
1102 if (svp && SvIOKp(*svp) && (o = (OP*)SvSTASH(*svp)))
1105 warn("Can't break at that line\n");
1110 magic_getarylen(SV *sv, MAGIC *mg)
1113 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + curcop->cop_arybase);
1118 magic_setarylen(SV *sv, MAGIC *mg)
1121 av_fill((AV*)mg->mg_obj, SvIV(sv) - curcop->cop_arybase);
1126 magic_getpos(SV *sv, MAGIC *mg)
1128 SV* lsv = LvTARG(sv);
1130 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1131 mg = mg_find(lsv, 'g');
1132 if (mg && mg->mg_len >= 0) {
1134 sv_setiv(sv, mg->mg_len + curcop->cop_arybase);
1143 magic_setpos(SV *sv, MAGIC *mg)
1145 SV* lsv = LvTARG(sv);
1151 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1152 mg = mg_find(lsv, 'g');
1156 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
1157 mg = mg_find(lsv, 'g');
1159 else if (!SvOK(sv)) {
1163 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1165 WITH_THR(pos = SvIV(sv) - curcop->cop_arybase);
1174 mg->mg_flags &= ~MGf_MINMATCH;
1180 magic_getglob(SV *sv, MAGIC *mg)
1182 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1184 gv_efullname3(sv,((GV*)sv), "*");
1188 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1193 magic_setglob(SV *sv, MAGIC *mg)
1201 if (*s == '*' && s[1])
1203 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1208 GvGP(sv) = gp_ref(GvGP(gv));
1213 magic_getsubstr(SV *sv, MAGIC *mg)
1216 SV *lsv = LvTARG(sv);
1217 char *tmps = SvPV(lsv,len);
1218 I32 offs = LvTARGOFF(sv);
1219 I32 rem = LvTARGLEN(sv);
1223 if (rem + offs > len)
1225 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1230 magic_setsubstr(SV *sv, MAGIC *mg)
1233 char *tmps = SvPV(sv,len);
1234 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
1239 magic_gettaint(SV *sv, MAGIC *mg)
1242 TAINT_IF((mg->mg_len & 1) ||
1243 (mg->mg_len & 2) && mg->mg_obj == sv); /* kludge */
1248 magic_settaint(SV *sv, MAGIC *mg)
1252 if (localizing == 1)
1265 magic_getvec(SV *sv, MAGIC *mg)
1267 SV *lsv = LvTARG(sv);
1269 unsigned long retnum;
1279 s = (unsigned char *) SvPV(lsv, lsvlen);
1280 offset = LvTARGOFF(sv);
1281 size = LvTARGLEN(sv);
1282 len = (offset + size + 7) / 8;
1284 /* Copied from pp_vec() */
1292 if (offset >= lsvlen)
1295 retnum = (unsigned long) s[offset] << 8;
1297 else if (size == 32) {
1298 if (offset >= lsvlen)
1300 else if (offset + 1 >= lsvlen)
1301 retnum = (unsigned long) s[offset] << 24;
1302 else if (offset + 2 >= lsvlen)
1303 retnum = ((unsigned long) s[offset] << 24) +
1304 ((unsigned long) s[offset + 1] << 16);
1306 retnum = ((unsigned long) s[offset] << 24) +
1307 ((unsigned long) s[offset + 1] << 16) +
1308 (s[offset + 2] << 8);
1313 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
1318 else if (size == 16)
1319 retnum = ((unsigned long) s[offset] << 8) + s[offset+1];
1320 else if (size == 32)
1321 retnum = ((unsigned long) s[offset] << 24) +
1322 ((unsigned long) s[offset + 1] << 16) +
1323 (s[offset + 2] << 8) + s[offset+3];
1326 sv_setuv(sv, (UV)retnum);
1331 magic_setvec(SV *sv, MAGIC *mg)
1333 do_vecset(sv); /* XXX slurp this routine */
1338 magic_getdefelem(SV *sv, MAGIC *mg)
1341 if (LvTARGLEN(sv)) {
1343 HV* hv = (HV*)LvTARG(sv);
1344 HE* he = hv_fetch_ent(hv, mg->mg_obj, FALSE, 0);
1349 AV* av = (AV*)LvTARG(sv);
1350 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1351 targ = AvARRAY(av)[LvTARGOFF(sv)];
1353 if (targ && targ != &sv_undef) {
1354 dTHR; /* just for SvREFCNT_dec */
1355 /* somebody else defined it for us */
1356 SvREFCNT_dec(LvTARG(sv));
1357 LvTARG(sv) = SvREFCNT_inc(targ);
1359 SvREFCNT_dec(mg->mg_obj);
1360 mg->mg_obj = Nullsv;
1361 mg->mg_flags &= ~MGf_REFCOUNTED;
1366 sv_setsv(sv, targ ? targ : &sv_undef);
1371 magic_setdefelem(SV *sv, MAGIC *mg)
1376 sv_setsv(LvTARG(sv), sv);
1377 SvSETMAGIC(LvTARG(sv));
1383 magic_freedefelem(SV *sv, MAGIC *mg)
1385 SvREFCNT_dec(LvTARG(sv));
1390 vivify_defelem(SV *sv)
1392 dTHR; /* just for SvREFCNT_inc and SvREFCNT_dec*/
1396 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, 'y')))
1399 HV* hv = (HV*)LvTARG(sv);
1400 HE* he = hv_fetch_ent(hv, mg->mg_obj, TRUE, 0);
1401 if (!he || (value = HeVAL(he)) == &sv_undef)
1402 croak(no_helem, SvPV(mg->mg_obj, na));
1405 AV* av = (AV*)LvTARG(sv);
1406 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1407 LvTARG(sv) = Nullsv; /* array can't be extended */
1409 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1410 if (!svp || (value = *svp) == &sv_undef)
1411 croak(no_aelem, (I32)LvTARGOFF(sv));
1414 (void)SvREFCNT_inc(value);
1415 SvREFCNT_dec(LvTARG(sv));
1418 SvREFCNT_dec(mg->mg_obj);
1419 mg->mg_obj = Nullsv;
1420 mg->mg_flags &= ~MGf_REFCOUNTED;
1424 magic_setmglob(SV *sv, MAGIC *mg)
1432 magic_setbm(SV *sv, MAGIC *mg)
1434 sv_unmagic(sv, 'B');
1440 magic_setfm(SV *sv, MAGIC *mg)
1442 sv_unmagic(sv, 'f');
1448 magic_setuvar(SV *sv, MAGIC *mg)
1450 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1452 if (uf && uf->uf_set)
1453 (*uf->uf_set)(uf->uf_index, sv);
1458 magic_freeregexp(SV *sv, MAGIC *mg)
1460 regexp *re = (regexp *)mg->mg_obj;
1465 #ifdef USE_LOCALE_COLLATE
1467 magic_setcollxfrm(SV *sv, MAGIC *mg)
1470 * René Descartes said "I think not."
1471 * and vanished with a faint plop.
1474 Safefree(mg->mg_ptr);
1480 #endif /* USE_LOCALE_COLLATE */
1483 magic_set(SV *sv, MAGIC *mg)
1489 switch (*mg->mg_ptr) {
1490 case '\001': /* ^A */
1491 sv_setsv(bodytarget, sv);
1493 case '\004': /* ^D */
1494 debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
1495 DEBUG_x(dump_all());
1497 case '\005': /* ^E */
1499 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1502 SetLastError( SvIV(sv) );
1504 /* will anyone ever use this? */
1505 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1509 case '\006': /* ^F */
1510 maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1512 case '\010': /* ^H */
1513 hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1519 inplace = savepv(SvPV(sv,na));
1523 case '\017': /* ^O */
1527 osname = savepv(SvPV(sv,na));
1531 case '\020': /* ^P */
1532 perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1534 case '\024': /* ^T */
1536 basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1538 basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1541 case '\027': /* ^W */
1542 dowarn = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1546 if (localizing == 1)
1547 save_sptr((SV**)&last_in_gv);
1549 else if (SvOK(sv) && GvIO(last_in_gv))
1550 IoLINES(GvIOp(last_in_gv)) = (long)SvIV(sv);
1553 Safefree(IoTOP_NAME(GvIOp(defoutgv)));
1554 IoTOP_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1555 IoTOP_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1558 Safefree(IoFMT_NAME(GvIOp(defoutgv)));
1559 IoFMT_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1560 IoFMT_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1563 IoPAGE_LEN(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1566 IoLINES_LEFT(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1567 if (IoLINES_LEFT(GvIOp(defoutgv)) < 0L)
1568 IoLINES_LEFT(GvIOp(defoutgv)) = 0L;
1571 IoPAGE(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1575 IO *io = GvIOp(defoutgv);
1576 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1577 IoFLAGS(io) &= ~IOf_FLUSH;
1579 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1580 PerlIO *ofp = IoOFP(io);
1582 (void)PerlIO_flush(ofp);
1583 IoFLAGS(io) |= IOf_FLUSH;
1589 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1590 multiline = (i != 0);
1596 rs = SvREFCNT_inc(nrs);
1601 if (SvOK(sv) || SvGMAGICAL(sv))
1602 ors = savepv(SvPV(sv,orslen));
1611 ofs = savepv(SvPV(sv, ofslen));
1616 ofmt = savepv(SvPV(sv,na));
1619 compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1622 #ifdef COMPLEX_STATUS
1623 if (localizing == 2) {
1624 statusvalue = LvTARGOFF(sv);
1625 statusvalue_vms = LvTARGLEN(sv);
1629 #ifdef VMSISH_STATUS
1631 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1634 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1637 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
1638 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
1641 uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1643 delaymagic |= DM_RUID;
1644 break; /* don't do magic till later */
1647 (void)setruid((Uid_t)uid);
1650 (void)setreuid((Uid_t)uid, (Uid_t)-1);
1652 #ifdef HAS_SETRESUID
1653 (void)setresuid((Uid_t)uid, (Uid_t)-1, (Uid_t)-1);
1655 if (uid == euid) /* special case $< = $> */
1656 (void)PerlProc_setuid(uid);
1658 uid = (I32)PerlProc_getuid();
1659 croak("setruid() not implemented");
1664 uid = (I32)PerlProc_getuid();
1665 tainting |= (uid && (euid != uid || egid != gid));
1668 euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1670 delaymagic |= DM_EUID;
1671 break; /* don't do magic till later */
1674 (void)seteuid((Uid_t)euid);
1677 (void)setreuid((Uid_t)-1, (Uid_t)euid);
1679 #ifdef HAS_SETRESUID
1680 (void)setresuid((Uid_t)-1, (Uid_t)euid, (Uid_t)-1);
1682 if (euid == uid) /* special case $> = $< */
1683 PerlProc_setuid(euid);
1685 euid = (I32)PerlProc_geteuid();
1686 croak("seteuid() not implemented");
1691 euid = (I32)PerlProc_geteuid();
1692 tainting |= (uid && (euid != uid || egid != gid));
1695 gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1697 delaymagic |= DM_RGID;
1698 break; /* don't do magic till later */
1701 (void)setrgid((Gid_t)gid);
1704 (void)setregid((Gid_t)gid, (Gid_t)-1);
1706 #ifdef HAS_SETRESGID
1707 (void)setresgid((Gid_t)gid, (Gid_t)-1, (Gid_t) 1);
1709 if (gid == egid) /* special case $( = $) */
1710 (void)PerlProc_setgid(gid);
1712 gid = (I32)PerlProc_getgid();
1713 croak("setrgid() not implemented");
1718 gid = (I32)PerlProc_getgid();
1719 tainting |= (uid && (euid != uid || egid != gid));
1722 #ifdef HAS_SETGROUPS
1724 char *p = SvPV(sv, na);
1725 Groups_t gary[NGROUPS];
1727 SET_NUMERIC_STANDARD();
1730 egid = I_V(atof(p));
1731 for (i = 0; i < NGROUPS; ++i) {
1732 while (*p && !isSPACE(*p))
1738 gary[i] = I_V(atof(p));
1741 (void)setgroups(i, gary);
1743 #else /* HAS_SETGROUPS */
1744 egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1745 #endif /* HAS_SETGROUPS */
1747 delaymagic |= DM_EGID;
1748 break; /* don't do magic till later */
1751 (void)setegid((Gid_t)egid);
1754 (void)setregid((Gid_t)-1, (Gid_t)egid);
1756 #ifdef HAS_SETRESGID
1757 (void)setresgid((Gid_t)-1, (Gid_t)egid, (Gid_t)-1);
1759 if (egid == gid) /* special case $) = $( */
1760 (void)PerlProc_setgid(egid);
1762 egid = (I32)PerlProc_getegid();
1763 croak("setegid() not implemented");
1768 egid = (I32)PerlProc_getegid();
1769 tainting |= (uid && (euid != uid || egid != gid));
1772 chopset = SvPV_force(sv,na);
1778 /* See if all the arguments are contiguous in memory */
1779 for (i = 1; i < origargc; i++) {
1780 if (origargv[i] == s + 1
1782 || origargv[i] == s + 2
1785 s += strlen(++s); /* this one is ok too */
1789 /* can grab env area too? */
1790 if (origenviron && (origenviron[0] == s + 1
1792 || (origenviron[0] == s + 9 && (s += 8))
1795 my_setenv("NoNe SuCh", Nullch);
1796 /* force copy of environment */
1797 for (i = 0; origenviron[i]; i++)
1798 if (origenviron[i] == s + 1)
1803 origalen = s - origargv[0];
1805 s = SvPV_force(sv,len);
1807 if (i >= origalen) {
1809 /* don't allow system to limit $0 seen by script */
1810 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
1811 Copy(s, origargv[0], i, char);
1816 Copy(s, origargv[0], i, char);
1819 while (++i < origalen)
1822 for (i = 1; i < origargc; i++)
1823 origargv[i] = Nullch;
1828 sv_setsv(thr->errsv, sv);
1830 #endif /* USE_THREADS */
1837 magic_mutexfree(SV *sv, MAGIC *mg)
1840 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: magic_mutexfree 0x%lx\n",
1841 (unsigned long)thr, (unsigned long)sv);)
1843 croak("panic: magic_mutexfree");
1844 MUTEX_DESTROY(MgMUTEXP(mg));
1845 COND_DESTROY(MgCONDP(mg));
1849 #endif /* USE_THREADS */
1854 register char **sigv;
1856 for (sigv = sig_name+1; *sigv; sigv++)
1857 if (strEQ(sig,*sigv))
1858 return sig_num[sigv - sig_name];
1860 if (strEQ(sig,"CHLD"))
1864 if (strEQ(sig,"CLD"))
1873 unwind_handler_stack(void *p)
1876 U32 flags = *(U32*)p;
1879 savestack_ix -= 5; /* Unprotect save in progress. */
1880 /* cxstack_ix-- Not needed, die already unwound it. */
1882 SvREFCNT_dec(sig_sv);
1895 I32 o_save_i = savestack_ix, type;
1898 if (savestack_ix + 15 <= savestack_max)
1900 if (markstack_ptr < markstack_max - 2)
1902 if (retstack_ix < retstack_max - 2)
1904 if (scopestack_ix < scopestack_max - 3)
1908 die("Signal SIG%s received, but no signal handler set.\n",
1911 /* Max number of items pushed there is 3*n or 4. We cannot fix
1912 infinity, so we fix 4 (in fact 5): */
1914 savestack_ix += 5; /* Protect save in progress. */
1915 o_save_i = savestack_ix;
1916 SAVEDESTRUCTOR(unwind_handler_stack, (void*)&flags);
1919 markstack_ptr++; /* Protect mark. */
1922 retstack[retstack_ix] = NULL;
1926 /* sv_2cv is too complicated, try a simpler variant first: */
1927 if (!SvROK(psig_ptr[sig]) || !(cv = (CV*)SvRV(psig_ptr[sig]))
1928 || SvTYPE(cv) != SVt_PVCV)
1929 cv = sv_2cv(psig_ptr[sig],&st,&gv,TRUE);
1931 if (!cv || !CvROOT(cv)) {
1933 warn("SIG%s handler \"%s\" not defined.\n",
1934 sig_name[sig], (gv ? GvENAME(gv)
1941 if(psig_name[sig]) {
1942 sv = SvREFCNT_inc(psig_name[sig]);
1946 sv = sv_newmortal();
1947 sv_setpv(sv,sig_name[sig]);
1950 PUSHSTACKi(PERLSI_SIGNAL);
1955 perl_call_sv((SV*)cv, G_DISCARD);
1960 savestack_ix -= 8; /* Unprotect save in progress. */
1969 op = myop; /* Apparently not needed... */
1971 Sv = tSv; /* Restore global temporaries. */