This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
SECURITY FIX: Buffer overflow in gv_fetchfile()
[perl5.git] / pp_hot.c
CommitLineData
a0d0e21e
LW
1/* pp_hot.c
2 *
9607fc9c 3 * Copyright (c) 1991-1997, Larry Wall
a0d0e21e
LW
4 *
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.
7 *
8 */
9
10/*
11 * Then he heard Merry change the note, and up went the Horn-cry of Buckland,
12 * shaking the air.
13 *
14 * Awake! Awake! Fear, Fire, Foes! Awake!
15 * Fire, Foes! Awake!
16 */
17
18#include "EXTERN.h"
19#include "perl.h"
20
21/* Hot code. */
22
23PP(pp_const)
24{
25 dSP;
26 XPUSHs(cSVOP->op_sv);
27 RETURN;
28}
29
30PP(pp_nextstate)
31{
32 curcop = (COP*)op;
33 TAINT_NOT; /* Each statement is presumed innocent */
34 stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
35 FREETMPS;
36 return NORMAL;
37}
38
39PP(pp_gvsv)
40{
41 dSP;
42 EXTEND(sp,1);
43 if (op->op_private & OPpLVAL_INTRO)
44 PUSHs(save_scalar(cGVOP->op_gv));
45 else
46 PUSHs(GvSV(cGVOP->op_gv));
47 RETURN;
48}
49
50PP(pp_null)
51{
52 return NORMAL;
53}
54
55PP(pp_pushmark)
56{
57 PUSHMARK(stack_sp);
58 return NORMAL;
59}
60
61PP(pp_stringify)
62{
63 dSP; dTARGET;
64 STRLEN len;
65 char *s;
66 s = SvPV(TOPs,len);
67 sv_setpvn(TARG,s,len);
68 SETTARG;
69 RETURN;
70}
71
72PP(pp_gv)
73{
74 dSP;
75 XPUSHs((SV*)cGVOP->op_gv);
76 RETURN;
77}
78
c07a80fd
PP
79PP(pp_gelem)
80{
81 GV *gv;
82 SV *sv;
83 SV *ref;
84 char *elem;
85 dSP;
86
87 sv = POPs;
88 elem = SvPV(sv, na);
89 gv = (GV*)POPs;
90 ref = Nullsv;
91 sv = Nullsv;
92 switch (elem ? *elem : '\0')
93 {
94 case 'A':
95 if (strEQ(elem, "ARRAY"))
96 ref = (SV*)GvAV(gv);
97 break;
98 case 'C':
99 if (strEQ(elem, "CODE"))
8ebc5c01 100 ref = (SV*)GvCVu(gv);
c07a80fd
PP
101 break;
102 case 'F':
36477c24 103 if (strEQ(elem, "FILEHANDLE")) /* XXX deprecate in 5.005 */
c07a80fd
PP
104 ref = (SV*)GvIOp(gv);
105 break;
106 case 'G':
107 if (strEQ(elem, "GLOB"))
108 ref = (SV*)gv;
109 break;
110 case 'H':
111 if (strEQ(elem, "HASH"))
112 ref = (SV*)GvHV(gv);
113 break;
36477c24
PP
114 case 'I':
115 if (strEQ(elem, "IO"))
116 ref = (SV*)GvIOp(gv);
117 break;
c07a80fd
PP
118 case 'N':
119 if (strEQ(elem, "NAME"))
120 sv = newSVpv(GvNAME(gv), GvNAMELEN(gv));
121 break;
122 case 'P':
123 if (strEQ(elem, "PACKAGE"))
124 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
125 break;
126 case 'S':
127 if (strEQ(elem, "SCALAR"))
128 ref = GvSV(gv);
129 break;
130 }
131 if (ref)
132 sv = newRV(ref);
133 if (sv)
134 sv_2mortal(sv);
135 else
136 sv = &sv_undef;
137 XPUSHs(sv);
138 RETURN;
139}
140
a0d0e21e
LW
141PP(pp_and)
142{
143 dSP;
144 if (!SvTRUE(TOPs))
145 RETURN;
146 else {
147 --SP;
148 RETURNOP(cLOGOP->op_other);
149 }
150}
151
152PP(pp_sassign)
153{
154 dSP; dPOPTOPssrl;
748a9306
LW
155 MAGIC *mg;
156
a0d0e21e
LW
157 if (op->op_private & OPpASSIGN_BACKWARDS) {
158 SV *temp;
159 temp = left; left = right; right = temp;
160 }
bbce6d69 161 if (tainting && tainted && !SvTAINTED(left))
a0d0e21e 162 TAINT_NOT;
54310121 163 SvSetMagicSV(right, left);
a0d0e21e
LW
164 SETs(right);
165 RETURN;
166}
167
168PP(pp_cond_expr)
169{
170 dSP;
171 if (SvTRUEx(POPs))
172 RETURNOP(cCONDOP->op_true);
173 else
174 RETURNOP(cCONDOP->op_false);
175}
176
177PP(pp_unstack)
178{
179 I32 oldsave;
180 TAINT_NOT; /* Each statement is presumed innocent */
181 stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
182 FREETMPS;
183 oldsave = scopestack[scopestack_ix - 1];
184 LEAVE_SCOPE(oldsave);
185 return NORMAL;
186}
187
a0d0e21e
LW
188PP(pp_concat)
189{
748a9306
LW
190 dSP; dATARGET; tryAMAGICbin(concat,opASSIGN);
191 {
192 dPOPTOPssrl;
a0d0e21e
LW
193 STRLEN len;
194 char *s;
195 if (TARG != left) {
196 s = SvPV(left,len);
197 sv_setpvn(TARG,s,len);
198 }
c07a80fd
PP
199 else if (SvGMAGICAL(TARG))
200 mg_get(TARG);
7a4c00b4 201 else if (!SvOK(TARG) && SvTYPE(TARG) <= SVt_PVMG) {
748a9306 202 sv_setpv(TARG, ""); /* Suppress warning. */
7a4c00b4 203 s = SvPV_force(TARG, len);
4633a7c4 204 }
a0d0e21e 205 s = SvPV(right,len);
68dc0745
PP
206 if (SvOK(TARG))
207 sv_catpvn(TARG,s,len);
208 else
209 sv_setpvn(TARG,s,len); /* suppress warning */
a0d0e21e
LW
210 SETTARG;
211 RETURN;
748a9306 212 }
a0d0e21e
LW
213}
214
215PP(pp_padsv)
216{
217 dSP; dTARGET;
218 XPUSHs(TARG);
4633a7c4
LW
219 if (op->op_flags & OPf_MOD) {
220 if (op->op_private & OPpLVAL_INTRO)
221 SAVECLEARSV(curpad[op->op_targ]);
5f05dabc 222 else if (op->op_private & OPpDEREF)
54310121 223 vivify_ref(curpad[op->op_targ], op->op_private & OPpDEREF);
4633a7c4 224 }
a0d0e21e
LW
225 RETURN;
226}
227
228PP(pp_readline)
229{
230 last_in_gv = (GV*)(*stack_sp--);
231 return do_readline();
232}
233
234PP(pp_eq)
235{
236 dSP; tryAMAGICbinSET(eq,0);
237 {
238 dPOPnv;
54310121 239 SETs(boolSV(TOPn == value));
a0d0e21e
LW
240 RETURN;
241 }
242}
243
244PP(pp_preinc)
245{
246 dSP;
68dc0745 247 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
44a8e56a 248 croak(no_modify);
55497cff
PP
249 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
250 SvIVX(TOPs) != IV_MAX)
251 {
252 ++SvIVX(TOPs);
253 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306
LW
254 }
255 else
256 sv_inc(TOPs);
a0d0e21e
LW
257 SvSETMAGIC(TOPs);
258 return NORMAL;
259}
260
261PP(pp_or)
262{
263 dSP;
264 if (SvTRUE(TOPs))
265 RETURN;
266 else {
267 --SP;
268 RETURNOP(cLOGOP->op_other);
269 }
270}
271
272PP(pp_add)
273{
274 dSP; dATARGET; tryAMAGICbin(add,opASSIGN);
275 {
7a4c00b4 276 dPOPTOPnnrl_ul;
a0d0e21e
LW
277 SETn( left + right );
278 RETURN;
279 }
280}
281
282PP(pp_aelemfast)
283{
284 dSP;
285 AV *av = GvAV((GV*)cSVOP->op_sv);
286 SV** svp = av_fetch(av, op->op_private, op->op_flags & OPf_MOD);
287 PUSHs(svp ? *svp : &sv_undef);
288 RETURN;
289}
290
291PP(pp_join)
292{
293 dSP; dMARK; dTARGET;
294 MARK++;
295 do_join(TARG, *MARK, MARK, SP);
296 SP = MARK;
297 SETs(TARG);
298 RETURN;
299}
300
301PP(pp_pushre)
302{
303 dSP;
44a8e56a
PP
304#ifdef DEBUGGING
305 /*
306 * We ass_u_me that LvTARGOFF() comes first, and that two STRLENs
307 * will be enough to hold an OP*.
308 */
309 SV* sv = sv_newmortal();
310 sv_upgrade(sv, SVt_PVLV);
311 LvTYPE(sv) = '/';
312 Copy(&op, &LvTARGOFF(sv), 1, OP*);
313 XPUSHs(sv);
314#else
a0d0e21e 315 XPUSHs((SV*)op);
44a8e56a 316#endif
a0d0e21e
LW
317 RETURN;
318}
319
320/* Oversized hot code. */
321
322PP(pp_print)
323{
324 dSP; dMARK; dORIGMARK;
325 GV *gv;
326 IO *io;
760ac839 327 register PerlIO *fp;
236988e4 328 MAGIC *mg;
a0d0e21e
LW
329
330 if (op->op_flags & OPf_STACKED)
331 gv = (GV*)*++MARK;
332 else
333 gv = defoutgv;
236988e4 334 if (SvMAGICAL(gv) && (mg = mg_find((SV*)gv, 'q'))) {
68dc0745
PP
335 if (MARK == ORIGMARK) {
336 EXTEND(SP, 1);
337 ++MARK;
338 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
339 ++SP;
340 }
341 PUSHMARK(MARK - 1);
236988e4 342 *MARK = mg->mg_obj;
68dc0745 343 PUTBACK;
236988e4
PP
344 ENTER;
345 perl_call_method("PRINT", G_SCALAR);
346 LEAVE;
347 SPAGAIN;
68dc0745
PP
348 MARK = ORIGMARK + 1;
349 *MARK = *SP;
350 SP = MARK;
236988e4
PP
351 RETURN;
352 }
a0d0e21e 353 if (!(io = GvIO(gv))) {
748a9306
LW
354 if (dowarn) {
355 SV* sv = sv_newmortal();
4f732624 356 gv_fullname3(sv, gv, Nullch);
748a9306
LW
357 warn("Filehandle %s never opened", SvPV(sv,na));
358 }
359
360 SETERRNO(EBADF,RMS$_IFI);
a0d0e21e
LW
361 goto just_say_no;
362 }
363 else if (!(fp = IoOFP(io))) {
364 if (dowarn) {
748a9306 365 SV* sv = sv_newmortal();
4f732624 366 gv_fullname3(sv, gv, Nullch);
a0d0e21e 367 if (IoIFP(io))
748a9306 368 warn("Filehandle %s opened only for input", SvPV(sv,na));
a0d0e21e 369 else
748a9306 370 warn("print on closed filehandle %s", SvPV(sv,na));
a0d0e21e 371 }
748a9306 372 SETERRNO(EBADF,IoIFP(io)?RMS$_FAC:RMS$_IFI);
a0d0e21e
LW
373 goto just_say_no;
374 }
375 else {
376 MARK++;
377 if (ofslen) {
378 while (MARK <= SP) {
379 if (!do_print(*MARK, fp))
380 break;
381 MARK++;
382 if (MARK <= SP) {
760ac839 383 if (PerlIO_write(fp, ofs, ofslen) == 0 || PerlIO_error(fp)) {
a0d0e21e
LW
384 MARK--;
385 break;
386 }
387 }
388 }
389 }
390 else {
391 while (MARK <= SP) {
392 if (!do_print(*MARK, fp))
393 break;
394 MARK++;
395 }
396 }
397 if (MARK <= SP)
398 goto just_say_no;
399 else {
400 if (orslen)
760ac839 401 if (PerlIO_write(fp, ors, orslen) == 0 || PerlIO_error(fp))
a0d0e21e
LW
402 goto just_say_no;
403
404 if (IoFLAGS(io) & IOf_FLUSH)
760ac839 405 if (PerlIO_flush(fp) == EOF)
a0d0e21e
LW
406 goto just_say_no;
407 }
408 }
409 SP = ORIGMARK;
410 PUSHs(&sv_yes);
411 RETURN;
412
413 just_say_no:
414 SP = ORIGMARK;
415 PUSHs(&sv_undef);
416 RETURN;
417}
418
419PP(pp_rv2av)
420{
421 dSP; dPOPss;
a0d0e21e
LW
422 AV *av;
423
424 if (SvROK(sv)) {
425 wasref:
426 av = (AV*)SvRV(sv);
427 if (SvTYPE(av) != SVt_PVAV)
428 DIE("Not an ARRAY reference");
429 if (op->op_private & OPpLVAL_INTRO)
430 av = (AV*)save_svref((SV**)sv);
431 if (op->op_flags & OPf_REF) {
432 PUSHs((SV*)av);
433 RETURN;
434 }
435 }
436 else {
437 if (SvTYPE(sv) == SVt_PVAV) {
438 av = (AV*)sv;
439 if (op->op_flags & OPf_REF) {
440 PUSHs((SV*)av);
441 RETURN;
442 }
443 }
444 else {
67955e0c
PP
445 GV *gv;
446
a0d0e21e 447 if (SvTYPE(sv) != SVt_PVGV) {
748a9306
LW
448 char *sym;
449
a0d0e21e
LW
450 if (SvGMAGICAL(sv)) {
451 mg_get(sv);
452 if (SvROK(sv))
453 goto wasref;
454 }
455 if (!SvOK(sv)) {
456 if (op->op_flags & OPf_REF ||
457 op->op_private & HINT_STRICT_REFS)
458 DIE(no_usym, "an ARRAY");
d83e6520
CS
459 if (dowarn)
460 warn(warn_uninit);
4633a7c4
LW
461 if (GIMME == G_ARRAY)
462 RETURN;
a0d0e21e
LW
463 RETPUSHUNDEF;
464 }
748a9306 465 sym = SvPV(sv,na);
a0d0e21e 466 if (op->op_private & HINT_STRICT_REFS)
748a9306 467 DIE(no_symref, sym, "an ARRAY");
67955e0c
PP
468 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVAV);
469 } else {
470 gv = (GV*)sv;
a0d0e21e 471 }
67955e0c 472 av = GvAVn(gv);
a0d0e21e 473 if (op->op_private & OPpLVAL_INTRO)
67955e0c 474 av = save_ary(gv);
a0d0e21e
LW
475 if (op->op_flags & OPf_REF) {
476 PUSHs((SV*)av);
477 RETURN;
478 }
479 }
480 }
481
482 if (GIMME == G_ARRAY) {
483 I32 maxarg = AvFILL(av) + 1;
484 EXTEND(SP, maxarg);
485 Copy(AvARRAY(av), SP+1, maxarg, SV*);
486 SP += maxarg;
487 }
488 else {
489 dTARGET;
490 I32 maxarg = AvFILL(av) + 1;
491 PUSHi(maxarg);
492 }
493 RETURN;
494}
495
496PP(pp_rv2hv)
497{
a0d0e21e 498 dSP; dTOPss;
a0d0e21e
LW
499 HV *hv;
500
501 if (SvROK(sv)) {
502 wasref:
503 hv = (HV*)SvRV(sv);
504 if (SvTYPE(hv) != SVt_PVHV)
505 DIE("Not a HASH reference");
506 if (op->op_private & OPpLVAL_INTRO)
507 hv = (HV*)save_svref((SV**)sv);
508 if (op->op_flags & OPf_REF) {
509 SETs((SV*)hv);
510 RETURN;
511 }
512 }
513 else {
514 if (SvTYPE(sv) == SVt_PVHV) {
515 hv = (HV*)sv;
516 if (op->op_flags & OPf_REF) {
517 SETs((SV*)hv);
518 RETURN;
519 }
520 }
521 else {
67955e0c
PP
522 GV *gv;
523
a0d0e21e 524 if (SvTYPE(sv) != SVt_PVGV) {
748a9306
LW
525 char *sym;
526
a0d0e21e
LW
527 if (SvGMAGICAL(sv)) {
528 mg_get(sv);
529 if (SvROK(sv))
530 goto wasref;
531 }
532 if (!SvOK(sv)) {
533 if (op->op_flags & OPf_REF ||
534 op->op_private & HINT_STRICT_REFS)
535 DIE(no_usym, "a HASH");
d83e6520
CS
536 if (dowarn)
537 warn(warn_uninit);
4633a7c4
LW
538 if (GIMME == G_ARRAY) {
539 SP--;
540 RETURN;
541 }
a0d0e21e
LW
542 RETSETUNDEF;
543 }
748a9306 544 sym = SvPV(sv,na);
a0d0e21e 545 if (op->op_private & HINT_STRICT_REFS)
748a9306 546 DIE(no_symref, sym, "a HASH");
67955e0c
PP
547 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVHV);
548 } else {
549 gv = (GV*)sv;
a0d0e21e 550 }
67955e0c 551 hv = GvHVn(gv);
a0d0e21e 552 if (op->op_private & OPpLVAL_INTRO)
67955e0c 553 hv = save_hash(gv);
a0d0e21e
LW
554 if (op->op_flags & OPf_REF) {
555 SETs((SV*)hv);
556 RETURN;
557 }
558 }
559 }
560
561 if (GIMME == G_ARRAY) { /* array wanted */
562 *stack_sp = (SV*)hv;
563 return do_kv(ARGS);
564 }
565 else {
566 dTARGET;
567 if (HvFILL(hv)) {
9607fc9c 568 sprintf(buf, "%ld/%ld", (long)HvFILL(hv), (long)HvMAX(hv)+1);
a0d0e21e
LW
569 sv_setpv(TARG, buf);
570 }
571 else
572 sv_setiv(TARG, 0);
573 SETTARG;
574 RETURN;
575 }
576}
577
578PP(pp_aassign)
579{
580 dSP;
581 SV **lastlelem = stack_sp;
582 SV **lastrelem = stack_base + POPMARK;
583 SV **firstrelem = stack_base + POPMARK + 1;
584 SV **firstlelem = lastrelem + 1;
585
586 register SV **relem;
587 register SV **lelem;
588
589 register SV *sv;
590 register AV *ary;
591
54310121 592 I32 gimme;
a0d0e21e
LW
593 HV *hash;
594 I32 i;
595 int magic;
596
597 delaymagic = DM_DELAY; /* catch simultaneous items */
598
599 /* If there's a common identifier on both sides we have to take
600 * special care that assigning the identifier on the left doesn't
601 * clobber a value on the right that's used later in the list.
602 */
603 if (op->op_private & OPpASSIGN_COMMON) {
604 for (relem = firstrelem; relem <= lastrelem; relem++) {
605 /*SUPPRESS 560*/
606 if (sv = *relem)
607 *relem = sv_mortalcopy(sv);
608 }
609 }
610
611 relem = firstrelem;
612 lelem = firstlelem;
613 ary = Null(AV*);
614 hash = Null(HV*);
615 while (lelem <= lastlelem) {
bbce6d69 616 TAINT_NOT; /* Each item stands on its own, taintwise. */
a0d0e21e
LW
617 sv = *lelem++;
618 switch (SvTYPE(sv)) {
619 case SVt_PVAV:
620 ary = (AV*)sv;
748a9306 621 magic = SvMAGICAL(ary) != 0;
a0d0e21e
LW
622
623 av_clear(ary);
7e42bd57 624 av_extend(ary, lastrelem - relem);
a0d0e21e
LW
625 i = 0;
626 while (relem <= lastrelem) { /* gobble up all the rest */
627 sv = NEWSV(28,0);
628 assert(*relem);
629 sv_setsv(sv,*relem);
630 *(relem++) = sv;
631 (void)av_store(ary,i++,sv);
632 if (magic)
633 mg_set(sv);
bbce6d69 634 TAINT_NOT;
a0d0e21e
LW
635 }
636 break;
637 case SVt_PVHV: {
a0d0e21e
LW
638 SV *tmpstr;
639
640 hash = (HV*)sv;
748a9306 641 magic = SvMAGICAL(hash) != 0;
a0d0e21e
LW
642 hv_clear(hash);
643
644 while (relem < lastrelem) { /* gobble up all the rest */
645 STRLEN len;
4633a7c4 646 if (*relem)
a0d0e21e 647 sv = *(relem++);
4633a7c4 648 else
a0d0e21e 649 sv = &sv_no, relem++;
a0d0e21e
LW
650 tmpstr = NEWSV(29,0);
651 if (*relem)
652 sv_setsv(tmpstr,*relem); /* value */
653 *(relem++) = tmpstr;
760ac839 654 (void)hv_store_ent(hash,sv,tmpstr,0);
a0d0e21e
LW
655 if (magic)
656 mg_set(tmpstr);
bbce6d69 657 TAINT_NOT;
8e07c86e 658 }
760ac839
LW
659 if (relem == lastrelem)
660 warn("Odd number of elements in hash list");
a0d0e21e
LW
661 }
662 break;
663 default:
664 if (SvTHINKFIRST(sv)) {
665 if (SvREADONLY(sv) && curcop != &compiling) {
666 if (sv != &sv_undef && sv != &sv_yes && sv != &sv_no)
667 DIE(no_modify);
668 if (relem <= lastrelem)
669 relem++;
670 break;
671 }
672 if (SvROK(sv))
673 sv_unref(sv);
674 }
675 if (relem <= lastrelem) {
676 sv_setsv(sv, *relem);
677 *(relem++) = sv;
678 }
679 else
680 sv_setsv(sv, &sv_undef);
681 SvSETMAGIC(sv);
682 break;
683 }
684 }
685 if (delaymagic & ~DM_DELAY) {
686 if (delaymagic & DM_UID) {
687#ifdef HAS_SETRESUID
688 (void)setresuid(uid,euid,(Uid_t)-1);
56febc5e
AD
689#else
690# ifdef HAS_SETREUID
a0d0e21e 691 (void)setreuid(uid,euid);
56febc5e
AD
692# else
693# ifdef HAS_SETRUID
a0d0e21e
LW
694 if ((delaymagic & DM_UID) == DM_RUID) {
695 (void)setruid(uid);
748a9306 696 delaymagic &= ~DM_RUID;
a0d0e21e 697 }
56febc5e
AD
698# endif /* HAS_SETRUID */
699# ifdef HAS_SETEUID
a0d0e21e
LW
700 if ((delaymagic & DM_UID) == DM_EUID) {
701 (void)seteuid(uid);
748a9306 702 delaymagic &= ~DM_EUID;
a0d0e21e 703 }
56febc5e 704# endif /* HAS_SETEUID */
a0d0e21e
LW
705 if (delaymagic & DM_UID) {
706 if (uid != euid)
707 DIE("No setreuid available");
708 (void)setuid(uid);
709 }
56febc5e
AD
710# endif /* HAS_SETREUID */
711#endif /* HAS_SETRESUID */
a0d0e21e
LW
712 uid = (int)getuid();
713 euid = (int)geteuid();
714 }
715 if (delaymagic & DM_GID) {
716#ifdef HAS_SETRESGID
717 (void)setresgid(gid,egid,(Gid_t)-1);
56febc5e
AD
718#else
719# ifdef HAS_SETREGID
a0d0e21e 720 (void)setregid(gid,egid);
56febc5e
AD
721# else
722# ifdef HAS_SETRGID
a0d0e21e
LW
723 if ((delaymagic & DM_GID) == DM_RGID) {
724 (void)setrgid(gid);
748a9306 725 delaymagic &= ~DM_RGID;
a0d0e21e 726 }
56febc5e
AD
727# endif /* HAS_SETRGID */
728# ifdef HAS_SETEGID
a0d0e21e
LW
729 if ((delaymagic & DM_GID) == DM_EGID) {
730 (void)setegid(gid);
748a9306 731 delaymagic &= ~DM_EGID;
a0d0e21e 732 }
56febc5e 733# endif /* HAS_SETEGID */
a0d0e21e
LW
734 if (delaymagic & DM_GID) {
735 if (gid != egid)
736 DIE("No setregid available");
737 (void)setgid(gid);
738 }
56febc5e
AD
739# endif /* HAS_SETREGID */
740#endif /* HAS_SETRESGID */
a0d0e21e
LW
741 gid = (int)getgid();
742 egid = (int)getegid();
743 }
4633a7c4 744 tainting |= (uid && (euid != uid || egid != gid));
a0d0e21e
LW
745 }
746 delaymagic = 0;
54310121
PP
747
748 gimme = GIMME_V;
749 if (gimme == G_VOID)
750 SP = firstrelem - 1;
751 else if (gimme == G_SCALAR) {
752 dTARGET;
753 SP = firstrelem;
754 SETi(lastrelem - firstrelem + 1);
755 }
756 else {
a0d0e21e
LW
757 if (ary || hash)
758 SP = lastrelem;
759 else
760 SP = firstrelem + (lastlelem - firstlelem);
0c8c7a05 761 lelem = firstlelem + (relem - firstrelem);
5f05dabc 762 while (relem <= SP)
0c8c7a05 763 *relem++ = (lelem <= lastlelem) ? *lelem++ : &sv_undef;
a0d0e21e 764 }
54310121 765 RETURN;
a0d0e21e
LW
766}
767
768PP(pp_match)
769{
770 dSP; dTARG;
771 register PMOP *pm = cPMOP;
772 register char *t;
773 register char *s;
774 char *strend;
775 I32 global;
776 I32 safebase;
777 char *truebase;
778 register REGEXP *rx = pm->op_pmregexp;
779 I32 gimme = GIMME;
780 STRLEN len;
748a9306 781 I32 minmatch = 0;
4633a7c4 782 I32 oldsave = savestack_ix;
f86702cc 783 I32 update_minmatch = 1;
a0d0e21e
LW
784
785 if (op->op_flags & OPf_STACKED)
786 TARG = POPs;
787 else {
788 TARG = GvSV(defgv);
789 EXTEND(SP,1);
790 }
791 s = SvPV(TARG, len);
792 strend = s + len;
793 if (!s)
794 DIE("panic: do_match");
9212bbba 795 TAINT_NOT;
a0d0e21e
LW
796
797 if (pm->op_pmflags & PMf_USED) {
798 if (gimme == G_ARRAY)
799 RETURN;
800 RETPUSHNO;
801 }
802
803 if (!rx->prelen && curpm) {
804 pm = curpm;
805 rx = pm->op_pmregexp;
806 }
807 truebase = t = s;
808 if (global = pm->op_pmflags & PMf_GLOBAL) {
809 rx->startp[0] = 0;
810 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
811 MAGIC* mg = mg_find(TARG, 'g');
748a9306 812 if (mg && mg->mg_len >= 0) {
a0d0e21e 813 rx->endp[0] = rx->startp[0] = s + mg->mg_len;
748a9306 814 minmatch = (mg->mg_flags & MGf_MINMATCH);
f86702cc 815 update_minmatch = 0;
748a9306 816 }
a0d0e21e
LW
817 }
818 }
748a9306
LW
819 if (!rx->nparens && !global)
820 gimme = G_SCALAR; /* accidental array context? */
137443ea
PP
821 safebase = (((gimme == G_ARRAY) || global || !rx->nparens)
822 && !sawampersand);
a0d0e21e
LW
823 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
824 SAVEINT(multiline);
825 multiline = pm->op_pmflags & PMf_MULTILINE;
826 }
827
828play_it_again:
829 if (global && rx->startp[0]) {
830 t = s = rx->endp[0];
51aa1f50 831 if ((s + rx->minlen) > strend)
a0d0e21e 832 goto nope;
f86702cc
PP
833 if (update_minmatch++)
834 minmatch = (s == rx->startp[0]);
a0d0e21e
LW
835 }
836 if (pm->op_pmshort) {
837 if (pm->op_pmflags & PMf_SCANFIRST) {
838 if (SvSCREAM(TARG)) {
839 if (screamfirst[BmRARE(pm->op_pmshort)] < 0)
840 goto nope;
841 else if (!(s = screaminstr(TARG, pm->op_pmshort)))
842 goto nope;
843 else if (pm->op_pmflags & PMf_ALL)
844 goto yup;
845 }
846 else if (!(s = fbm_instr((unsigned char*)s,
847 (unsigned char*)strend, pm->op_pmshort)))
848 goto nope;
849 else if (pm->op_pmflags & PMf_ALL)
850 goto yup;
851 if (s && rx->regback >= 0) {
852 ++BmUSEFUL(pm->op_pmshort);
853 s -= rx->regback;
854 if (s < t)
855 s = t;
856 }
857 else
858 s = t;
859 }
860 else if (!multiline) {
bbce6d69
PP
861 if (*SvPVX(pm->op_pmshort) != *s
862 || (pm->op_pmslen > 1
36477c24 863 && memNE(SvPVX(pm->op_pmshort), s, pm->op_pmslen)))
bbce6d69 864 goto nope;
a0d0e21e
LW
865 }
866 if (!rx->naughty && --BmUSEFUL(pm->op_pmshort) < 0) {
867 SvREFCNT_dec(pm->op_pmshort);
868 pm->op_pmshort = Nullsv; /* opt is being useless */
869 }
870 }
e50aee73 871 if (pregexec(rx, s, strend, truebase, minmatch,
bbce6d69
PP
872 SvSCREAM(TARG) ? TARG : Nullsv, safebase))
873 {
a0d0e21e
LW
874 curpm = pm;
875 if (pm->op_pmflags & PMf_ONCE)
876 pm->op_pmflags |= PMf_USED;
877 goto gotcha;
878 }
879 else
880 goto ret_no;
881 /*NOTREACHED*/
882
883 gotcha:
9212bbba 884 TAINT_IF(rx->exec_tainted);
a0d0e21e
LW
885 if (gimme == G_ARRAY) {
886 I32 iters, i, len;
887
888 iters = rx->nparens;
889 if (global && !iters)
890 i = 1;
891 else
892 i = 0;
893 EXTEND(SP, iters + i);
bbce6d69 894 EXTEND_MORTAL(iters + i);
a0d0e21e
LW
895 for (i = !i; i <= iters; i++) {
896 PUSHs(sv_newmortal());
897 /*SUPPRESS 560*/
898 if ((s = rx->startp[i]) && rx->endp[i] ) {
899 len = rx->endp[i] - s;
900 sv_setpvn(*SP, s, len);
901 }
902 }
903 if (global) {
904 truebase = rx->subbeg;
5f05dabc 905 strend = rx->subend;
a0d0e21e
LW
906 if (rx->startp[0] && rx->startp[0] == rx->endp[0])
907 ++rx->endp[0];
908 goto play_it_again;
909 }
4633a7c4 910 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
911 RETURN;
912 }
913 else {
914 if (global) {
915 MAGIC* mg = 0;
916 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
917 mg = mg_find(TARG, 'g');
918 if (!mg) {
919 sv_magic(TARG, (SV*)0, 'g', Nullch, 0);
920 mg = mg_find(TARG, 'g');
921 }
748a9306 922 if (rx->startp[0]) {
5f05dabc 923 mg->mg_len = rx->endp[0] - rx->subbeg;
748a9306
LW
924 if (rx->startp[0] == rx->endp[0])
925 mg->mg_flags |= MGf_MINMATCH;
926 else
927 mg->mg_flags &= ~MGf_MINMATCH;
928 }
a0d0e21e 929 }
4633a7c4 930 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
931 RETPUSHYES;
932 }
933
934yup:
9212bbba 935 TAINT_IF(rx->exec_tainted);
a0d0e21e
LW
936 ++BmUSEFUL(pm->op_pmshort);
937 curpm = pm;
938 if (pm->op_pmflags & PMf_ONCE)
939 pm->op_pmflags |= PMf_USED;
5f05dabc
PP
940 Safefree(rx->subbase);
941 rx->subbase = Nullch;
a0d0e21e
LW
942 if (global) {
943 rx->subbeg = truebase;
944 rx->subend = strend;
945 rx->startp[0] = s;
946 rx->endp[0] = s + SvCUR(pm->op_pmshort);
947 goto gotcha;
948 }
949 if (sawampersand) {
950 char *tmps;
951
a0d0e21e
LW
952 tmps = rx->subbase = savepvn(t, strend-t);
953 rx->subbeg = tmps;
954 rx->subend = tmps + (strend-t);
955 tmps = rx->startp[0] = tmps + (s - t);
956 rx->endp[0] = tmps + SvCUR(pm->op_pmshort);
957 }
4633a7c4 958 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
959 RETPUSHYES;
960
961nope:
962 if (pm->op_pmshort)
963 ++BmUSEFUL(pm->op_pmshort);
964
965ret_no:
4633a7c4 966 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
967 if (gimme == G_ARRAY)
968 RETURN;
969 RETPUSHNO;
970}
971
972OP *
973do_readline()
974{
975 dSP; dTARGETSTACKED;
976 register SV *sv;
977 STRLEN tmplen = 0;
978 STRLEN offset;
760ac839 979 PerlIO *fp;
a0d0e21e
LW
980 register IO *io = GvIO(last_in_gv);
981 register I32 type = op->op_type;
54310121 982 I32 gimme = GIMME_V;
e79b0511 983 MAGIC *mg;
a0d0e21e 984
e79b0511
PP
985 if (SvMAGICAL(last_in_gv) && (mg = mg_find((SV*)last_in_gv, 'q'))) {
986 PUSHMARK(SP);
987 XPUSHs(mg->mg_obj);
988 PUTBACK;
989 ENTER;
54310121 990 perl_call_method("READLINE", gimme);
e79b0511
PP
991 LEAVE;
992 SPAGAIN;
54310121
PP
993 if (gimme == G_SCALAR)
994 SvSetMagicSV_nosteal(TARG, TOPs);
e79b0511
PP
995 RETURN;
996 }
a0d0e21e
LW
997 fp = Nullfp;
998 if (io) {
999 fp = IoIFP(io);
1000 if (!fp) {
1001 if (IoFLAGS(io) & IOf_ARGV) {
1002 if (IoFLAGS(io) & IOf_START) {
1003 IoFLAGS(io) &= ~IOf_START;
1004 IoLINES(io) = 0;
1005 if (av_len(GvAVn(last_in_gv)) < 0) {
1006 SV *tmpstr = newSVpv("-", 1); /* assume stdin */
1007 av_push(GvAVn(last_in_gv), tmpstr);
1008 }
1009 }
1010 fp = nextargv(last_in_gv);
1011 if (!fp) { /* Note: fp != IoIFP(io) */
1012 (void)do_close(last_in_gv, FALSE); /* now it does*/
1013 IoFLAGS(io) |= IOf_START;
1014 }
1015 }
1016 else if (type == OP_GLOB) {
1017 SV *tmpcmd = NEWSV(55, 0);
1018 SV *tmpglob = POPs;
1019 ENTER;
1020 SAVEFREESV(tmpcmd);
1021#ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
1022 /* since spawning off a process is a real performance hit */
1023 {
1024#include <descrip.h>
1025#include <lib$routines.h>
1026#include <nam.h>
1027#include <rmsdef.h>
1028 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
1029 char vmsspec[NAM$C_MAXRSS+1];
1030 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
1031 char tmpfnam[L_tmpnam] = "SYS$SCRATCH:";
1032 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
760ac839 1033 PerlIO *tmpfp;
a0d0e21e
LW
1034 STRLEN i;
1035 struct dsc$descriptor_s wilddsc
1036 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
1037 struct dsc$descriptor_vs rsdsc
1038 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
1039 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
1040
1041 /* We could find out if there's an explicit dev/dir or version
1042 by peeking into lib$find_file's internal context at
1043 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
1044 but that's unsupported, so I don't want to do it now and
1045 have it bite someone in the future. */
1046 strcat(tmpfnam,tmpnam(NULL));
1047 cp = SvPV(tmpglob,i);
1048 for (; i; i--) {
1049 if (cp[i] == ';') hasver = 1;
1050 if (cp[i] == '.') {
1051 if (sts) hasver = 1;
1052 else sts = 1;
1053 }
1054 if (cp[i] == '/') {
1055 hasdir = isunix = 1;
1056 break;
748a9306 1057 }
a0d0e21e
LW
1058 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
1059 hasdir = 1;
1060 break;
1061 }
1062 }
760ac839 1063 if ((tmpfp = PerlIO_open(tmpfnam,"w+","fop=dlt")) != NULL) {
a0d0e21e
LW
1064 ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
1065 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
1066 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
1067 &dfltdsc,NULL,NULL,NULL))&1)) {
1068 end = rstr + (unsigned long int) *rslt;
1069 if (!hasver) while (*end != ';') end--;
1070 *(end++) = '\n'; *end = '\0';
1071 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
1072 if (hasdir) {
f86702cc 1073 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
a0d0e21e
LW
1074 begin = rstr;
1075 }
1076 else {
1077 begin = end;
1078 while (*(--begin) != ']' && *begin != '>') ;
1079 ++begin;
1080 }
760ac839 1081 ok = (PerlIO_puts(tmpfp,begin) != EOF);
a0d0e21e
LW
1082 }
1083 if (cxt) (void)lib$find_file_end(&cxt);
748a9306
LW
1084 if (ok && sts != RMS$_NMF &&
1085 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
a0d0e21e 1086 if (!ok) {
c07a80fd
PP
1087 if (!(sts & 1)) {
1088 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
1089 }
760ac839 1090 PerlIO_close(tmpfp);
a0d0e21e
LW
1091 fp = NULL;
1092 }
1093 else {
760ac839 1094 PerlIO_rewind(tmpfp);
a0d0e21e
LW
1095 IoTYPE(io) = '<';
1096 IoIFP(io) = fp = tmpfp;
1e422769 1097 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
a0d0e21e
LW
1098 }
1099 }
1100 }
1101#else /* !VMS */
1102#ifdef DOSISH
67955e0c
PP
1103#ifdef OS2
1104 sv_setpv(tmpcmd, "for a in ");
1105 sv_catsv(tmpcmd, tmpglob);
1106 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
1107#else
a0d0e21e
LW
1108 sv_setpv(tmpcmd, "perlglob ");
1109 sv_catsv(tmpcmd, tmpglob);
1110 sv_catpv(tmpcmd, " |");
67955e0c
PP
1111#endif /* !OS2 */
1112#else /* !DOSISH */
1113#if defined(CSH)
a0d0e21e
LW
1114 sv_setpvn(tmpcmd, cshname, cshlen);
1115 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
1116 sv_catsv(tmpcmd, tmpglob);
16d20bd9 1117 sv_catpv(tmpcmd, "' 2>/dev/null |");
a0d0e21e
LW
1118#else
1119 sv_setpv(tmpcmd, "echo ");
1120 sv_catsv(tmpcmd, tmpglob);
1121#if 'z' - 'a' == 25
1122 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
1123#else
1124 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
1125#endif
1126#endif /* !CSH */
67955e0c 1127#endif /* !DOSISH */
c07a80fd
PP
1128 (void)do_open(last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
1129 FALSE, 0, 0, Nullfp);
a0d0e21e
LW
1130 fp = IoIFP(io);
1131#endif /* !VMS */
1132 LEAVE;
1133 }
1134 }
1135 else if (type == OP_GLOB)
1136 SP--;
1137 }
1138 if (!fp) {
4633a7c4 1139 if (dowarn && io && !(IoFLAGS(io) & IOf_START))
a0d0e21e 1140 warn("Read on closed filehandle <%s>", GvENAME(last_in_gv));
54310121 1141 if (gimme == G_SCALAR) {
a0d0e21e
LW
1142 (void)SvOK_off(TARG);
1143 PUSHTARG;
1144 }
1145 RETURN;
1146 }
54310121 1147 if (gimme == G_SCALAR) {
a0d0e21e 1148 sv = TARG;
9607fc9c
PP
1149 if (SvROK(sv))
1150 sv_unref(sv);
a0d0e21e
LW
1151 (void)SvUPGRADE(sv, SVt_PV);
1152 tmplen = SvLEN(sv); /* remember if already alloced */
1153 if (!tmplen)
1154 Sv_Grow(sv, 80); /* try short-buffering it */
1155 if (type == OP_RCATLINE)
1156 offset = SvCUR(sv);
1157 else
1158 offset = 0;
1159 }
54310121
PP
1160 else {
1161 sv = sv_2mortal(NEWSV(57, 80));
1162 offset = 0;
1163 }
a0d0e21e
LW
1164 for (;;) {
1165 if (!sv_gets(sv, fp, offset)) {
760ac839 1166 PerlIO_clearerr(fp);
a0d0e21e
LW
1167 if (IoFLAGS(io) & IOf_ARGV) {
1168 fp = nextargv(last_in_gv);
1169 if (fp)
1170 continue;
1171 (void)do_close(last_in_gv, FALSE);
1172 IoFLAGS(io) |= IOf_START;
1173 }
1174 else if (type == OP_GLOB) {
5cd24f17
PP
1175 if (do_close(last_in_gv, FALSE) & ~0xFF)
1176 warn("internal error: glob failed");
a0d0e21e 1177 }
54310121 1178 if (gimme == G_SCALAR) {
a0d0e21e
LW
1179 (void)SvOK_off(TARG);
1180 PUSHTARG;
1181 }
1182 RETURN;
1183 }
bbce6d69
PP
1184 /* This should not be marked tainted if the fp is marked clean */
1185 if (!(IoFLAGS(io) & IOf_UNTAINT)) {
1186 TAINT;
1187 SvTAINTED_on(sv);
1188 }
a0d0e21e 1189 IoLINES(io)++;
71be2cbc 1190 SvSETMAGIC(sv);
a0d0e21e 1191 XPUSHs(sv);
a0d0e21e
LW
1192 if (type == OP_GLOB) {
1193 char *tmps;
1194
c07a80fd
PP
1195 if (SvCUR(sv) > 0 && SvCUR(rs) > 0) {
1196 tmps = SvEND(sv) - 1;
1197 if (*tmps == *SvPVX(rs)) {
1198 *tmps = '\0';
1199 SvCUR(sv)--;
1200 }
1201 }
a0d0e21e
LW
1202 for (tmps = SvPVX(sv); *tmps; tmps++)
1203 if (!isALPHA(*tmps) && !isDIGIT(*tmps) &&
1204 strchr("$&*(){}[]'\";\\|?<>~`", *tmps))
1205 break;
1206 if (*tmps && Stat(SvPVX(sv), &statbuf) < 0) {
1207 (void)POPs; /* Unmatched wildcard? Chuck it... */
1208 continue;
1209 }
1210 }
54310121 1211 if (gimme == G_ARRAY) {
a0d0e21e
LW
1212 if (SvLEN(sv) - SvCUR(sv) > 20) {
1213 SvLEN_set(sv, SvCUR(sv)+1);
1214 Renew(SvPVX(sv), SvLEN(sv), char);
1215 }
1216 sv = sv_2mortal(NEWSV(58, 80));
1217 continue;
1218 }
54310121 1219 else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
a0d0e21e
LW
1220 /* try to reclaim a bit of scalar space (only on 1st alloc) */
1221 if (SvCUR(sv) < 60)
1222 SvLEN_set(sv, 80);
1223 else
1224 SvLEN_set(sv, SvCUR(sv)+40); /* allow some slop */
1225 Renew(SvPVX(sv), SvLEN(sv), char);
1226 }
1227 RETURN;
1228 }
1229}
1230
1231PP(pp_enter)
1232{
1233 dSP;
1234 register CONTEXT *cx;
54310121 1235 I32 gimme = OP_GIMME(op, -1);
a0d0e21e 1236
54310121
PP
1237 if (gimme == -1) {
1238 if (cxstack_ix >= 0)
1239 gimme = cxstack[cxstack_ix].blk_gimme;
1240 else
1241 gimme = G_SCALAR;
1242 }
a0d0e21e
LW
1243
1244 ENTER;
1245
1246 SAVETMPS;
1247 PUSHBLOCK(cx, CXt_BLOCK, sp);
1248
1249 RETURN;
1250}
1251
1252PP(pp_helem)
1253{
1254 dSP;
760ac839 1255 HE* he;
a0d0e21e 1256 SV *keysv = POPs;
a0d0e21e 1257 HV *hv = (HV*)POPs;
68dc0745
PP
1258 U32 lval = op->op_flags & OPf_MOD;
1259 U32 defer = op->op_private & OPpLVAL_DEFER;
a0d0e21e
LW
1260
1261 if (SvTYPE(hv) != SVt_PVHV)
1262 RETPUSHUNDEF;
68dc0745 1263 he = hv_fetch_ent(hv, keysv, lval && !defer, 0);
a0d0e21e 1264 if (lval) {
68dc0745
PP
1265 if (!he || HeVAL(he) == &sv_undef) {
1266 SV* lv;
1267 SV* key2;
1268 if (!defer)
1269 DIE(no_helem, SvPV(keysv, na));
1270 lv = sv_newmortal();
1271 sv_upgrade(lv, SVt_PVLV);
1272 LvTYPE(lv) = 'y';
1273 sv_magic(lv, key2 = newSVsv(keysv), 'y', Nullch, 0);
1274 SvREFCNT_dec(key2); /* sv_magic() increments refcount */
1275 LvTARG(lv) = SvREFCNT_inc(hv);
1276 LvTARGLEN(lv) = 1;
1277 PUSHs(lv);
1278 RETURN;
1279 }
5f05dabc
PP
1280 if (op->op_private & OPpLVAL_INTRO) {
1281 if (HvNAME(hv) && isGV(HeVAL(he)))
1282 save_gp((GV*)HeVAL(he), !(op->op_flags & OPf_SPECIAL));
1283 else
1284 save_svref(&HeVAL(he));
1285 }
1286 else if (op->op_private & OPpDEREF)
68dc0745 1287 vivify_ref(HeVAL(he), op->op_private & OPpDEREF);
a0d0e21e 1288 }
760ac839 1289 PUSHs(he ? HeVAL(he) : &sv_undef);
a0d0e21e
LW
1290 RETURN;
1291}
1292
1293PP(pp_leave)
1294{
1295 dSP;
1296 register CONTEXT *cx;
1297 register SV **mark;
1298 SV **newsp;
1299 PMOP *newpm;
1300 I32 gimme;
1301
1302 if (op->op_flags & OPf_SPECIAL) {
1303 cx = &cxstack[cxstack_ix];
1304 cx->blk_oldpm = curpm; /* fake block should preserve $1 et al */
1305 }
1306
1307 POPBLOCK(cx,newpm);
1308
54310121
PP
1309 gimme = OP_GIMME(op, -1);
1310 if (gimme == -1) {
1311 if (cxstack_ix >= 0)
1312 gimme = cxstack[cxstack_ix].blk_gimme;
1313 else
1314 gimme = G_SCALAR;
1315 }
a0d0e21e 1316
54310121
PP
1317 if (gimme == G_VOID)
1318 SP = newsp;
1319 else if (gimme == G_SCALAR) {
1320 MARK = newsp + 1;
1321 if (MARK <= SP)
1322 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
1323 *MARK = TOPs;
1324 else
1325 *MARK = sv_mortalcopy(TOPs);
a0d0e21e 1326 else {
54310121
PP
1327 MEXTEND(mark,0);
1328 *MARK = &sv_undef;
a0d0e21e 1329 }
54310121 1330 SP = MARK;
a0d0e21e 1331 }
54310121 1332 else if (gimme == G_ARRAY) {
a0d0e21e
LW
1333 for (mark = newsp + 1; mark <= SP; mark++)
1334 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP)))
1335 *mark = sv_mortalcopy(*mark);
1336 /* in case LEAVE wipes old return values */
1337 }
1338 curpm = newpm; /* Don't pop $1 et al till now */
1339
1340 LEAVE;
1341
1342 RETURN;
1343}
1344
1345PP(pp_iter)
1346{
1347 dSP;
1348 register CONTEXT *cx;
5f05dabc 1349 SV* sv;
4633a7c4 1350 AV* av;
a0d0e21e
LW
1351
1352 EXTEND(sp, 1);
1353 cx = &cxstack[cxstack_ix];
1354 if (cx->cx_type != CXt_LOOP)
1355 DIE("panic: pp_iter");
1356
71be2cbc
PP
1357 av = cx->blk_loop.iterary;
1358 if (cx->blk_loop.iterix >= (av == curstack ? cx->blk_oldsp : AvFILL(av)))
a0d0e21e
LW
1359 RETPUSHNO;
1360
7a4c00b4
PP
1361 SvREFCNT_dec(*cx->blk_loop.itervar);
1362
5f05dabc 1363 if (sv = AvARRAY(av)[++cx->blk_loop.iterix])
a0d0e21e 1364 SvTEMP_off(sv);
a0d0e21e 1365 else
5f05dabc
PP
1366 sv = &sv_undef;
1367 if (av != curstack && SvIMMORTAL(sv)) {
1368 SV *lv = cx->blk_loop.iterlval;
71be2cbc
PP
1369 if (lv && SvREFCNT(lv) > 1) {
1370 SvREFCNT_dec(lv);
1371 lv = Nullsv;
1372 }
5f05dabc
PP
1373 if (lv)
1374 SvREFCNT_dec(LvTARG(lv));
1375 else {
68dc0745 1376 lv = cx->blk_loop.iterlval = NEWSV(26, 0);
5f05dabc 1377 sv_upgrade(lv, SVt_PVLV);
5f05dabc 1378 LvTYPE(lv) = 'y';
68dc0745 1379 sv_magic(lv, Nullsv, 'y', Nullch, 0);
5f05dabc
PP
1380 }
1381 LvTARG(lv) = SvREFCNT_inc(av);
1382 LvTARGOFF(lv) = cx->blk_loop.iterix;
68dc0745 1383 LvTARGLEN(lv) = -1;
5f05dabc
PP
1384 sv = (SV*)lv;
1385 }
7a4c00b4
PP
1386
1387 *cx->blk_loop.itervar = SvREFCNT_inc(sv);
a0d0e21e
LW
1388 RETPUSHYES;
1389}
1390
1391PP(pp_subst)
1392{
1393 dSP; dTARG;
1394 register PMOP *pm = cPMOP;
1395 PMOP *rpm = pm;
1396 register SV *dstr;
1397 register char *s;
1398 char *strend;
1399 register char *m;
1400 char *c;
1401 register char *d;
1402 STRLEN clen;
1403 I32 iters = 0;
1404 I32 maxiters;
1405 register I32 i;
1406 bool once;
71be2cbc 1407 bool rxtainted;
a0d0e21e
LW
1408 char *orig;
1409 I32 safebase;
1410 register REGEXP *rx = pm->op_pmregexp;
1411 STRLEN len;
1412 int force_on_match = 0;
4633a7c4 1413 I32 oldsave = savestack_ix;
a0d0e21e 1414
5cd24f17
PP
1415 /* known replacement string? */
1416 dstr = (pm->op_pmflags & PMf_CONST) ? POPs : Nullsv;
a0d0e21e
LW
1417 if (op->op_flags & OPf_STACKED)
1418 TARG = POPs;
1419 else {
1420 TARG = GvSV(defgv);
1421 EXTEND(SP,1);
1422 }
68dc0745
PP
1423 if (SvREADONLY(TARG)
1424 || (SvTYPE(TARG) > SVt_PVLV
1425 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG))))
1426 croak(no_modify);
a0d0e21e 1427 s = SvPV(TARG, len);
68dc0745 1428 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV)
a0d0e21e 1429 force_on_match = 1;
9212bbba 1430 TAINT_NOT;
a0d0e21e
LW
1431
1432 force_it:
1433 if (!pm || !s)
1434 DIE("panic: do_subst");
1435
1436 strend = s + len;
1437 maxiters = (strend - s) + 10;
1438
1439 if (!rx->prelen && curpm) {
1440 pm = curpm;
1441 rx = pm->op_pmregexp;
1442 }
5f05dabc 1443 safebase = (!rx->nparens && !sawampersand);
a0d0e21e
LW
1444 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
1445 SAVEINT(multiline);
1446 multiline = pm->op_pmflags & PMf_MULTILINE;
1447 }
1448 orig = m = s;
1449 if (pm->op_pmshort) {
1450 if (pm->op_pmflags & PMf_SCANFIRST) {
1451 if (SvSCREAM(TARG)) {
1452 if (screamfirst[BmRARE(pm->op_pmshort)] < 0)
1453 goto nope;
1454 else if (!(s = screaminstr(TARG, pm->op_pmshort)))
1455 goto nope;
1456 }
1457 else if (!(s = fbm_instr((unsigned char*)s, (unsigned char*)strend,
1458 pm->op_pmshort)))
1459 goto nope;
1460 if (s && rx->regback >= 0) {
1461 ++BmUSEFUL(pm->op_pmshort);
1462 s -= rx->regback;
1463 if (s < m)
1464 s = m;
1465 }
1466 else
1467 s = m;
1468 }
1469 else if (!multiline) {
bbce6d69
PP
1470 if (*SvPVX(pm->op_pmshort) != *s
1471 || (pm->op_pmslen > 1
36477c24 1472 && memNE(SvPVX(pm->op_pmshort), s, pm->op_pmslen)))
bbce6d69 1473 goto nope;
a0d0e21e
LW
1474 }
1475 if (!rx->naughty && --BmUSEFUL(pm->op_pmshort) < 0) {
1476 SvREFCNT_dec(pm->op_pmshort);
1477 pm->op_pmshort = Nullsv; /* opt is being useless */
1478 }
1479 }
71be2cbc
PP
1480
1481 /* only replace once? */
a0d0e21e 1482 once = !(rpm->op_pmflags & PMf_GLOBAL);
71be2cbc
PP
1483
1484 /* known replacement string? */
5cd24f17 1485 c = dstr ? SvPV(dstr, clen) : Nullch;
71be2cbc
PP
1486
1487 /* can do inplace substitution? */
137443ea 1488 if (c && clen <= rx->minlen && safebase) {
71be2cbc
PP
1489 if (! pregexec(rx, s, strend, orig, 0,
1490 SvSCREAM(TARG) ? TARG : Nullsv, safebase)) {
1491 PUSHs(&sv_no);
1492 LEAVE_SCOPE(oldsave);
1493 RETURN;
1494 }
1495 if (force_on_match) {
1496 force_on_match = 0;
1497 s = SvPV_force(TARG, len);
1498 goto force_it;
1499 }
71be2cbc
PP
1500 d = s;
1501 curpm = pm;
1502 SvSCREAM_off(TARG); /* disable possible screamer */
1503 if (once) {
1504 rxtainted = rx->exec_tainted;
1505 m = rx->startp[0];
1506 d = rx->endp[0];
1507 s = orig;
1508 if (m - s > strend - d) { /* faster to shorten from end */
1509 if (clen) {
1510 Copy(c, m, clen, char);
1511 m += clen;
a0d0e21e 1512 }
71be2cbc
PP
1513 i = strend - d;
1514 if (i > 0) {
1515 Move(d, m, i, char);
1516 m += i;
a0d0e21e 1517 }
71be2cbc
PP
1518 *m = '\0';
1519 SvCUR_set(TARG, m - s);
1520 }
1521 /*SUPPRESS 560*/
1522 else if (i = m - s) { /* faster from front */
1523 d -= clen;
1524 m = d;
1525 sv_chop(TARG, d-i);
1526 s += i;
1527 while (i--)
1528 *--d = *--s;
1529 if (clen)
1530 Copy(c, m, clen, char);
1531 }
1532 else if (clen) {
1533 d -= clen;
1534 sv_chop(TARG, d);
1535 Copy(c, d, clen, char);
1536 }
1537 else {
1538 sv_chop(TARG, d);
1539 }
9212bbba 1540 TAINT_IF(rxtainted);
71be2cbc
PP
1541 PUSHs(&sv_yes);
1542 }
1543 else {
1544 rxtainted = 0;
1545 do {
1546 if (iters++ > maxiters)
1547 DIE("Substitution loop");
1548 rxtainted |= rx->exec_tainted;
1549 m = rx->startp[0];
1550 /*SUPPRESS 560*/
1551 if (i = m - s) {
1552 if (s != d)
1553 Move(s, d, i, char);
1554 d += i;
a0d0e21e 1555 }
71be2cbc
PP
1556 if (clen) {
1557 Copy(c, d, clen, char);
1558 d += clen;
1559 }
1560 s = rx->endp[0];
1561 } while (pregexec(rx, s, strend, orig, s == m,
1562 Nullsv, TRUE)); /* don't match same null twice */
1563 if (s != d) {
1564 i = strend - s;
1565 SvCUR_set(TARG, d - SvPVX(TARG) + i);
1566 Move(s, d, i+1, char); /* include the NUL */
a0d0e21e 1567 }
9212bbba 1568 TAINT_IF(rxtainted);
71be2cbc 1569 PUSHs(sv_2mortal(newSViv((I32)iters)));
a0d0e21e 1570 }
71be2cbc
PP
1571 (void)SvPOK_only(TARG);
1572 SvSETMAGIC(TARG);
9212bbba 1573 SvTAINT(TARG);
71be2cbc
PP
1574 LEAVE_SCOPE(oldsave);
1575 RETURN;
a0d0e21e 1576 }
71be2cbc 1577
e50aee73 1578 if (pregexec(rx, s, strend, orig, 0,
5f05dabc 1579 SvSCREAM(TARG) ? TARG : Nullsv, safebase)) {
a0d0e21e
LW
1580 if (force_on_match) {
1581 force_on_match = 0;
1582 s = SvPV_force(TARG, len);
1583 goto force_it;
1584 }
71be2cbc 1585 rxtainted = rx->exec_tainted;
a0d0e21e
LW
1586 dstr = NEWSV(25, sv_len(TARG));
1587 sv_setpvn(dstr, m, s-m);
1588 curpm = pm;
1589 if (!c) {
1590 register CONTEXT *cx;
1591 PUSHSUBST(cx);
1592 RETURNOP(cPMOP->op_pmreplroot);
1593 }
1594 do {
1595 if (iters++ > maxiters)
1596 DIE("Substitution loop");
71be2cbc 1597 rxtainted |= rx->exec_tainted;
a0d0e21e
LW
1598 if (rx->subbase && rx->subbase != orig) {
1599 m = s;
1600 s = orig;
1601 orig = rx->subbase;
1602 s = orig + (m - s);
1603 strend = s + (strend - m);
1604 }
1605 m = rx->startp[0];
1606 sv_catpvn(dstr, s, m-s);
1607 s = rx->endp[0];
1608 if (clen)
1609 sv_catpvn(dstr, c, clen);
1610 if (once)
1611 break;
5f05dabc 1612 } while (pregexec(rx, s, strend, orig, s == m, Nullsv, safebase));
a0d0e21e 1613 sv_catpvn(dstr, s, strend - s);
748a9306 1614
9212bbba
CS
1615 TAINT_IF(rxtainted);
1616
4633a7c4 1617 (void)SvOOK_off(TARG);
cb0b1708 1618 Safefree(SvPVX(TARG));
748a9306
LW
1619 SvPVX(TARG) = SvPVX(dstr);
1620 SvCUR_set(TARG, SvCUR(dstr));
1621 SvLEN_set(TARG, SvLEN(dstr));
1622 SvPVX(dstr) = 0;
1623 sv_free(dstr);
1624
a0d0e21e
LW
1625 (void)SvPOK_only(TARG);
1626 SvSETMAGIC(TARG);
9212bbba 1627 SvTAINT(TARG);
a0d0e21e 1628 PUSHs(sv_2mortal(newSViv((I32)iters)));
4633a7c4 1629 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1630 RETURN;
1631 }
5cd24f17 1632 goto ret_no;
a0d0e21e
LW
1633
1634nope:
1635 ++BmUSEFUL(pm->op_pmshort);
5cd24f17
PP
1636
1637ret_no:
a0d0e21e 1638 PUSHs(&sv_no);
4633a7c4 1639 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1640 RETURN;
1641}
1642
1643PP(pp_grepwhile)
1644{
1645 dSP;
1646
1647 if (SvTRUEx(POPs))
1648 stack_base[markstack_ptr[-1]++] = stack_base[*markstack_ptr];
1649 ++*markstack_ptr;
1650 LEAVE; /* exit inner scope */
1651
1652 /* All done yet? */
1653 if (stack_base + *markstack_ptr > sp) {
1654 I32 items;
54310121 1655 I32 gimme = GIMME_V;
a0d0e21e
LW
1656
1657 LEAVE; /* exit outer scope */
1658 (void)POPMARK; /* pop src */
1659 items = --*markstack_ptr - markstack_ptr[-1];
1660 (void)POPMARK; /* pop dst */
1661 SP = stack_base + POPMARK; /* pop original mark */
54310121 1662 if (gimme == G_SCALAR) {
a0d0e21e
LW
1663 dTARGET;
1664 XPUSHi(items);
a0d0e21e 1665 }
54310121
PP
1666 else if (gimme == G_ARRAY)
1667 SP += items;
a0d0e21e
LW
1668 RETURN;
1669 }
1670 else {
1671 SV *src;
1672
1673 ENTER; /* enter inner scope */
1674 SAVESPTR(curpm);
1675
1676 src = stack_base[*markstack_ptr];
1677 SvTEMP_off(src);
1678 GvSV(defgv) = src;
1679
1680 RETURNOP(cLOGOP->op_other);
1681 }
1682}
1683
1684PP(pp_leavesub)
1685{
1686 dSP;
1687 SV **mark;
1688 SV **newsp;
1689 PMOP *newpm;
1690 I32 gimme;
1691 register CONTEXT *cx;
f86702cc 1692 struct block_sub cxsub;
a0d0e21e
LW
1693
1694 POPBLOCK(cx,newpm);
f86702cc
PP
1695 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1696
a0d0e21e
LW
1697 if (gimme == G_SCALAR) {
1698 MARK = newsp + 1;
1699 if (MARK <= SP)
f86702cc 1700 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
a0d0e21e 1701 else {
f86702cc 1702 MEXTEND(MARK, 0);
a0d0e21e
LW
1703 *MARK = &sv_undef;
1704 }
1705 SP = MARK;
1706 }
54310121 1707 else if (gimme == G_ARRAY) {
f86702cc
PP
1708 for (MARK = newsp + 1; MARK <= SP; MARK++) {
1709 if (!SvTEMP(*MARK))
1710 *MARK = sv_mortalcopy(*MARK);
1711 }
a0d0e21e 1712 }
f86702cc
PP
1713 PUTBACK;
1714
1715 POPSUB2(); /* Stack values are safe: release CV and @_ ... */
1716 curpm = newpm; /* ... and pop $1 et al */
a0d0e21e
LW
1717
1718 LEAVE;
a0d0e21e
LW
1719 return pop_return();
1720}
1721
1722PP(pp_entersub)
1723{
1724 dSP; dPOPss;
1725 GV *gv;
1726 HV *stash;
1727 register CV *cv;
1728 register CONTEXT *cx;
5d94fbed 1729 I32 gimme;
07055b4c 1730 bool hasargs = (op->op_flags & OPf_STACKED) != 0;
a0d0e21e
LW
1731
1732 if (!sv)
1733 DIE("Not a CODE reference");
1734 switch (SvTYPE(sv)) {
1735 default:
1736 if (!SvROK(sv)) {
748a9306
LW
1737 char *sym;
1738
a0d0e21e
LW
1739 if (sv == &sv_yes) /* unfound import, ignore */
1740 RETURN;
15ff848f
CS
1741 if (SvGMAGICAL(sv)) {
1742 mg_get(sv);
1743 sym = SvPOKp(sv) ? SvPVX(sv) : Nullch;
1744 }
1745 else
1746 sym = SvPV(sv, na);
1747 if (!sym)
a0d0e21e
LW
1748 DIE(no_usym, "a subroutine");
1749 if (op->op_private & HINT_STRICT_REFS)
748a9306
LW
1750 DIE(no_symref, sym, "a subroutine");
1751 cv = perl_get_cv(sym, TRUE);
a0d0e21e
LW
1752 break;
1753 }
1754 cv = (CV*)SvRV(sv);
28757baa 1755 if (SvTYPE(cv) == SVt_PVCV)
a0d0e21e
LW
1756 break;
1757 /* FALL THROUGH */
1758 case SVt_PVHV:
1759 case SVt_PVAV:
1760 DIE("Not a CODE reference");
1761 case SVt_PVCV:
1762 cv = (CV*)sv;
1763 break;
1764 case SVt_PVGV:
8ebc5c01 1765 if (!(cv = GvCVu((GV*)sv)))
a0d0e21e
LW
1766 cv = sv_2cv(sv, &stash, &gv, TRUE);
1767 break;
1768 }
1769
8ebc5c01
PP
1770 ENTER;
1771 SAVETMPS;
1772
a0d0e21e
LW
1773 retry:
1774 if (!cv)
1775 DIE("Not a CODE reference");
1776
1777 if (!CvROOT(cv) && !CvXSUB(cv)) {
44a8e56a
PP
1778 GV* autogv;
1779 SV* subname;
1780
1781 /* anonymous or undef'd function leaves us no recourse */
1782 if (CvANON(cv) || !(gv = CvGV(cv)))
1783 DIE("Undefined subroutine called");
1784 /* autoloaded stub? */
1785 if (cv != GvCV(gv)) {
1786 cv = GvCV(gv);
1787 goto retry;
a0d0e21e 1788 }
44a8e56a 1789 /* should call AUTOLOAD now? */
54310121
PP
1790 if ((autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
1791 FALSE)))
1792 {
44a8e56a
PP
1793 cv = GvCV(autogv);
1794 goto retry;
1795 }
1796 /* sorry */
1797 subname = sv_newmortal();
1798 gv_efullname3(subname, gv, Nullch);
1799 DIE("Undefined subroutine &%s called", SvPVX(subname));
a0d0e21e
LW
1800 }
1801
54310121 1802 gimme = GIMME_V;
28757baa 1803 if ((op->op_private & OPpENTERSUB_DB) && GvCV(DBsub) && !CvNODEBUG(cv)) {
71be2cbc 1804 SV *oldsv = sv;
a0d0e21e
LW
1805 sv = GvSV(DBsub);
1806 save_item(sv);
67955e0c 1807 gv = CvGV(cv);
71be2cbc
PP
1808 if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED))
1809 || strEQ(GvNAME(gv), "END")
1810 || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */
1811 !( (SvTYPE(oldsv) == SVt_PVGV) && (GvCV((GV*)oldsv) == cv)
1812 && (gv = (GV*)oldsv) ))) { /* Use GV from the stack as a fallback. */
1813 /* GV is potentially non-unique, or contain different CV. */
748a9306 1814 sv_setsv(sv, newRV((SV*)cv));
a5f75d66 1815 }
748a9306 1816 else {
4f732624 1817 gv_efullname3(sv, gv, Nullch);
748a9306 1818 }
a0d0e21e 1819 cv = GvCV(DBsub);
67955e0c 1820 if (CvXSUB(cv)) curcopdb = curcop;
a0d0e21e
LW
1821 if (!cv)
1822 DIE("No DBsub routine");
1823 }
1824
1825 if (CvXSUB(cv)) {
1826 if (CvOLDSTYLE(cv)) {
ecfc5424 1827 I32 (*fp3)_((int,int,int));
a0d0e21e
LW
1828 dMARK;
1829 register I32 items = SP - MARK;
67955e0c 1830 /* We dont worry to copy from @_. */
a0d0e21e
LW
1831 while (sp > mark) {
1832 sp[1] = sp[0];
1833 sp--;
1834 }
1835 stack_sp = mark + 1;
ecfc5424
AD
1836 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
1837 items = (*fp3)(CvXSUBANY(cv).any_i32,
1838 MARK - stack_base + 1,
1839 items);
a0d0e21e
LW
1840 stack_sp = stack_base + items;
1841 }
1842 else {
748a9306
LW
1843 I32 markix = TOPMARK;
1844
a0d0e21e 1845 PUTBACK;
67955e0c
PP
1846
1847 if (!hasargs) {
1848 /* Need to copy @_ to stack. Alternative may be to
1849 * switch stack to @_, and copy return values
1850 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
1851 AV* av = GvAV(defgv);
1852 I32 items = AvFILL(av) + 1;
1853
1854 if (items) {
1855 /* Mark is at the end of the stack. */
1856 EXTEND(sp, items);
1857 Copy(AvARRAY(av), sp + 1, items, SV*);
1858 sp += items;
1859 PUTBACK ;
1860 }
1861 }
1862 if (curcopdb) { /* We assume that the first
1863 XSUB in &DB::sub is the
1864 called one. */
1865 SAVESPTR(curcop);
1866 curcop = curcopdb;
1867 curcopdb = NULL;
1868 }
1869 /* Do we need to open block here? XXXX */
a0d0e21e 1870 (void)(*CvXSUB(cv))(cv);
748a9306
LW
1871
1872 /* Enforce some sanity in scalar context. */
5d94fbed 1873 if (gimme == G_SCALAR && ++markix != stack_sp - stack_base ) {
748a9306
LW
1874 if (markix > stack_sp - stack_base)
1875 *(stack_base + markix) = &sv_undef;
1876 else
1877 *(stack_base + markix) = *stack_sp;
1878 stack_sp = stack_base + markix;
1879 }
a0d0e21e
LW
1880 }
1881 LEAVE;
1882 return NORMAL;
1883 }
1884 else {
1885 dMARK;
1886 register I32 items = SP - MARK;
a0d0e21e
LW
1887 AV* padlist = CvPADLIST(cv);
1888 SV** svp = AvARRAY(padlist);
1889 push_return(op->op_next);
1890 PUSHBLOCK(cx, CXt_SUB, MARK);
1891 PUSHSUB(cx);
1892 CvDEPTH(cv)++;
1893 if (CvDEPTH(cv) < 2)
1894 (void)SvREFCNT_inc(cv);
1895 else { /* save temporaries on recursion? */
5f05dabc 1896 if (CvDEPTH(cv) == 100 && dowarn
44a8e56a
PP
1897 && !(perldb && cv == GvCV(DBsub)))
1898 sub_crush_depth(cv);
a0d0e21e
LW
1899 if (CvDEPTH(cv) > AvFILL(padlist)) {
1900 AV *av;
1901 AV *newpad = newAV();
4aa0a1f7 1902 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
a0d0e21e
LW
1903 I32 ix = AvFILL((AV*)svp[1]);
1904 svp = AvARRAY(svp[0]);
748a9306 1905 for ( ;ix > 0; ix--) {
a0d0e21e 1906 if (svp[ix] != &sv_undef) {
748a9306 1907 char *name = SvPVX(svp[ix]);
5f05dabc
PP
1908 if ((SvFLAGS(svp[ix]) & SVf_FAKE) /* outer lexical? */
1909 || *name == '&') /* anonymous code? */
1910 {
1911 av_store(newpad, ix, SvREFCNT_inc(oldpad[ix]));
748a9306
LW
1912 }
1913 else { /* our own lexical */
1914 if (*name == '@')
1915 av_store(newpad, ix, sv = (SV*)newAV());
1916 else if (*name == '%')
1917 av_store(newpad, ix, sv = (SV*)newHV());
1918 else
1919 av_store(newpad, ix, sv = NEWSV(0,0));
1920 SvPADMY_on(sv);
1921 }
a0d0e21e
LW
1922 }
1923 else {
748a9306 1924 av_store(newpad, ix, sv = NEWSV(0,0));
a0d0e21e
LW
1925 SvPADTMP_on(sv);
1926 }
1927 }
1928 av = newAV(); /* will be @_ */
1929 av_extend(av, 0);
1930 av_store(newpad, 0, (SV*)av);
1931 AvFLAGS(av) = AVf_REIFY;
1932 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
1933 AvFILL(padlist) = CvDEPTH(cv);
1934 svp = AvARRAY(padlist);
1935 }
1936 }
1937 SAVESPTR(curpad);
1938 curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
1939 if (hasargs) {
1940 AV* av = (AV*)curpad[0];
1941 SV** ary;
1942
1943 if (AvREAL(av)) {
1944 av_clear(av);
1945 AvREAL_off(av);
1946 }
1947 cx->blk_sub.savearray = GvAV(defgv);
1948 cx->blk_sub.argarray = av;
0c8c7a05 1949 GvAV(defgv) = (AV*)SvREFCNT_inc(av);
a0d0e21e
LW
1950 ++MARK;
1951
1952 if (items > AvMAX(av) + 1) {
1953 ary = AvALLOC(av);
1954 if (AvARRAY(av) != ary) {
1955 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
1956 SvPVX(av) = (char*)ary;
1957 }
1958 if (items > AvMAX(av) + 1) {
1959 AvMAX(av) = items - 1;
1960 Renew(ary,items,SV*);
1961 AvALLOC(av) = ary;
1962 SvPVX(av) = (char*)ary;
1963 }
1964 }
1965 Copy(MARK,AvARRAY(av),items,SV*);
1966 AvFILL(av) = items - 1;
1967
1968 while (items--) {
1969 if (*MARK)
1970 SvTEMP_off(*MARK);
1971 MARK++;
1972 }
1973 }
1974 RETURNOP(CvSTART(cv));
1975 }
1976}
1977
44a8e56a
PP
1978void
1979sub_crush_depth(cv)
1980CV* cv;
1981{
1982 if (CvANON(cv))
1983 warn("Deep recursion on anonymous subroutine");
1984 else {
1985 SV* tmpstr = sv_newmortal();
1986 gv_efullname3(tmpstr, CvGV(cv), Nullch);
1987 warn("Deep recursion on subroutine \"%s\"", SvPVX(tmpstr));
1988 }
1989}
1990
a0d0e21e
LW
1991PP(pp_aelem)
1992{
1993 dSP;
1994 SV** svp;
748a9306 1995 I32 elem = POPi;
68dc0745
PP
1996 AV* av = (AV*)POPs;
1997 U32 lval = op->op_flags & OPf_MOD;
1998 U32 defer = (op->op_private & OPpLVAL_DEFER) && (elem > AvFILL(av));
a0d0e21e 1999
748a9306
LW
2000 if (elem > 0)
2001 elem -= curcop->cop_arybase;
a0d0e21e
LW
2002 if (SvTYPE(av) != SVt_PVAV)
2003 RETPUSHUNDEF;
68dc0745 2004 svp = av_fetch(av, elem, lval && !defer);
a0d0e21e 2005 if (lval) {
68dc0745
PP
2006 if (!svp || *svp == &sv_undef) {
2007 SV* lv;
2008 if (!defer)
2009 DIE(no_aelem, elem);
2010 lv = sv_newmortal();
2011 sv_upgrade(lv, SVt_PVLV);
2012 LvTYPE(lv) = 'y';
2013 sv_magic(lv, Nullsv, 'y', Nullch, 0);
2014 LvTARG(lv) = SvREFCNT_inc(av);
2015 LvTARGOFF(lv) = elem;
2016 LvTARGLEN(lv) = 1;
2017 PUSHs(lv);
2018 RETURN;
2019 }
a0d0e21e
LW
2020 if (op->op_private & OPpLVAL_INTRO)
2021 save_svref(svp);
5f05dabc 2022 else if (op->op_private & OPpDEREF)
68dc0745 2023 vivify_ref(*svp, op->op_private & OPpDEREF);
a0d0e21e
LW
2024 }
2025 PUSHs(svp ? *svp : &sv_undef);
2026 RETURN;
2027}
2028
02a9e968 2029void
68dc0745 2030vivify_ref(sv, to_what)
02a9e968 2031SV* sv;
68dc0745 2032U32 to_what;
02a9e968
CS
2033{
2034 if (SvGMAGICAL(sv))
2035 mg_get(sv);
2036 if (!SvOK(sv)) {
2037 if (SvREADONLY(sv))
2038 croak(no_modify);
5f05dabc
PP
2039 if (SvTYPE(sv) < SVt_RV)
2040 sv_upgrade(sv, SVt_RV);
2041 else if (SvTYPE(sv) >= SVt_PV) {
2042 (void)SvOOK_off(sv);
2043 Safefree(SvPVX(sv));
2044 SvLEN(sv) = SvCUR(sv) = 0;
2045 }
68dc0745 2046 switch (to_what) {
5f05dabc
PP
2047 case OPpDEREF_SV:
2048 SvRV(sv) = newSV(0);
2049 break;
2050 case OPpDEREF_AV:
2051 SvRV(sv) = (SV*)newAV();
2052 break;
2053 case OPpDEREF_HV:
2054 SvRV(sv) = (SV*)newHV();
2055 break;
2056 }
02a9e968
CS
2057 SvROK_on(sv);
2058 SvSETMAGIC(sv);
2059 }
2060}
2061
a0d0e21e
LW
2062PP(pp_method)
2063{
2064 dSP;
2065 SV* sv;
2066 SV* ob;
ac91690f 2067 HV* stash;
a0d0e21e 2068 GV* gv;
ac91690f
CS
2069 char* packname;
2070 STRLEN packlen;
2071 char* name;
2072 char* origname;
2073 char* sep;
2074 char* p;
a0d0e21e 2075
a0d0e21e 2076 sv = *(stack_base + TOPMARK + 1);
ac91690f
CS
2077
2078 sep = name = origname = SvPV(TOPs, na);
2079 for (p = name; *p; p++) {
2080 if (*p == '\'')
2081 sep = p, name = p + 1;
2082 else if (*p == ':' && *(p + 1) == ':')
2083 sep = p, name = p + 2;
2084 }
2085 if (name == origname)
2086 packname = Nullch;
2087 else {
2088 packname = origname;
2089 packlen = sep - origname;
2090
2091 /* let gv_fetchmethod() handle SUPER:: */
2092 if (packlen == 5 && strnEQ(packname, "SUPER", 5)) {
2093 packname = Nullch;
2094 name = origname;
2095 }
2096 }
2097
16d20bd9
AD
2098 if (SvGMAGICAL(sv))
2099 mg_get(sv);
a0d0e21e 2100 if (SvROK(sv))
16d20bd9 2101 ob = (SV*)SvRV(sv);
a0d0e21e 2102 else {
ac91690f
CS
2103 char* tname = Nullch;
2104 STRLEN tlen = 0;
a0d0e21e 2105 GV* iogv;
a0d0e21e
LW
2106
2107 if (!SvOK(sv) ||
ac91690f
CS
2108 !(tname = SvPV(sv, tlen)) ||
2109 !(iogv = gv_fetchpv(tname, FALSE, SVt_PVIO)) ||
2110 !(ob = (SV*)GvIO(iogv)))
a0d0e21e 2111 {
ac91690f
CS
2112 if (!packname) {
2113 packname = tname;
2114 packlen = tlen;
a0d0e21e 2115 }
ac91690f
CS
2116 if (!packname || !isALPHA(*packname))
2117 DIE("Can't call method \"%s\" without a package or object reference", name);
2118 goto fetch;
a0d0e21e 2119 }
a0d0e21e 2120
ac91690f
CS
2121 /* working on an IO object */
2122 *(stack_base + TOPMARK + 1) = sv_2mortal(newRV((SV*)iogv));
a0d0e21e
LW
2123 }
2124
ac91690f
CS
2125 if (!ob || !SvOBJECT(ob))
2126 DIE("Can't call method \"%s\" on unblessed reference", origname);
a0d0e21e 2127
ac91690f
CS
2128 if (!packname)
2129 stash = SvSTASH(ob);
2130 else {
2131 fetch:
2132 stash = gv_stashpvn(packname, packlen, TRUE);
2133 }
2134 gv = gv_fetchmethod(stash, name);
2135 if (!gv)
2136 DIE("Can't locate object method \"%s\" via package \"%s\"",
2137 name, HvNAME((strnEQ(name, "SUPER", 5) &&
2138 (name[5] == '\'' ||
2139 (name[5] == ':' && name[6] == ':')))
2140 ? curcop->cop_stash : stash));
8ebc5c01 2141 SETs(isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv);
a0d0e21e
LW
2142 RETURN;
2143}
2144