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