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