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