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