3 * Copyright (c) 1991-1994, 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 /* Omit -- it causes too much grief on mixed systems.
29 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
30 MGVTBL* vtbl = mg->mg_virtual;
32 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
36 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
47 U32 savemagic = SvMAGICAL(sv) | SvREADONLY(sv);
49 assert(SvGMAGICAL(sv));
52 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
54 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
55 MGVTBL* vtbl = mg->mg_virtual;
56 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
57 (*vtbl->svt_get)(sv, mg);
58 if (mg->mg_flags & MGf_GSKIP)
64 SvFLAGS(sv) |= savemagic;
68 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
79 U32 savemagic = SvMAGICAL(sv);
83 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
84 MGVTBL* vtbl = mg->mg_virtual;
85 nextmg = mg->mg_moremagic; /* it may delete itself */
86 if (mg->mg_flags & MGf_GSKIP) {
87 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
90 if (vtbl && vtbl->svt_set)
91 (*vtbl->svt_set)(sv, mg);
96 SvFLAGS(sv) |= savemagic;
100 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
114 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
115 MGVTBL* vtbl = mg->mg_virtual;
116 if (vtbl && vtbl->svt_len) {
117 U32 savemagic = SvMAGICAL(sv);
120 SvFLAGS(sv) |= (SvFLAGS(sv)&(SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
122 /* omit MGf_GSKIP -- not changed here */
123 len = (*vtbl->svt_len)(sv, mg);
125 SvFLAGS(sv) |= savemagic;
127 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
142 U32 savemagic = SvMAGICAL(sv);
145 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
147 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
148 MGVTBL* vtbl = mg->mg_virtual;
149 /* omit GSKIP -- never set here */
151 if (vtbl && vtbl->svt_clear)
152 (*vtbl->svt_clear)(sv, mg);
155 SvFLAGS(sv) |= savemagic;
157 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
168 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
169 if (mg->mg_type == type)
176 mg_copy(sv, nsv, key, klen)
184 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
185 if (isUPPER(mg->mg_type)) {
186 sv_magic(nsv, mg->mg_obj, toLOWER(mg->mg_type), key, klen);
199 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
200 MGVTBL* vtbl = mg->mg_virtual;
201 moremagic = mg->mg_moremagic;
202 if (vtbl && vtbl->svt_free)
203 (*vtbl->svt_free)(sv, mg);
204 if (mg->mg_ptr && mg->mg_type != 'g')
205 Safefree(mg->mg_ptr);
206 if (mg->mg_flags & MGf_REFCOUNTED)
207 SvREFCNT_dec(mg->mg_obj);
214 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
227 switch (*mg->mg_ptr) {
228 case '1': case '2': case '3': case '4':
229 case '5': case '6': case '7': case '8': case '9': case '&':
231 paren = atoi(mg->mg_ptr);
233 if (curpm->op_pmregexp &&
234 paren <= curpm->op_pmregexp->nparens &&
235 (s = curpm->op_pmregexp->startp[paren]) ) {
236 i = curpm->op_pmregexp->endp[paren] - s;
248 paren = curpm->op_pmregexp->lastparen;
256 if (curpm->op_pmregexp &&
257 (s = curpm->op_pmregexp->subbeg) ) {
258 i = curpm->op_pmregexp->startp[0] - s;
270 if (curpm->op_pmregexp &&
271 (s = curpm->op_pmregexp->endp[0]) ) {
272 return (STRLEN) (curpm->op_pmregexp->subend - s);
279 return (STRLEN)ofslen;
281 return (STRLEN)orslen;
284 if (!SvPOK(sv) && SvNIOK(sv))
300 switch (*mg->mg_ptr) {
301 case '\004': /* ^D */
302 sv_setiv(sv,(I32)(debug & 32767));
304 case '\006': /* ^F */
305 sv_setiv(sv,(I32)maxsysfd);
307 case '\010': /* ^H */
308 sv_setiv(sv,(I32)hints);
312 sv_setpv(sv, inplace);
314 sv_setsv(sv,&sv_undef);
316 case '\020': /* ^P */
317 sv_setiv(sv,(I32)perldb);
319 case '\024': /* ^T */
320 sv_setiv(sv,(I32)basetime);
322 case '\027': /* ^W */
323 sv_setiv(sv,(I32)dowarn);
325 case '1': case '2': case '3': case '4':
326 case '5': case '6': case '7': case '8': case '9': case '&':
328 paren = atoi(GvENAME(mg->mg_obj));
330 if (curpm->op_pmregexp &&
331 paren <= curpm->op_pmregexp->nparens &&
332 (s = curpm->op_pmregexp->startp[paren]) &&
333 curpm->op_pmregexp->endp[paren] ) {
334 i = curpm->op_pmregexp->endp[paren] - s;
338 sv_setsv(sv,&sv_undef);
341 sv_setsv(sv,&sv_undef);
346 paren = curpm->op_pmregexp->lastparen;
350 sv_setsv(sv,&sv_undef);
355 if (curpm->op_pmregexp &&
356 (s = curpm->op_pmregexp->subbeg) ) {
357 i = curpm->op_pmregexp->startp[0] - s;
369 if (curpm->op_pmregexp &&
370 (s = curpm->op_pmregexp->endp[0]) ) {
371 sv_setpvn(sv,s, curpm->op_pmregexp->subend - s);
379 if (GvIO(last_in_gv)) {
380 sv_setiv(sv,(I32)IoLINES(GvIO(last_in_gv)));
385 sv_setiv(sv,(I32)statusvalue);
388 s = IoTOP_NAME(GvIOp(defoutgv));
392 sv_setpv(sv,GvENAME(defoutgv));
397 s = IoFMT_NAME(GvIOp(defoutgv));
399 s = GvENAME(defoutgv);
404 sv_setiv(sv,(I32)IoPAGE_LEN(GvIOp(defoutgv)));
407 sv_setiv(sv,(I32)IoLINES_LEFT(GvIOp(defoutgv)));
410 sv_setiv(sv,(I32)IoPAGE(GvIOp(defoutgv)));
418 sv_setiv(sv,(I32)curcop->cop_arybase);
421 sv_setiv(sv, (IoFLAGS(GvIOp(defoutgv)) & IOf_FLUSH) != 0 );
424 sv_setpvn(sv,ofs,ofslen);
427 sv_setpvn(sv,ors,orslen);
433 sv_setnv(sv,(double)errno);
434 sv_setpv(sv, errno ? Strerror(errno) : "");
435 SvNOK_on(sv); /* what a wonderful hack! */
438 sv_setiv(sv,(I32)uid);
441 sv_setiv(sv,(I32)euid);
445 (void)sprintf(s,"%d",(int)gid);
449 (void)sprintf(s,"%d",(int)egid);
457 Groups_t gary[NGROUPS];
459 i = getgroups(NGROUPS,gary);
461 (void)sprintf(s," %ld", (long)gary[i]);
477 magic_getuvar(sv, mg)
481 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
483 if (uf && uf->uf_val)
484 (*uf->uf_val)(uf->uf_index, sv);
497 my_setenv(mg->mg_ptr,s);
498 #ifdef DYNAMIC_ENV_FETCH
499 /* We just undefd an environment var. Is a replacement */
500 /* waiting in the wings? */
503 if (envsvp = hv_fetch(GvHVn(envgv),mg->mg_ptr,mg->mg_len,FALSE))
504 s = SvPV(*envsvp,len);
507 /* And you'll never guess what the dog had */
508 /* in its mouth... */
510 if (s && strEQ(mg->mg_ptr,"PATH")) {
511 char *strend = s + len;
514 s = cpytill(tokenbuf,s,strend,':',&i);
517 || (Stat(tokenbuf,&statbuf) && (statbuf.st_mode & 2)) )
526 magic_clearenv(sv,mg)
530 my_setenv(mg->mg_ptr,Nullch);
542 i = whichsig(mg->mg_ptr); /* ...no, a brick */
543 if (!i && (dowarn || strEQ(mg->mg_ptr,"ALARM")))
544 warn("No such signal: SIG%s", mg->mg_ptr);
545 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
546 (void)signal(i,sighandler);
549 s = SvPV_force(sv,na);
550 if (strEQ(s,"IGNORE"))
552 (void)signal(i,SIG_IGN);
556 else if (strEQ(s,"DEFAULT") || !*s)
557 (void)signal(i,SIG_DFL);
559 (void)signal(i,sighandler);
560 if (!strchr(s,':') && !strchr(s,'\'')) {
561 sprintf(tokenbuf, "main::%s",s);
562 sv_setpv(sv,tokenbuf);
580 magic_setamagic(sv,mg)
584 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
589 #endif /* OVERLOAD */
592 magic_methpack(sv,mg,meth)
605 PUSHs(sv_2mortal(newSVpv(mg->mg_ptr, mg->mg_len)));
606 else if (mg->mg_type == 'p')
607 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
610 if (perl_call_method(meth, G_SCALAR))
611 sv_setsv(sv, *stack_sp--);
623 magic_methpack(sv,mg,"FETCH");
625 mg->mg_flags |= MGf_GSKIP;
640 PUSHs(sv_2mortal(newSVpv(mg->mg_ptr, mg->mg_len)));
641 else if (mg->mg_type == 'p')
642 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
646 perl_call_method("STORE", G_SCALAR|G_DISCARD);
652 magic_clearpack(sv,mg)
656 return magic_methpack(sv,mg,"DELETE");
659 int magic_wipepack(sv,mg)
669 perl_call_method("CLEAR", G_SCALAR|G_DISCARD);
675 magic_nextpack(sv,mg,key)
681 char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
692 if (perl_call_method(meth, G_SCALAR))
693 sv_setsv(key, *stack_sp--);
701 magic_existspack(sv,mg)
705 return magic_methpack(sv,mg,"EXISTS");
709 magic_setdbline(sv,mg)
720 svp = av_fetch(GvAV(gv),atoi(mg->mg_ptr), FALSE);
721 if (svp && SvIOKp(*svp) && (o = (OP*)SvSTASH(*svp)))
724 warn("Can't break at that line\n");
729 magic_getarylen(sv,mg)
733 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + curcop->cop_arybase);
738 magic_setarylen(sv,mg)
742 av_fill((AV*)mg->mg_obj, SvIV(sv) - curcop->cop_arybase);
751 SV* lsv = LvTARG(sv);
753 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
754 mg = mg_find(lsv, 'g');
755 if (mg && mg->mg_len >= 0) {
756 sv_setiv(sv, mg->mg_len + curcop->cop_arybase);
769 SV* lsv = LvTARG(sv);
775 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
776 mg = mg_find(lsv, 'g');
780 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
781 mg = mg_find(lsv, 'g');
783 else if (!SvOK(sv)) {
787 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
789 pos = SvIV(sv) - curcop->cop_arybase;
807 gv_efullname(sv,((GV*)sv));/* a gv value, be nice */
822 if (*s == '*' && s[1])
824 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
829 GvGP(sv) = gp_ref(GvGP(gv));
840 magic_setsubstr(sv,mg)
845 char *tmps = SvPV(sv,len);
846 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
851 magic_gettaint(sv,mg)
860 magic_settaint(sv,mg)
877 do_vecset(sv); /* XXX slurp this routine */
882 magic_setmglob(sv,mg)
905 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
907 if (uf && uf->uf_set)
908 (*uf->uf_set)(uf->uf_index, sv);
920 switch (*mg->mg_ptr) {
921 case '\004': /* ^D */
922 debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
925 case '\006': /* ^F */
926 maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
928 case '\010': /* ^H */
929 hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
935 inplace = savepv(SvPV(sv,na));
939 case '\020': /* ^P */
940 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
949 case '\024': /* ^T */
950 basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
952 case '\027': /* ^W */
953 dowarn = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
957 save_sptr((SV**)&last_in_gv);
959 IoLINES(GvIOp(last_in_gv)) = (long)SvIV(sv);
962 Safefree(IoTOP_NAME(GvIOp(defoutgv)));
963 IoTOP_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
964 IoTOP_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
967 Safefree(IoFMT_NAME(GvIOp(defoutgv)));
968 IoFMT_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
969 IoFMT_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
972 IoPAGE_LEN(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
975 IoLINES_LEFT(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
976 if (IoLINES_LEFT(GvIOp(defoutgv)) < 0L)
977 IoLINES_LEFT(GvIOp(defoutgv)) = 0L;
980 IoPAGE(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
983 IoFLAGS(GvIOp(defoutgv)) &= ~IOf_FLUSH;
984 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) != 0) {
985 IoFLAGS(GvIOp(defoutgv)) |= IOf_FLUSH;
989 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
990 multiline = (i != 0);
994 nrs = rs = SvPV_force(sv,rslen);
996 if (rspara = !rslen) {
1000 nrschar = rschar = rs[rslen - 1];
1003 nrschar = rschar = 0777; /* fake a non-existent char */
1010 ors = savepv(SvPV(sv,orslen));
1015 ofs = savepv(SvPV(sv, ofslen));
1020 ofmt = savepv(SvPV(sv,na));
1023 compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1026 statusvalue = U_S(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1029 errno = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv); /* will anyone ever use this? */
1032 uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1034 delaymagic |= DM_RUID;
1035 break; /* don't do magic till later */
1038 (void)setruid((Uid_t)uid);
1041 (void)setreuid((Uid_t)uid, (Uid_t)-1);
1042 #ifdef HAS_SETRESUID
1043 (void)setresuid((Uid_t)uid, (Uid_t)-1, (Uid_t)-1);
1045 if (uid == euid) /* special case $< = $> */
1048 uid = (I32)getuid();
1049 croak("setruid() not implemented");
1054 uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1055 tainting |= (euid != uid || egid != gid);
1058 euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1060 delaymagic |= DM_EUID;
1061 break; /* don't do magic till later */
1064 (void)seteuid((Uid_t)euid);
1067 (void)setreuid((Uid_t)-1, (Uid_t)euid);
1069 #ifdef HAS_SETRESUID
1070 (void)setresuid((Uid_t)-1, (Uid_t)euid, (Uid_t)-1);
1072 if (euid == uid) /* special case $> = $< */
1075 euid = (I32)geteuid();
1076 croak("seteuid() not implemented");
1081 euid = (I32)geteuid();
1082 tainting |= (euid != uid || egid != gid);
1085 gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1087 delaymagic |= DM_RGID;
1088 break; /* don't do magic till later */
1091 (void)setrgid((Gid_t)gid);
1094 (void)setregid((Gid_t)gid, (Gid_t)-1);
1096 #ifdef HAS_SETRESGID
1097 (void)setresgid((Gid_t)gid, (Gid_t)-1, (Gid_t) 1);
1099 if (gid == egid) /* special case $( = $) */
1102 croak("setrgid() not implemented");
1106 gid = (I32)getgid();
1107 tainting |= (euid != uid || egid != gid);
1110 egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1112 delaymagic |= DM_EGID;
1113 break; /* don't do magic till later */
1116 (void)setegid((Gid_t)egid);
1119 (void)setregid((Gid_t)-1, (Gid_t)egid);
1121 #ifdef HAS_SETRESGID
1122 (void)setresgid((Gid_t)-1, (Gid_t)egid, (Gid_t)-1);
1124 if (egid == gid) /* special case $) = $( */
1127 croak("setegid() not implemented");
1131 egid = (I32)getegid();
1132 tainting |= (euid != uid || egid != gid);
1135 chopset = SvPV_force(sv,na);
1141 /* See if all the arguments are contiguous in memory */
1142 for (i = 1; i < origargc; i++) {
1143 if (origargv[i] == s + 1)
1144 s += strlen(++s); /* this one is ok too */
1146 if (origenviron[0] == s + 1) { /* can grab env area too? */
1147 my_setenv("NoNeSuCh", Nullch);
1148 /* force copy of environment */
1149 for (i = 0; origenviron[i]; i++)
1150 if (origenviron[i] == s + 1)
1153 origalen = s - origargv[0];
1155 s = SvPV_force(sv,len);
1157 if (i >= origalen) {
1161 Copy(s, origargv[0], i, char);
1164 Copy(s, origargv[0], i, char);
1167 while (++i < origalen)
1170 for (i = 1; i < origargc; i++)
1171 origargv[i] = Nullch;
1182 register char **sigv;
1184 for (sigv = sig_name+1; *sigv; sigv++)
1185 if (strEQ(sig,*sigv))
1186 return sigv - sig_name;
1188 if (strEQ(sig,"CHLD"))
1192 if (strEQ(sig,"CLD"))
1209 #ifdef OS2 /* or anybody else who requires SIG_ACK */
1210 signal(sig, SIG_ACK);
1213 cv = sv_2cv(*hv_fetch(GvHVn(siggv),sig_name[sig],strlen(sig_name[sig]),
1216 if (!cv || !CvROOT(cv) &&
1217 *sig_name[sig] == 'C' && instr(sig_name[sig],"LD")) {
1219 if (sig_name[sig][1] == 'H')
1220 cv = sv_2cv(*hv_fetch(GvHVn(siggv),"CLD",3,TRUE),
1223 cv = sv_2cv(*hv_fetch(GvHVn(siggv),"CHLD",4,TRUE),
1227 if (!cv || !CvROOT(cv)) {
1229 warn("SIG%s handler \"%s\" not defined.\n",
1230 sig_name[sig], GvENAME(gv) );
1235 if (stack != signalstack)
1236 AvFILL(signalstack) = 0;
1237 SWITCHSTACK(stack, signalstack);
1239 sv = sv_newmortal();
1240 sv_setpv(sv,sig_name[sig]);
1245 perl_call_sv((SV*)cv, G_DISCARD);
1247 SWITCHSTACK(signalstack, oldstack);