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));
46 assert(SvMAGICAL(sv));
49 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
50 SAVEDESTRUCTOR(restore_magic, mgs);
54 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
64 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
67 SvFLAGS(sv) |= mgs->mgs_flags;
71 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
81 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
82 MGVTBL* vtbl = mg->mg_virtual;
84 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
88 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
104 save_magic(&mgs, sv);
107 while ((mg = *mgp) != 0) {
108 MGVTBL* vtbl = mg->mg_virtual;
109 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
110 (*vtbl->svt_get)(sv, mg);
111 /* Ignore this magic if it's been deleted */
112 if ((mg == (mgp_valid ? *mgp : SvMAGIC(sv))) &&
113 (mg->mg_flags & MGf_GSKIP))
116 /* Advance to next magic (complicated by possible deletion) */
117 if (mg == (mgp_valid ? *mgp : SvMAGIC(sv))) {
118 mgp = &mg->mg_moremagic;
122 mgp = &SvMAGIC(sv); /* Re-establish pointer after sv_upgrade */
138 save_magic(&mgs, sv);
140 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
141 MGVTBL* vtbl = mg->mg_virtual;
142 nextmg = mg->mg_moremagic; /* it may delete itself */
143 if (mg->mg_flags & MGf_GSKIP) {
144 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
147 if (vtbl && vtbl->svt_set)
148 (*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) {
169 save_magic(&mgs, sv);
170 /* omit MGf_GSKIP -- not changed here */
171 len = (*vtbl->svt_len)(sv, mg);
177 junk = SvPV(sv, len);
189 save_magic(&mgs, sv);
191 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
192 MGVTBL* vtbl = mg->mg_virtual;
193 /* omit GSKIP -- never set here */
195 if (vtbl && vtbl->svt_clear)
196 (*vtbl->svt_clear)(sv, mg);
209 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
210 if (mg->mg_type == type)
217 mg_copy(sv, nsv, key, klen)
225 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
226 if (isUPPER(mg->mg_type)) {
227 sv_magic(nsv, mg->mg_obj, toLOWER(mg->mg_type), key, klen);
240 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
241 MGVTBL* vtbl = mg->mg_virtual;
242 moremagic = mg->mg_moremagic;
243 if (vtbl && vtbl->svt_free)
244 (*vtbl->svt_free)(sv, mg);
245 if (mg->mg_ptr && mg->mg_type != 'g')
247 Safefree(mg->mg_ptr);
248 else if (mg->mg_len == HEf_SVKEY)
249 SvREFCNT_dec((SV*)mg->mg_ptr);
250 if (mg->mg_flags & MGf_REFCOUNTED)
251 SvREFCNT_dec(mg->mg_obj);
258 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
273 switch (*mg->mg_ptr) {
274 case '1': case '2': case '3': case '4':
275 case '5': case '6': case '7': case '8': case '9': case '&':
276 if (curpm && (rx = curpm->op_pmregexp)) {
277 paren = atoi(mg->mg_ptr);
279 if (paren <= rx->nparens &&
280 (s = rx->startp[paren]) &&
281 (t = rx->endp[paren]))
290 if (curpm && (rx = curpm->op_pmregexp)) {
291 paren = rx->lastparen;
297 if (curpm && (rx = curpm->op_pmregexp)) {
298 if ((s = rx->subbeg) && rx->startp[0]) {
299 i = rx->startp[0] - s;
306 if (curpm && (rx = curpm->op_pmregexp)) {
307 if (rx->subend && (s = rx->endp[0])) {
315 return (STRLEN)ofslen;
317 return (STRLEN)orslen;
320 if (!SvPOK(sv) && SvNIOK(sv))
338 switch (*mg->mg_ptr) {
339 case '\001': /* ^A */
340 sv_setsv(sv, bodytarget);
342 case '\004': /* ^D */
343 sv_setiv(sv, (IV)(debug & 32767));
345 case '\005': /* ^E */
348 # include <descrip.h>
349 # include <starlet.h>
351 $DESCRIPTOR(msgdsc,msg);
352 sv_setnv(sv,(double) vaxc$errno);
353 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
354 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
360 if (!(_emx_env & 0x200)) { /* Under DOS */
361 sv_setnv(sv, (double)errno);
362 sv_setpv(sv, errno ? Strerror(errno) : "");
364 if (errno != errno_isOS2)
365 Perl_rc = _syserrno();
366 sv_setnv(sv, (double)Perl_rc);
367 sv_setpv(sv, os2error(Perl_rc));
370 sv_setnv(sv, (double)errno);
371 sv_setpv(sv, errno ? Strerror(errno) : "");
374 SvNOK_on(sv); /* what a wonderful hack! */
376 case '\006': /* ^F */
377 sv_setiv(sv, (IV)maxsysfd);
379 case '\010': /* ^H */
380 sv_setiv(sv, (IV)hints);
384 sv_setpv(sv, inplace);
386 sv_setsv(sv, &sv_undef);
388 case '\017': /* ^O */
389 sv_setpv(sv, osname);
391 case '\020': /* ^P */
392 sv_setiv(sv, (IV)perldb);
394 case '\023': /* ^S */
395 if (lex_state != LEX_NOTPARSING)
402 case '\024': /* ^T */
404 sv_setnv(sv, basetime);
406 sv_setiv(sv, (IV)basetime);
409 case '\027': /* ^W */
410 sv_setiv(sv, (IV)dowarn);
412 case '1': case '2': case '3': case '4':
413 case '5': case '6': case '7': case '8': case '9': case '&':
414 if (curpm && (rx = curpm->op_pmregexp)) {
415 paren = atoi(GvENAME((GV*)mg->mg_obj));
417 if (paren <= rx->nparens &&
418 (s = rx->startp[paren]) &&
419 (t = rx->endp[paren]))
426 was_tainted = tainted;
431 tainted = was_tainted || rx->exec_tainted;
436 sv_setsv(sv,&sv_undef);
439 if (curpm && (rx = curpm->op_pmregexp)) {
440 paren = rx->lastparen;
444 sv_setsv(sv,&sv_undef);
447 if (curpm && (rx = curpm->op_pmregexp)) {
448 if ((s = rx->subbeg) && rx->startp[0]) {
449 i = rx->startp[0] - s;
453 sv_setsv(sv,&sv_undef);
456 if (curpm && (rx = curpm->op_pmregexp)) {
457 if (rx->subend && (s = rx->endp[0])) {
462 sv_setsv(sv,&sv_undef);
466 if (GvIO(last_in_gv)) {
467 sv_setiv(sv, (IV)IoLINES(GvIO(last_in_gv)));
472 sv_setiv(sv, (IV)STATUS_CURRENT);
473 #ifdef COMPLEX_STATUS
474 LvTARGOFF(sv) = statusvalue;
475 LvTARGLEN(sv) = statusvalue_vms;
479 s = IoTOP_NAME(GvIOp(defoutgv));
483 sv_setpv(sv,GvENAME(defoutgv));
488 s = IoFMT_NAME(GvIOp(defoutgv));
490 s = GvENAME(defoutgv);
495 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(defoutgv)));
498 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(defoutgv)));
501 sv_setiv(sv, (IV)IoPAGE(GvIOp(defoutgv)));
509 sv_setiv(sv, (IV)curcop->cop_arybase);
512 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(defoutgv)) & IOf_FLUSH) != 0 );
515 sv_setpvn(sv,ofs,ofslen);
518 sv_setpvn(sv,ors,orslen);
525 sv_setnv(sv, (double)((errno == EVMSERR) ? vaxc$errno : errno));
526 sv_setpv(sv, errno ? Strerror(errno) : "");
529 int saveerrno = errno;
530 sv_setnv(sv, (double)errno);
532 if (errno == errno_isOS2) sv_setpv(sv, os2error(Perl_rc));
535 sv_setpv(sv, errno ? Strerror(errno) : "");
539 SvNOK_on(sv); /* what a wonderful hack! */
542 sv_setiv(sv, (IV)uid);
545 sv_setiv(sv, (IV)euid);
548 sv_setiv(sv, (IV)gid);
549 sv_setpvf(sv, "%Vd", (IV)gid);
552 sv_setiv(sv, (IV)egid);
553 sv_setpvf(sv, "%Vd", (IV)egid);
557 Groups_t gary[NGROUPS];
558 i = getgroups(NGROUPS,gary);
560 sv_catpvf(sv, " %Vd", (IV)gary[i]);
563 SvIOK_on(sv); /* what a wonderful hack! */
574 magic_getuvar(sv, mg)
578 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
580 if (uf && uf->uf_val)
581 (*uf->uf_val)(uf->uf_index, sv);
599 #ifdef DYNAMIC_ENV_FETCH
600 /* We just undefd an environment var. Is a replacement */
601 /* waiting in the wings? */
604 if ((valp = hv_fetch(GvHVn(envgv), ptr, klen, FALSE)))
605 s = SvPV(*valp, len);
609 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32)
610 /* And you'll never guess what the dog had */
611 /* in its mouth... */
613 MgTAINTEDDIR_off(mg);
615 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
616 char pathbuf[256], eltbuf[256], *cp, *elt = s;
620 do { /* DCL$PATH may be a search list */
621 while (1) { /* as may dev portion of any element */
622 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
623 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
624 cando_by_name(S_IWUSR,0,elt) ) {
629 if ((cp = strchr(elt, ':')) != Nullch)
631 if (my_trnlnm(elt, eltbuf, j++))
637 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
640 if (s && klen == 4 && strEQ(ptr,"PATH")) {
641 char *strend = s + len;
645 s = delimcpy(tokenbuf, tokenbuf + sizeof tokenbuf,
648 if (i >= sizeof tokenbuf /* too long -- assume the worst */
650 || (Stat(tokenbuf, &st) == 0 && (st.st_mode & 2)) ) {
657 #endif /* neither OS2 nor AMIGAOS nor WIN32 */
663 magic_clearenv(sv,mg)
667 my_setenv(MgPV(mg,na),Nullch);
672 magic_set_all_env(sv,mg)
677 die("Can't make list assignment to %%ENV on this system");
681 magic_clear_all_env(sv,mg);
682 hv_iterinit((HV*)sv);
683 while (entry = hv_iternext((HV*)sv)) {
685 my_setenv(hv_iterkey(entry, &keylen),
686 SvPV(hv_iterval((HV*)sv, entry), na));
694 magic_clear_all_env(sv,mg)
699 die("Can't make list assignment to %%ENV on this system");
702 char *envv = GetEnvironmentStrings();
706 char *end = strchr(cur,'=');
707 if (end && end != cur) {
709 my_setenv(cur,Nullch);
711 cur += strlen(end+1)+1;
713 else if ((len = strlen(cur)))
716 FreeEnvironmentStrings(envv);
720 if (environ == origenviron)
721 New(901, environ, 1, char*);
723 for (i = 0; environ[i]; i++)
724 Safefree(environ[i]);
738 /* Are we fetching a signal entry? */
739 i = whichsig(MgPV(mg,na));
742 sv_setsv(sv,psig_ptr[i]);
744 Sighandler_t sigstate = rsignal_state(i);
746 /* cache state so we don't fetch it again */
747 if(sigstate == SIG_IGN)
748 sv_setpv(sv,"IGNORE");
750 sv_setsv(sv,&sv_undef);
751 psig_ptr[i] = SvREFCNT_inc(sv);
758 magic_clearsig(sv,mg)
763 /* Are we clearing a signal entry? */
764 i = whichsig(MgPV(mg,na));
767 SvREFCNT_dec(psig_ptr[i]);
771 SvREFCNT_dec(psig_name[i]);
789 if (strEQ(s,"__DIE__"))
791 else if (strEQ(s,"__WARN__"))
793 else if (strEQ(s,"__PARSE__"))
796 croak("No such hook: %s", s);
804 i = whichsig(s); /* ...no, a brick */
806 if (dowarn || strEQ(s,"ALARM"))
807 warn("No such signal: SIG%s", s);
810 SvREFCNT_dec(psig_name[i]);
811 SvREFCNT_dec(psig_ptr[i]);
812 psig_ptr[i] = SvREFCNT_inc(sv);
813 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
814 psig_name[i] = newSVpv(s, strlen(s));
815 SvREADONLY_on(psig_name[i]);
817 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
819 (void)rsignal(i, sighandler);
821 *svp = SvREFCNT_inc(sv);
824 s = SvPV_force(sv,na);
825 if (strEQ(s,"IGNORE")) {
827 (void)rsignal(i, SIG_IGN);
831 else if (strEQ(s,"DEFAULT") || !*s) {
833 (void)rsignal(i, SIG_DFL);
839 * We should warn if HINT_STRICT_REFS, but without
840 * access to a known hint bit in a known OP, we can't
841 * tell whether HINT_STRICT_REFS is in force or not.
843 if (!strchr(s,':') && !strchr(s,'\''))
844 sv_setpv(sv, form("main::%s", s));
846 (void)rsignal(i, sighandler);
848 *svp = SvREFCNT_inc(sv);
865 magic_setamagic(sv,mg)
869 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
874 #endif /* OVERLOAD */
877 magic_setnkeys(sv,mg)
882 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
883 LvTARG(sv) = Nullsv; /* Don't allow a ref to reassign this. */
889 magic_methpack(sv,mg,meth)
903 PUSHs(sv_2mortal(newSVpv(mg->mg_ptr, mg->mg_len)));
904 else if (mg->mg_len == HEf_SVKEY)
905 PUSHs((SV*)mg->mg_ptr);
907 else if (mg->mg_type == 'p')
908 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
911 if (perl_call_method(meth, G_SCALAR))
912 sv_setsv(sv, *stack_sp--);
924 magic_methpack(sv,mg,"FETCH");
926 mg->mg_flags |= MGf_GSKIP;
942 PUSHs(sv_2mortal(newSVpv(mg->mg_ptr, mg->mg_len)));
943 else if (mg->mg_len == HEf_SVKEY)
944 PUSHs((SV*)mg->mg_ptr);
946 else if (mg->mg_type == 'p')
947 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
951 perl_call_method("STORE", G_SCALAR|G_DISCARD);
957 magic_clearpack(sv,mg)
961 return magic_methpack(sv,mg,"DELETE");
964 int magic_wipepack(sv,mg)
974 perl_call_method("CLEAR", G_SCALAR|G_DISCARD);
980 magic_nextpack(sv,mg,key)
986 char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
997 if (perl_call_method(meth, G_SCALAR))
998 sv_setsv(key, *stack_sp--);
1006 magic_existspack(sv,mg)
1010 return magic_methpack(sv,mg,"EXISTS");
1014 magic_setdbline(sv,mg)
1025 svp = av_fetch(GvAV(gv),
1026 atoi(MgPV(mg,na)), FALSE);
1027 if (svp && SvIOKp(*svp) && (o = (OP*)SvSTASH(*svp)))
1030 warn("Can't break at that line\n");
1035 magic_getarylen(sv,mg)
1039 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + curcop->cop_arybase);
1044 magic_setarylen(sv,mg)
1048 av_fill((AV*)mg->mg_obj, SvIV(sv) - curcop->cop_arybase);
1057 SV* lsv = LvTARG(sv);
1059 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1060 mg = mg_find(lsv, 'g');
1061 if (mg && mg->mg_len >= 0) {
1062 sv_setiv(sv, mg->mg_len + curcop->cop_arybase);
1075 SV* lsv = LvTARG(sv);
1081 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1082 mg = mg_find(lsv, 'g');
1086 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
1087 mg = mg_find(lsv, 'g');
1089 else if (!SvOK(sv)) {
1093 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1095 pos = SvIV(sv) - curcop->cop_arybase;
1104 mg->mg_flags &= ~MGf_MINMATCH;
1110 magic_getglob(sv,mg)
1114 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1116 gv_efullname3(sv,((GV*)sv), "*");
1120 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1125 magic_setglob(sv,mg)
1135 if (*s == '*' && s[1])
1137 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1142 GvGP(sv) = gp_ref(GvGP(gv));
1147 magic_setsubstr(sv,mg)
1152 char *tmps = SvPV(sv,len);
1153 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
1158 magic_gettaint(sv,mg)
1162 TAINT_IF((mg->mg_len & 1) ||
1163 (mg->mg_len & 2) && mg->mg_obj == sv); /* kludge */
1168 magic_settaint(sv,mg)
1173 if (localizing == 1)
1190 do_vecset(sv); /* XXX slurp this routine */
1195 magic_getdefelem(sv,mg)
1200 if (LvTARGLEN(sv)) {
1202 HV* hv = (HV*)LvTARG(sv);
1203 HE* he = hv_fetch_ent(hv, mg->mg_obj, FALSE, 0);
1208 AV* av = (AV*)LvTARG(sv);
1209 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1210 targ = AvARRAY(av)[LvTARGOFF(sv)];
1212 if (targ && targ != &sv_undef) {
1213 /* somebody else defined it for us */
1214 SvREFCNT_dec(LvTARG(sv));
1215 LvTARG(sv) = SvREFCNT_inc(targ);
1217 SvREFCNT_dec(mg->mg_obj);
1218 mg->mg_obj = Nullsv;
1219 mg->mg_flags &= ~MGf_REFCOUNTED;
1224 sv_setsv(sv, targ ? targ : &sv_undef);
1229 magic_setdefelem(sv,mg)
1236 sv_setsv(LvTARG(sv), sv);
1237 SvSETMAGIC(LvTARG(sv));
1243 magic_freedefelem(sv,mg)
1247 SvREFCNT_dec(LvTARG(sv));
1258 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, 'y')))
1261 HV* hv = (HV*)LvTARG(sv);
1262 HE* he = hv_fetch_ent(hv, mg->mg_obj, TRUE, 0);
1263 if (!he || (value = HeVAL(he)) == &sv_undef)
1264 croak(no_helem, SvPV(mg->mg_obj, na));
1267 AV* av = (AV*)LvTARG(sv);
1268 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1269 LvTARG(sv) = Nullsv; /* array can't be extended */
1271 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1272 if (!svp || (value = *svp) == &sv_undef)
1273 croak(no_aelem, (I32)LvTARGOFF(sv));
1276 (void)SvREFCNT_inc(value);
1277 SvREFCNT_dec(LvTARG(sv));
1280 SvREFCNT_dec(mg->mg_obj);
1281 mg->mg_obj = Nullsv;
1282 mg->mg_flags &= ~MGf_REFCOUNTED;
1286 magic_setmglob(sv,mg)
1300 sv_unmagic(sv, 'B');
1310 sv_unmagic(sv, 'f');
1316 magic_setuvar(sv,mg)
1320 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1322 if (uf && uf->uf_set)
1323 (*uf->uf_set)(uf->uf_index, sv);
1327 #ifdef USE_LOCALE_COLLATE
1329 magic_setcollxfrm(sv,mg)
1334 * René Descartes said "I think not."
1335 * and vanished with a faint plop.
1338 Safefree(mg->mg_ptr);
1344 #endif /* USE_LOCALE_COLLATE */
1354 switch (*mg->mg_ptr) {
1355 case '\001': /* ^A */
1356 sv_setsv(bodytarget, sv);
1358 case '\004': /* ^D */
1359 debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
1360 DEBUG_x(dump_all());
1362 case '\005': /* ^E */
1364 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1366 /* will anyone ever use this? */
1367 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1370 case '\006': /* ^F */
1371 maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1373 case '\010': /* ^H */
1374 hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1380 inplace = savepv(SvPV(sv,na));
1384 case '\017': /* ^O */
1388 osname = savepv(SvPV(sv,na));
1392 case '\020': /* ^P */
1393 perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1395 case '\024': /* ^T */
1397 basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1399 basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1402 case '\027': /* ^W */
1403 dowarn = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1407 if (localizing == 1)
1408 save_sptr((SV**)&last_in_gv);
1410 else if (SvOK(sv) && GvIO(last_in_gv))
1411 IoLINES(GvIOp(last_in_gv)) = (long)SvIV(sv);
1414 Safefree(IoTOP_NAME(GvIOp(defoutgv)));
1415 IoTOP_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1416 IoTOP_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1419 Safefree(IoFMT_NAME(GvIOp(defoutgv)));
1420 IoFMT_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1421 IoFMT_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1424 IoPAGE_LEN(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1427 IoLINES_LEFT(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1428 if (IoLINES_LEFT(GvIOp(defoutgv)) < 0L)
1429 IoLINES_LEFT(GvIOp(defoutgv)) = 0L;
1432 IoPAGE(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1436 IO *io = GvIOp(defoutgv);
1437 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1438 IoFLAGS(io) &= ~IOf_FLUSH;
1440 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1441 PerlIO *ofp = IoOFP(io);
1443 (void)PerlIO_flush(ofp);
1444 IoFLAGS(io) |= IOf_FLUSH;
1450 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1451 multiline = (i != 0);
1457 rs = SvREFCNT_inc(nrs);
1462 if (SvOK(sv) || SvGMAGICAL(sv))
1463 ors = savepv(SvPV(sv,orslen));
1472 ofs = savepv(SvPV(sv, ofslen));
1477 ofmt = savepv(SvPV(sv,na));
1480 compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1483 #ifdef COMPLEX_STATUS
1484 if (localizing == 2) {
1485 statusvalue = LvTARGOFF(sv);
1486 statusvalue_vms = LvTARGLEN(sv);
1490 #ifdef VMSISH_STATUS
1492 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1495 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1498 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv),
1499 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
1502 uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1504 delaymagic |= DM_RUID;
1505 break; /* don't do magic till later */
1508 (void)setruid((Uid_t)uid);
1511 (void)setreuid((Uid_t)uid, (Uid_t)-1);
1513 #ifdef HAS_SETRESUID
1514 (void)setresuid((Uid_t)uid, (Uid_t)-1, (Uid_t)-1);
1516 if (uid == euid) /* special case $< = $> */
1519 uid = (I32)getuid();
1520 croak("setruid() not implemented");
1525 uid = (I32)getuid();
1526 tainting |= (uid && (euid != uid || egid != gid));
1529 euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1531 delaymagic |= DM_EUID;
1532 break; /* don't do magic till later */
1535 (void)seteuid((Uid_t)euid);
1538 (void)setreuid((Uid_t)-1, (Uid_t)euid);
1540 #ifdef HAS_SETRESUID
1541 (void)setresuid((Uid_t)-1, (Uid_t)euid, (Uid_t)-1);
1543 if (euid == uid) /* special case $> = $< */
1546 euid = (I32)geteuid();
1547 croak("seteuid() not implemented");
1552 euid = (I32)geteuid();
1553 tainting |= (uid && (euid != uid || egid != gid));
1556 gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1558 delaymagic |= DM_RGID;
1559 break; /* don't do magic till later */
1562 (void)setrgid((Gid_t)gid);
1565 (void)setregid((Gid_t)gid, (Gid_t)-1);
1567 #ifdef HAS_SETRESGID
1568 (void)setresgid((Gid_t)gid, (Gid_t)-1, (Gid_t) 1);
1570 if (gid == egid) /* special case $( = $) */
1573 gid = (I32)getgid();
1574 croak("setrgid() not implemented");
1579 gid = (I32)getgid();
1580 tainting |= (uid && (euid != uid || egid != gid));
1583 #ifdef HAS_SETGROUPS
1585 char *p = SvPV(sv, na);
1586 Groups_t gary[NGROUPS];
1588 SET_NUMERIC_STANDARD();
1591 egid = I_V(atof(p));
1592 for (i = 0; i < NGROUPS; ++i) {
1593 while (*p && !isSPACE(*p))
1599 gary[i] = I_V(atof(p));
1602 (void)setgroups(i, gary);
1604 #else /* HAS_SETGROUPS */
1605 egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1606 #endif /* HAS_SETGROUPS */
1608 delaymagic |= DM_EGID;
1609 break; /* don't do magic till later */
1612 (void)setegid((Gid_t)egid);
1615 (void)setregid((Gid_t)-1, (Gid_t)egid);
1617 #ifdef HAS_SETRESGID
1618 (void)setresgid((Gid_t)-1, (Gid_t)egid, (Gid_t)-1);
1620 if (egid == gid) /* special case $) = $( */
1623 egid = (I32)getegid();
1624 croak("setegid() not implemented");
1629 egid = (I32)getegid();
1630 tainting |= (uid && (euid != uid || egid != gid));
1633 chopset = SvPV_force(sv,na);
1639 /* See if all the arguments are contiguous in memory */
1640 for (i = 1; i < origargc; i++) {
1641 if (origargv[i] == s + 1
1643 || origargv[i] == s + 2
1646 s += strlen(++s); /* this one is ok too */
1650 /* can grab env area too? */
1651 if (origenviron && (origenviron[0] == s + 1
1653 || (origenviron[0] == s + 9 && (s += 8))
1656 my_setenv("NoNe SuCh", Nullch);
1657 /* force copy of environment */
1658 for (i = 0; origenviron[i]; i++)
1659 if (origenviron[i] == s + 1)
1664 origalen = s - origargv[0];
1666 s = SvPV_force(sv,len);
1668 if (i >= origalen) {
1670 /* don't allow system to limit $0 seen by script */
1671 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
1672 Copy(s, origargv[0], i, char);
1677 Copy(s, origargv[0], i, char);
1680 while (++i < origalen)
1683 for (i = 1; i < origargc; i++)
1684 origargv[i] = Nullch;
1695 register char **sigv;
1697 for (sigv = sig_name+1; *sigv; sigv++)
1698 if (strEQ(sig,*sigv))
1699 return sig_num[sigv - sig_name];
1701 if (strEQ(sig,"CHLD"))
1705 if (strEQ(sig,"CLD"))
1714 unwind_handler_stack(p)
1717 U32 flags = *(U32*)p;
1720 savestack_ix -= 5; /* Unprotect save in progress. */
1721 /* cxstack_ix-- Not needed, die already unwound it. */
1723 SvREFCNT_dec(sig_sv);
1738 I32 o_save_i = savestack_ix, type;
1742 if (savestack_ix + 15 <= savestack_max)
1744 if (cxstack_ix < cxstack_max - 2)
1746 if (markstack_ptr < markstack_max - 2)
1748 if (retstack_ix < retstack_max - 2)
1750 if (scopestack_ix < scopestack_max - 3)
1753 if (flags & 2) { /* POPBLOCK may decrease cxstack too early. */
1754 cxstack_ix++; /* Protect from overwrite. */
1755 cx = &cxstack[cxstack_ix];
1756 type = cx->cx_type; /* Can be during partial write. */
1757 cx->cx_type = CXt_NULL; /* Make it safe for unwind. */
1760 die("Signal SIG%s received, but no signal handler set.\n",
1763 /* Max number of items pushed there is 3*n or 4. We cannot fix
1764 infinity, so we fix 4 (in fact 5): */
1766 savestack_ix += 5; /* Protect save in progress. */
1767 o_save_i = savestack_ix;
1768 SAVEDESTRUCTOR(unwind_handler_stack, (void*)&flags);
1771 markstack_ptr++; /* Protect mark. */
1774 retstack[retstack_ix] = NULL;
1778 /* sv_2cv is too complicated, try a simpler variant first: */
1779 if (!SvROK(psig_ptr[sig]) || !(cv = (CV*)SvRV(psig_ptr[sig]))
1780 || SvTYPE(cv) != SVt_PVCV)
1781 cv = sv_2cv(psig_ptr[sig],&st,&gv,TRUE);
1783 if (!cv || !CvROOT(cv)) {
1785 warn("SIG%s handler \"%s\" not defined.\n",
1786 sig_name[sig], GvENAME(gv) );
1790 oldstack = curstack;
1791 if (curstack != signalstack)
1792 AvFILL(signalstack) = 0;
1793 SWITCHSTACK(curstack, signalstack);
1795 if(psig_name[sig]) {
1796 sv = SvREFCNT_inc(psig_name[sig]);
1800 sv = sv_newmortal();
1801 sv_setpv(sv,sig_name[sig]);
1807 perl_call_sv((SV*)cv, G_DISCARD);
1809 SWITCHSTACK(signalstack, oldstack);
1811 savestack_ix -= 8; /* Unprotect save in progress. */
1813 cxstack[cxstack_ix].cx_type = type;
1824 op = myop; /* Apparently not needed... */
1826 Sv = tSv; /* Restore global temporaries. */