This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perly_c.diff typo
[perl5.git] / pp_hot.c
CommitLineData
a0d0e21e
LW
1/* pp_hot.c
2 *
9607fc9c 3 * Copyright (c) 1991-1997, Larry Wall
a0d0e21e
LW
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
8 */
9
10/*
11 * Then he heard Merry change the note, and up went the Horn-cry of Buckland,
12 * shaking the air.
13 *
14 * Awake! Awake! Fear, Fire, Foes! Awake!
15 * Fire, Foes! Awake!
16 */
17
18#include "EXTERN.h"
19#include "perl.h"
20
8ac85365
NIS
21#ifdef I_UNISTD
22#include <unistd.h>
23#endif
9d116dd7
JH
24#ifdef I_FCNTL
25#include <fcntl.h>
26#endif
27#ifdef I_SYS_FILE
28#include <sys/file.h>
29#endif
8ac85365 30
a0ed51b3
LW
31#define HOP(pos,off) (IN_UTF8 ? utf8_hop(pos, off) : (pos + off))
32
a0d0e21e
LW
33/* Hot code. */
34
11343788
MB
35#ifdef USE_THREADS
36static void
8ac85365 37unset_cvowner(void *cvarg)
11343788
MB
38{
39 register CV* cv = (CV *) cvarg;
40#ifdef DEBUGGING
41 dTHR;
42#endif /* DEBUGGING */
43
8b73bbec 44 DEBUG_S((PerlIO_printf(PerlIO_stderr(), "%p unsetting CvOWNER of %p:%s\n",
1fd28e87 45 thr, cv, SvPEEK((SV*)cv))));
11343788 46 MUTEX_LOCK(CvMUTEXP(cv));
8b73bbec 47 DEBUG_S(if (CvDEPTH(cv) != 0)
77a005ab
MB
48 PerlIO_printf(PerlIO_stderr(), "depth %ld != 0\n",
49 CvDEPTH(cv)););
11343788
MB
50 assert(thr == CvOWNER(cv));
51 CvOWNER(cv) = 0;
11343788
MB
52 MUTEX_UNLOCK(CvMUTEXP(cv));
53 SvREFCNT_dec(cv);
54}
11343788
MB
55#endif /* USE_THREADS */
56
a0d0e21e
LW
57PP(pp_const)
58{
4e35701f 59 djSP;
a0d0e21e
LW
60 XPUSHs(cSVOP->op_sv);
61 RETURN;
62}
63
64PP(pp_nextstate)
65{
533c011a 66 PL_curcop = (COP*)PL_op;
a0d0e21e 67 TAINT_NOT; /* Each statement is presumed innocent */
3280af22 68 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
a0d0e21e
LW
69 FREETMPS;
70 return NORMAL;
71}
72
73PP(pp_gvsv)
74{
4e35701f 75 djSP;
924508f0 76 EXTEND(SP,1);
533c011a 77 if (PL_op->op_private & OPpLVAL_INTRO)
a0d0e21e
LW
78 PUSHs(save_scalar(cGVOP->op_gv));
79 else
80 PUSHs(GvSV(cGVOP->op_gv));
81 RETURN;
82}
83
84PP(pp_null)
85{
86 return NORMAL;
87}
88
89PP(pp_pushmark)
90{
3280af22 91 PUSHMARK(PL_stack_sp);
a0d0e21e
LW
92 return NORMAL;
93}
94
95PP(pp_stringify)
96{
4e35701f 97 djSP; dTARGET;
a0d0e21e
LW
98 STRLEN len;
99 char *s;
100 s = SvPV(TOPs,len);
101 sv_setpvn(TARG,s,len);
102 SETTARG;
103 RETURN;
104}
105
106PP(pp_gv)
107{
4e35701f 108 djSP;
a0d0e21e
LW
109 XPUSHs((SV*)cGVOP->op_gv);
110 RETURN;
111}
112
113PP(pp_and)
114{
4e35701f 115 djSP;
a0d0e21e
LW
116 if (!SvTRUE(TOPs))
117 RETURN;
118 else {
119 --SP;
120 RETURNOP(cLOGOP->op_other);
121 }
122}
123
124PP(pp_sassign)
125{
4e35701f 126 djSP; dPOPTOPssrl;
748a9306
LW
127 MAGIC *mg;
128
533c011a 129 if (PL_op->op_private & OPpASSIGN_BACKWARDS) {
a0d0e21e
LW
130 SV *temp;
131 temp = left; left = right; right = temp;
132 }
3280af22 133 if (PL_tainting && PL_tainted && !SvTAINTED(left))
a0d0e21e 134 TAINT_NOT;
54310121 135 SvSetMagicSV(right, left);
a0d0e21e
LW
136 SETs(right);
137 RETURN;
138}
139
140PP(pp_cond_expr)
141{
4e35701f 142 djSP;
a0d0e21e
LW
143 if (SvTRUEx(POPs))
144 RETURNOP(cCONDOP->op_true);
145 else
146 RETURNOP(cCONDOP->op_false);
147}
148
149PP(pp_unstack)
150{
151 I32 oldsave;
152 TAINT_NOT; /* Each statement is presumed innocent */
3280af22 153 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
a0d0e21e 154 FREETMPS;
3280af22 155 oldsave = PL_scopestack[PL_scopestack_ix - 1];
a0d0e21e
LW
156 LEAVE_SCOPE(oldsave);
157 return NORMAL;
158}
159
a0d0e21e
LW
160PP(pp_concat)
161{
4e35701f 162 djSP; dATARGET; tryAMAGICbin(concat,opASSIGN);
748a9306
LW
163 {
164 dPOPTOPssrl;
a0d0e21e
LW
165 STRLEN len;
166 char *s;
167 if (TARG != left) {
168 s = SvPV(left,len);
169 sv_setpvn(TARG,s,len);
170 }
c07a80fd
PP
171 else if (SvGMAGICAL(TARG))
172 mg_get(TARG);
7a4c00b4 173 else if (!SvOK(TARG) && SvTYPE(TARG) <= SVt_PVMG) {
748a9306 174 sv_setpv(TARG, ""); /* Suppress warning. */
7a4c00b4 175 s = SvPV_force(TARG, len);
4633a7c4 176 }
a0d0e21e 177 s = SvPV(right,len);
68dc0745
PP
178 if (SvOK(TARG))
179 sv_catpvn(TARG,s,len);
180 else
181 sv_setpvn(TARG,s,len); /* suppress warning */
a0d0e21e
LW
182 SETTARG;
183 RETURN;
748a9306 184 }
a0d0e21e
LW
185}
186
187PP(pp_padsv)
188{
4e35701f 189 djSP; dTARGET;
a0d0e21e 190 XPUSHs(TARG);
533c011a
NIS
191 if (PL_op->op_flags & OPf_MOD) {
192 if (PL_op->op_private & OPpLVAL_INTRO)
193 SAVECLEARSV(PL_curpad[PL_op->op_targ]);
194 else if (PL_op->op_private & OPpDEREF) {
8ec5e241 195 PUTBACK;
533c011a 196 vivify_ref(PL_curpad[PL_op->op_targ], PL_op->op_private & OPpDEREF);
8ec5e241
NIS
197 SPAGAIN;
198 }
4633a7c4 199 }
a0d0e21e
LW
200 RETURN;
201}
202
203PP(pp_readline)
204{
f5284f61 205 tryAMAGICunTARGET(iter, 0);
3280af22 206 PL_last_in_gv = (GV*)(*PL_stack_sp--);
f5284f61
IZ
207 if (PL_op->op_flags & OPf_SPECIAL) { /* Are called as <$var> */
208 if (SvROK(PL_last_in_gv)) {
209 if (SvTYPE(SvRV(PL_last_in_gv)) != SVt_PVGV)
210 goto hard_way;
211 PL_last_in_gv = (GV*)SvRV(PL_last_in_gv);
212 } else if (SvTYPE(PL_last_in_gv) != SVt_PVGV) {
213 hard_way: {
214 dSP;
215 XPUSHs((SV*)PL_last_in_gv);
216 PUTBACK;
217 pp_rv2gv(ARGS);
218 PL_last_in_gv = (GV*)(*PL_stack_sp--);
219 }
220 }
221 }
a0d0e21e
LW
222 return do_readline();
223}
224
225PP(pp_eq)
226{
4e35701f 227 djSP; tryAMAGICbinSET(eq,0);
a0d0e21e
LW
228 {
229 dPOPnv;
54310121 230 SETs(boolSV(TOPn == value));
a0d0e21e
LW
231 RETURN;
232 }
233}
234
235PP(pp_preinc)
236{
4e35701f 237 djSP;
68dc0745 238 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
22c35a8c 239 croak(PL_no_modify);
55497cff
PP
240 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
241 SvIVX(TOPs) != IV_MAX)
242 {
748a9306 243 ++SvIVX(TOPs);
55497cff 244 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306
LW
245 }
246 else
247 sv_inc(TOPs);
a0d0e21e
LW
248 SvSETMAGIC(TOPs);
249 return NORMAL;
250}
251
252PP(pp_or)
253{
4e35701f 254 djSP;
a0d0e21e
LW
255 if (SvTRUE(TOPs))
256 RETURN;
257 else {
258 --SP;
259 RETURNOP(cLOGOP->op_other);
260 }
261}
262
263PP(pp_add)
264{
4e35701f 265 djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
a0d0e21e 266 {
7a4c00b4 267 dPOPTOPnnrl_ul;
a0d0e21e
LW
268 SETn( left + right );
269 RETURN;
270 }
271}
272
273PP(pp_aelemfast)
274{
4e35701f 275 djSP;
a0d0e21e 276 AV *av = GvAV((GV*)cSVOP->op_sv);
533c011a
NIS
277 U32 lval = PL_op->op_flags & OPf_MOD;
278 SV** svp = av_fetch(av, PL_op->op_private, lval);
3280af22 279 SV *sv = (svp ? *svp : &PL_sv_undef);
6ff81951 280 EXTEND(SP, 1);
be6c24e0
GS
281 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
282 sv = sv_mortalcopy(sv);
283 PUSHs(sv);
a0d0e21e
LW
284 RETURN;
285}
286
287PP(pp_join)
288{
4e35701f 289 djSP; dMARK; dTARGET;
a0d0e21e
LW
290 MARK++;
291 do_join(TARG, *MARK, MARK, SP);
292 SP = MARK;
293 SETs(TARG);
294 RETURN;
295}
296
297PP(pp_pushre)
298{
4e35701f 299 djSP;
44a8e56a
PP
300#ifdef DEBUGGING
301 /*
302 * We ass_u_me that LvTARGOFF() comes first, and that two STRLENs
303 * will be enough to hold an OP*.
304 */
305 SV* sv = sv_newmortal();
306 sv_upgrade(sv, SVt_PVLV);
307 LvTYPE(sv) = '/';
533c011a 308 Copy(&PL_op, &LvTARGOFF(sv), 1, OP*);
44a8e56a
PP
309 XPUSHs(sv);
310#else
6b88bc9c 311 XPUSHs((SV*)PL_op);
44a8e56a 312#endif
a0d0e21e
LW
313 RETURN;
314}
315
316/* Oversized hot code. */
317
318PP(pp_print)
319{
4e35701f 320 djSP; dMARK; dORIGMARK;
a0d0e21e
LW
321 GV *gv;
322 IO *io;
760ac839 323 register PerlIO *fp;
236988e4 324 MAGIC *mg;
2d8e6c8d 325 STRLEN n_a;
a0d0e21e 326
533c011a 327 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e
LW
328 gv = (GV*)*++MARK;
329 else
3280af22 330 gv = PL_defoutgv;
33c27489 331 if (mg = SvTIED_mg((SV*)gv, 'q')) {
68dc0745 332 if (MARK == ORIGMARK) {
a60c0954
NIS
333 /* If using default handle then we need to make space to
334 * pass object as 1st arg, so move other args up ...
335 */
4352c267 336 MEXTEND(SP, 1);
68dc0745
PP
337 ++MARK;
338 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
339 ++SP;
340 }
341 PUSHMARK(MARK - 1);
33c27489 342 *MARK = SvTIED_obj((SV*)gv, mg);
68dc0745 343 PUTBACK;
236988e4
PP
344 ENTER;
345 perl_call_method("PRINT", G_SCALAR);
346 LEAVE;
347 SPAGAIN;
68dc0745
PP
348 MARK = ORIGMARK + 1;
349 *MARK = *SP;
350 SP = MARK;
236988e4
PP
351 RETURN;
352 }
a0d0e21e 353 if (!(io = GvIO(gv))) {
599cee73 354 if (ckWARN(WARN_UNOPENED)) {
748a9306 355 SV* sv = sv_newmortal();
4f732624 356 gv_fullname3(sv, gv, Nullch);
2d8e6c8d 357 warner(WARN_UNOPENED, "Filehandle %s never opened", SvPV(sv,n_a));
748a9306
LW
358 }
359
360 SETERRNO(EBADF,RMS$_IFI);
a0d0e21e
LW
361 goto just_say_no;
362 }
363 else if (!(fp = IoOFP(io))) {
599cee73 364 if (ckWARN2(WARN_CLOSED, WARN_IO)) {
748a9306 365 SV* sv = sv_newmortal();
4f732624 366 gv_fullname3(sv, gv, Nullch);
a0d0e21e 367 if (IoIFP(io))
599cee73 368 warner(WARN_IO, "Filehandle %s opened only for input",
2d8e6c8d 369 SvPV(sv,n_a));
599cee73
PM
370 else if (ckWARN(WARN_CLOSED))
371 warner(WARN_CLOSED, "print on closed filehandle %s",
2d8e6c8d 372 SvPV(sv,n_a));
a0d0e21e 373 }
748a9306 374 SETERRNO(EBADF,IoIFP(io)?RMS$_FAC:RMS$_IFI);
a0d0e21e
LW
375 goto just_say_no;
376 }
377 else {
378 MARK++;
3280af22 379 if (PL_ofslen) {
a0d0e21e
LW
380 while (MARK <= SP) {
381 if (!do_print(*MARK, fp))
382 break;
383 MARK++;
384 if (MARK <= SP) {
3280af22 385 if (PerlIO_write(fp, PL_ofs, PL_ofslen) == 0 || PerlIO_error(fp)) {
a0d0e21e
LW
386 MARK--;
387 break;
388 }
389 }
390 }
391 }
392 else {
393 while (MARK <= SP) {
394 if (!do_print(*MARK, fp))
395 break;
396 MARK++;
397 }
398 }
399 if (MARK <= SP)
400 goto just_say_no;
401 else {
3280af22
NIS
402 if (PL_orslen)
403 if (PerlIO_write(fp, PL_ors, PL_orslen) == 0 || PerlIO_error(fp))
a0d0e21e
LW
404 goto just_say_no;
405
406 if (IoFLAGS(io) & IOf_FLUSH)
760ac839 407 if (PerlIO_flush(fp) == EOF)
a0d0e21e
LW
408 goto just_say_no;
409 }
410 }
411 SP = ORIGMARK;
3280af22 412 PUSHs(&PL_sv_yes);
a0d0e21e
LW
413 RETURN;
414
415 just_say_no:
416 SP = ORIGMARK;
3280af22 417 PUSHs(&PL_sv_undef);
a0d0e21e
LW
418 RETURN;
419}
420
421PP(pp_rv2av)
422{
f5284f61 423 djSP; dTOPss;
a0d0e21e
LW
424 AV *av;
425
426 if (SvROK(sv)) {
427 wasref:
f5284f61
IZ
428 tryAMAGICunDEREF(to_av);
429
a0d0e21e
LW
430 av = (AV*)SvRV(sv);
431 if (SvTYPE(av) != SVt_PVAV)
432 DIE("Not an ARRAY reference");
533c011a 433 if (PL_op->op_flags & OPf_REF) {
f5284f61 434 SETs((SV*)av);
a0d0e21e
LW
435 RETURN;
436 }
437 }
438 else {
439 if (SvTYPE(sv) == SVt_PVAV) {
440 av = (AV*)sv;
533c011a 441 if (PL_op->op_flags & OPf_REF) {
f5284f61 442 SETs((SV*)av);
a0d0e21e
LW
443 RETURN;
444 }
445 }
446 else {
67955e0c
PP
447 GV *gv;
448
a0d0e21e 449 if (SvTYPE(sv) != SVt_PVGV) {
748a9306 450 char *sym;
2d8e6c8d 451 STRLEN n_a;
748a9306 452
a0d0e21e
LW
453 if (SvGMAGICAL(sv)) {
454 mg_get(sv);
455 if (SvROK(sv))
456 goto wasref;
457 }
458 if (!SvOK(sv)) {
533c011a
NIS
459 if (PL_op->op_flags & OPf_REF ||
460 PL_op->op_private & HINT_STRICT_REFS)
22c35a8c 461 DIE(PL_no_usym, "an ARRAY");
599cee73 462 if (ckWARN(WARN_UNINITIALIZED))
22c35a8c 463 warner(WARN_UNINITIALIZED, PL_warn_uninit);
f5284f61 464 if (GIMME == G_ARRAY) {
c2444246 465 (void)POPs;
4633a7c4 466 RETURN;
f5284f61
IZ
467 }
468 RETSETUNDEF;
a0d0e21e 469 }
2d8e6c8d 470 sym = SvPV(sv,n_a);
35cd451c
GS
471 if ((PL_op->op_flags & OPf_SPECIAL) &&
472 !(PL_op->op_flags & OPf_MOD))
473 {
474 gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PVAV);
475 if (!gv)
476 RETSETUNDEF;
477 }
478 else {
479 if (PL_op->op_private & HINT_STRICT_REFS)
480 DIE(PL_no_symref, sym, "an ARRAY");
481 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVAV);
482 }
483 }
484 else {
67955e0c 485 gv = (GV*)sv;
a0d0e21e 486 }
67955e0c 487 av = GvAVn(gv);
533c011a 488 if (PL_op->op_private & OPpLVAL_INTRO)
67955e0c 489 av = save_ary(gv);
533c011a 490 if (PL_op->op_flags & OPf_REF) {
f5284f61 491 SETs((SV*)av);
a0d0e21e
LW
492 RETURN;
493 }
494 }
495 }
496
497 if (GIMME == G_ARRAY) {
498 I32 maxarg = AvFILL(av) + 1;
c2444246 499 (void)POPs; /* XXXX May be optimized away? */
93965878
NIS
500 EXTEND(SP, maxarg);
501 if (SvRMAGICAL(av)) {
502 U32 i;
503 for (i=0; i < maxarg; i++) {
504 SV **svp = av_fetch(av, i, FALSE);
3280af22 505 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
93965878
NIS
506 }
507 }
508 else {
509 Copy(AvARRAY(av), SP+1, maxarg, SV*);
510 }
a0d0e21e
LW
511 SP += maxarg;
512 }
513 else {
514 dTARGET;
515 I32 maxarg = AvFILL(av) + 1;
f5284f61 516 SETi(maxarg);
a0d0e21e
LW
517 }
518 RETURN;
519}
520
521PP(pp_rv2hv)
522{
4e35701f 523 djSP; dTOPss;
a0d0e21e
LW
524 HV *hv;
525
526 if (SvROK(sv)) {
527 wasref:
f5284f61
IZ
528 tryAMAGICunDEREF(to_hv);
529
a0d0e21e 530 hv = (HV*)SvRV(sv);
c750a3ec 531 if (SvTYPE(hv) != SVt_PVHV && SvTYPE(hv) != SVt_PVAV)
a0d0e21e 532 DIE("Not a HASH reference");
533c011a 533 if (PL_op->op_flags & OPf_REF) {
a0d0e21e
LW
534 SETs((SV*)hv);
535 RETURN;
536 }
537 }
538 else {
c750a3ec 539 if (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV) {
a0d0e21e 540 hv = (HV*)sv;
533c011a 541 if (PL_op->op_flags & OPf_REF) {
a0d0e21e
LW
542 SETs((SV*)hv);
543 RETURN;
544 }
545 }
546 else {
67955e0c
PP
547 GV *gv;
548
a0d0e21e 549 if (SvTYPE(sv) != SVt_PVGV) {
748a9306 550 char *sym;
2d8e6c8d 551 STRLEN n_a;
748a9306 552
a0d0e21e
LW
553 if (SvGMAGICAL(sv)) {
554 mg_get(sv);
555 if (SvROK(sv))
556 goto wasref;
557 }
558 if (!SvOK(sv)) {
533c011a
NIS
559 if (PL_op->op_flags & OPf_REF ||
560 PL_op->op_private & HINT_STRICT_REFS)
22c35a8c 561 DIE(PL_no_usym, "a HASH");
599cee73 562 if (ckWARN(WARN_UNINITIALIZED))
22c35a8c 563 warner(WARN_UNINITIALIZED, PL_warn_uninit);
4633a7c4
LW
564 if (GIMME == G_ARRAY) {
565 SP--;
566 RETURN;
567 }
a0d0e21e
LW
568 RETSETUNDEF;
569 }
2d8e6c8d 570 sym = SvPV(sv,n_a);
35cd451c
GS
571 if ((PL_op->op_flags & OPf_SPECIAL) &&
572 !(PL_op->op_flags & OPf_MOD))
573 {
574 gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PVHV);
575 if (!gv)
576 RETSETUNDEF;
577 }
578 else {
579 if (PL_op->op_private & HINT_STRICT_REFS)
580 DIE(PL_no_symref, sym, "a HASH");
581 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVHV);
582 }
583 }
584 else {
67955e0c 585 gv = (GV*)sv;
a0d0e21e 586 }
67955e0c 587 hv = GvHVn(gv);
533c011a 588 if (PL_op->op_private & OPpLVAL_INTRO)
67955e0c 589 hv = save_hash(gv);
533c011a 590 if (PL_op->op_flags & OPf_REF) {
a0d0e21e
LW
591 SETs((SV*)hv);
592 RETURN;
593 }
594 }
595 }
596
597 if (GIMME == G_ARRAY) { /* array wanted */
3280af22 598 *PL_stack_sp = (SV*)hv;
a0d0e21e
LW
599 return do_kv(ARGS);
600 }
601 else {
602 dTARGET;
4b154ab5
GA
603 if (SvTYPE(hv) == SVt_PVAV)
604 hv = avhv_keys((AV*)hv);
46fc3d4c
PP
605 if (HvFILL(hv))
606 sv_setpvf(TARG, "%ld/%ld",
607 (long)HvFILL(hv), (long)HvMAX(hv) + 1);
a0d0e21e
LW
608 else
609 sv_setiv(TARG, 0);
c750a3ec 610
a0d0e21e
LW
611 SETTARG;
612 RETURN;
613 }
614}
615
616PP(pp_aassign)
617{
4e35701f 618 djSP;
3280af22
NIS
619 SV **lastlelem = PL_stack_sp;
620 SV **lastrelem = PL_stack_base + POPMARK;
621 SV **firstrelem = PL_stack_base + POPMARK + 1;
a0d0e21e
LW
622 SV **firstlelem = lastrelem + 1;
623
624 register SV **relem;
625 register SV **lelem;
626
627 register SV *sv;
628 register AV *ary;
629
54310121 630 I32 gimme;
a0d0e21e
LW
631 HV *hash;
632 I32 i;
633 int magic;
634
3280af22 635 PL_delaymagic = DM_DELAY; /* catch simultaneous items */
a0d0e21e
LW
636
637 /* If there's a common identifier on both sides we have to take
638 * special care that assigning the identifier on the left doesn't
639 * clobber a value on the right that's used later in the list.
640 */
533c011a 641 if (PL_op->op_private & OPpASSIGN_COMMON) {
cc5e57d2 642 EXTEND_MORTAL(lastrelem - firstrelem + 1);
a0d0e21e
LW
643 for (relem = firstrelem; relem <= lastrelem; relem++) {
644 /*SUPPRESS 560*/
a1f49e72
CS
645 if (sv = *relem) {
646 TAINT_NOT; /* Each item is independent */
a0d0e21e 647 *relem = sv_mortalcopy(sv);
a1f49e72 648 }
a0d0e21e
LW
649 }
650 }
651
652 relem = firstrelem;
653 lelem = firstlelem;
654 ary = Null(AV*);
655 hash = Null(HV*);
656 while (lelem <= lastlelem) {
bbce6d69 657 TAINT_NOT; /* Each item stands on its own, taintwise. */
a0d0e21e
LW
658 sv = *lelem++;
659 switch (SvTYPE(sv)) {
660 case SVt_PVAV:
661 ary = (AV*)sv;
748a9306 662 magic = SvMAGICAL(ary) != 0;
a0d0e21e
LW
663
664 av_clear(ary);
7e42bd57 665 av_extend(ary, lastrelem - relem);
a0d0e21e
LW
666 i = 0;
667 while (relem <= lastrelem) { /* gobble up all the rest */
5117ca91 668 SV **didstore;
a0d0e21e
LW
669 sv = NEWSV(28,0);
670 assert(*relem);
671 sv_setsv(sv,*relem);
672 *(relem++) = sv;
5117ca91
GS
673 didstore = av_store(ary,i++,sv);
674 if (magic) {
fb73857a
PP
675 if (SvSMAGICAL(sv))
676 mg_set(sv);
5117ca91 677 if (!didstore)
8127e0e3 678 sv_2mortal(sv);
5117ca91 679 }
bbce6d69 680 TAINT_NOT;
a0d0e21e
LW
681 }
682 break;
683 case SVt_PVHV: {
a0d0e21e
LW
684 SV *tmpstr;
685
686 hash = (HV*)sv;
748a9306 687 magic = SvMAGICAL(hash) != 0;
a0d0e21e
LW
688 hv_clear(hash);
689
690 while (relem < lastrelem) { /* gobble up all the rest */
5117ca91 691 HE *didstore;
4633a7c4 692 if (*relem)
a0d0e21e 693 sv = *(relem++);
4633a7c4 694 else
3280af22 695 sv = &PL_sv_no, relem++;
a0d0e21e
LW
696 tmpstr = NEWSV(29,0);
697 if (*relem)
698 sv_setsv(tmpstr,*relem); /* value */
699 *(relem++) = tmpstr;
5117ca91
GS
700 didstore = hv_store_ent(hash,sv,tmpstr,0);
701 if (magic) {
fb73857a
PP
702 if (SvSMAGICAL(tmpstr))
703 mg_set(tmpstr);
5117ca91 704 if (!didstore)
8127e0e3 705 sv_2mortal(tmpstr);
5117ca91 706 }
bbce6d69 707 TAINT_NOT;
8e07c86e 708 }
6a0deba8
GS
709 if (relem == lastrelem) {
710 if (*relem) {
711 HE *didstore;
599cee73 712 if (ckWARN(WARN_UNSAFE)) {
6a0deba8
GS
713 if (relem == firstrelem &&
714 SvROK(*relem) &&
715 ( SvTYPE(SvRV(*relem)) == SVt_PVAV ||
716 SvTYPE(SvRV(*relem)) == SVt_PVHV ) )
599cee73 717 warner(WARN_UNSAFE, "Reference found where even-sized list expected");
6a0deba8 718 else
599cee73 719 warner(WARN_UNSAFE, "Odd number of elements in hash assignment");
6a0deba8
GS
720 }
721 tmpstr = NEWSV(29,0);
722 didstore = hv_store_ent(hash,*relem,tmpstr,0);
723 if (magic) {
724 if (SvSMAGICAL(tmpstr))
725 mg_set(tmpstr);
726 if (!didstore)
8127e0e3 727 sv_2mortal(tmpstr);
6a0deba8
GS
728 }
729 TAINT_NOT;
730 }
731 relem++;
1930e939 732 }
a0d0e21e
LW
733 }
734 break;
735 default:
736 if (SvTHINKFIRST(sv)) {
3280af22 737 if (SvREADONLY(sv) && PL_curcop != &PL_compiling) {
d689ffdd 738 if (!SvIMMORTAL(sv))
22c35a8c 739 DIE(PL_no_modify);
a0d0e21e
LW
740 if (relem <= lastrelem)
741 relem++;
742 break;
743 }
744 if (SvROK(sv))
745 sv_unref(sv);
746 }
747 if (relem <= lastrelem) {
748 sv_setsv(sv, *relem);
749 *(relem++) = sv;
750 }
751 else
3280af22 752 sv_setsv(sv, &PL_sv_undef);
a0d0e21e
LW
753 SvSETMAGIC(sv);
754 break;
755 }
756 }
3280af22
NIS
757 if (PL_delaymagic & ~DM_DELAY) {
758 if (PL_delaymagic & DM_UID) {
a0d0e21e 759#ifdef HAS_SETRESUID
b28d0864 760 (void)setresuid(PL_uid,PL_euid,(Uid_t)-1);
56febc5e
AD
761#else
762# ifdef HAS_SETREUID
3280af22 763 (void)setreuid(PL_uid,PL_euid);
56febc5e
AD
764# else
765# ifdef HAS_SETRUID
b28d0864
NIS
766 if ((PL_delaymagic & DM_UID) == DM_RUID) {
767 (void)setruid(PL_uid);
768 PL_delaymagic &= ~DM_RUID;
a0d0e21e 769 }
56febc5e
AD
770# endif /* HAS_SETRUID */
771# ifdef HAS_SETEUID
b28d0864
NIS
772 if ((PL_delaymagic & DM_UID) == DM_EUID) {
773 (void)seteuid(PL_uid);
774 PL_delaymagic &= ~DM_EUID;
a0d0e21e 775 }
56febc5e 776# endif /* HAS_SETEUID */
b28d0864
NIS
777 if (PL_delaymagic & DM_UID) {
778 if (PL_uid != PL_euid)
a0d0e21e 779 DIE("No setreuid available");
b28d0864 780 (void)PerlProc_setuid(PL_uid);
a0d0e21e 781 }
56febc5e
AD
782# endif /* HAS_SETREUID */
783#endif /* HAS_SETRESUID */
3280af22
NIS
784 PL_uid = (int)PerlProc_getuid();
785 PL_euid = (int)PerlProc_geteuid();
a0d0e21e 786 }
3280af22 787 if (PL_delaymagic & DM_GID) {
a0d0e21e 788#ifdef HAS_SETRESGID
b28d0864 789 (void)setresgid(PL_gid,PL_egid,(Gid_t)-1);
56febc5e
AD
790#else
791# ifdef HAS_SETREGID
3280af22 792 (void)setregid(PL_gid,PL_egid);
56febc5e
AD
793# else
794# ifdef HAS_SETRGID
b28d0864
NIS
795 if ((PL_delaymagic & DM_GID) == DM_RGID) {
796 (void)setrgid(PL_gid);
797 PL_delaymagic &= ~DM_RGID;
a0d0e21e 798 }
56febc5e
AD
799# endif /* HAS_SETRGID */
800# ifdef HAS_SETEGID
b28d0864
NIS
801 if ((PL_delaymagic & DM_GID) == DM_EGID) {
802 (void)setegid(PL_gid);
803 PL_delaymagic &= ~DM_EGID;
a0d0e21e 804 }
56febc5e 805# endif /* HAS_SETEGID */
b28d0864
NIS
806 if (PL_delaymagic & DM_GID) {
807 if (PL_gid != PL_egid)
a0d0e21e 808 DIE("No setregid available");
b28d0864 809 (void)PerlProc_setgid(PL_gid);
a0d0e21e 810 }
56febc5e
AD
811# endif /* HAS_SETREGID */
812#endif /* HAS_SETRESGID */
3280af22
NIS
813 PL_gid = (int)PerlProc_getgid();
814 PL_egid = (int)PerlProc_getegid();
a0d0e21e 815 }
3280af22 816 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
a0d0e21e 817 }
3280af22 818 PL_delaymagic = 0;
54310121
PP
819
820 gimme = GIMME_V;
821 if (gimme == G_VOID)
822 SP = firstrelem - 1;
823 else if (gimme == G_SCALAR) {
824 dTARGET;
825 SP = firstrelem;
826 SETi(lastrelem - firstrelem + 1);
827 }
828 else {
a0d0e21e
LW
829 if (ary || hash)
830 SP = lastrelem;
831 else
832 SP = firstrelem + (lastlelem - firstlelem);
0c8c7a05 833 lelem = firstlelem + (relem - firstrelem);
5f05dabc 834 while (relem <= SP)
3280af22 835 *relem++ = (lelem <= lastlelem) ? *lelem++ : &PL_sv_undef;
a0d0e21e 836 }
54310121 837 RETURN;
a0d0e21e
LW
838}
839
8782bef2
GB
840PP(pp_qr)
841{
842 djSP;
843 register PMOP *pm = cPMOP;
844 SV *rv = sv_newmortal();
57668c4d 845 SV *sv = newSVrv(rv, "Regexp");
8782bef2
GB
846 sv_magic(sv,(SV*)ReREFCNT_inc(pm->op_pmregexp),'r',0,0);
847 RETURNX(PUSHs(rv));
848}
849
a0d0e21e
LW
850PP(pp_match)
851{
4e35701f 852 djSP; dTARG;
a0d0e21e
LW
853 register PMOP *pm = cPMOP;
854 register char *t;
855 register char *s;
856 char *strend;
857 I32 global;
22e551b9 858 I32 r_flags;
a0d0e21e 859 char *truebase;
d9f97599 860 register REGEXP *rx = pm->op_pmregexp;
b3eb6a9b 861 bool rxtainted;
a0d0e21e
LW
862 I32 gimme = GIMME;
863 STRLEN len;
748a9306 864 I32 minmatch = 0;
3280af22 865 I32 oldsave = PL_savestack_ix;
f86702cc 866 I32 update_minmatch = 1;
e60df1fa 867 I32 had_zerolen = 0;
a0d0e21e 868
533c011a 869 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e
LW
870 TARG = POPs;
871 else {
54b9620d 872 TARG = DEFSV;
a0d0e21e
LW
873 EXTEND(SP,1);
874 }
c277df42 875 PUTBACK; /* EVAL blocks need stack_sp. */
a0d0e21e
LW
876 s = SvPV(TARG, len);
877 strend = s + len;
878 if (!s)
879 DIE("panic: do_match");
b3eb6a9b 880 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
3280af22 881 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
9212bbba 882 TAINT_NOT;
a0d0e21e 883
48c036b1 884 if (pm->op_pmdynflags & PMdf_USED) {
c277df42 885 failure:
a0d0e21e
LW
886 if (gimme == G_ARRAY)
887 RETURN;
888 RETPUSHNO;
889 }
890
3280af22
NIS
891 if (!rx->prelen && PL_curpm) {
892 pm = PL_curpm;
d9f97599 893 rx = pm->op_pmregexp;
a0d0e21e 894 }
d9f97599 895 if (rx->minlen > len) goto failure;
c277df42 896
a0d0e21e 897 truebase = t = s;
ad94a511
IZ
898
899 /* XXXX What part of this is needed with true \G-support? */
a0d0e21e 900 if (global = pm->op_pmflags & PMf_GLOBAL) {
d9f97599 901 rx->startp[0] = 0;
a0d0e21e
LW
902 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
903 MAGIC* mg = mg_find(TARG, 'g');
565764a8 904 if (mg && mg->mg_len >= 0) {
b7a35066
IZ
905 if (!(rx->reganch & ROPT_GPOS_SEEN))
906 rx->endp[0] = rx->startp[0] = s + mg->mg_len;
748a9306 907 minmatch = (mg->mg_flags & MGf_MINMATCH);
f86702cc 908 update_minmatch = 0;
748a9306 909 }
a0d0e21e
LW
910 }
911 }
22e551b9 912 r_flags = ((gimme != G_ARRAY && !global && rx->nparens)
9d080a66
GS
913 || SvTEMP(TARG) || PL_sawampersand)
914 ? REXEC_COPY_STR : 0;
22e551b9
IZ
915 if (SvSCREAM(TARG) && rx->check_substr
916 && SvTYPE(rx->check_substr) == SVt_PVBM
917 && SvVALID(rx->check_substr))
918 r_flags |= REXEC_SCREAM;
919
a0d0e21e 920 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
3280af22
NIS
921 SAVEINT(PL_multiline);
922 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
a0d0e21e
LW
923 }
924
925play_it_again:
d9f97599
GS
926 if (global && rx->startp[0]) {
927 t = s = rx->endp[0];
928 if ((s + rx->minlen) > strend)
a0d0e21e 929 goto nope;
f86702cc 930 if (update_minmatch++)
e60df1fa 931 minmatch = had_zerolen;
a0d0e21e 932 }
d9f97599
GS
933 if (rx->check_substr) {
934 if (!(rx->reganch & ROPT_NOSCAN)) { /* Floating checkstring. */
22e551b9 935 if (r_flags & REXEC_SCREAM) {
c277df42 936 I32 p = -1;
a0ed51b3 937 char *b;
c277df42 938
3280af22 939 if (PL_screamfirst[BmRARE(rx->check_substr)] < 0)
a0d0e21e 940 goto nope;
a0ed51b3 941
dfe13c55 942 b = (char*)HOP((U8*)s, rx->check_offset_min);
a0ed51b3 943 if (!(s = screaminstr(TARG, rx->check_substr, b - s, 0, &p, 0)))
a0d0e21e 944 goto nope;
a0ed51b3
LW
945
946 if ((rx->reganch & ROPT_CHECK_ALL)
3280af22 947 && !PL_sawampersand && !SvTAIL(rx->check_substr))
a0d0e21e
LW
948 goto yup;
949 }
a0ed51b3 950 else if (!(s = fbm_instr((unsigned char*)HOP((U8*)s, rx->check_offset_min),
c277df42 951 (unsigned char*)strend,
411d5715 952 rx->check_substr, 0)))
a0d0e21e 953 goto nope;
3280af22 954 else if ((rx->reganch & ROPT_CHECK_ALL) && !PL_sawampersand)
a0d0e21e 955 goto yup;
ee027f1d 956 if (s && rx->check_offset_max < s - t) {
d9f97599 957 ++BmUSEFUL(rx->check_substr);
dfe13c55 958 s = (char*)HOP((U8*)s, -rx->check_offset_max);
a0d0e21e
LW
959 }
960 else
961 s = t;
962 }
c277df42
IZ
963 /* Now checkstring is fixed, i.e. at fixed offset from the
964 beginning of match, and the match is anchored at s. */
3280af22 965 else if (!PL_multiline) { /* Anchored near beginning of string. */
c277df42 966 I32 slen;
dfe13c55 967 char *b = (char*)HOP((U8*)s, rx->check_offset_min);
a0ed51b3 968 if (*SvPVX(rx->check_substr) != *b
d9f97599 969 || ((slen = SvCUR(rx->check_substr)) > 1
a0ed51b3 970 && memNE(SvPVX(rx->check_substr), b, slen)))
bbce6d69 971 goto nope;
a0d0e21e 972 }
a0ed51b3 973 if (!(rx->reganch & ROPT_NAUGHTY) && --BmUSEFUL(rx->check_substr) < 0
d9f97599
GS
974 && rx->check_substr == rx->float_substr) {
975 SvREFCNT_dec(rx->check_substr);
976 rx->check_substr = Nullsv; /* opt is being useless */
977 rx->float_substr = Nullsv;
a0d0e21e
LW
978 }
979 }
22e551b9 980 if (CALLREGEXEC(rx, s, strend, truebase, minmatch, TARG, NULL, r_flags))
bbce6d69 981 {
3280af22 982 PL_curpm = pm;
a0d0e21e 983 if (pm->op_pmflags & PMf_ONCE)
48c036b1 984 pm->op_pmdynflags |= PMdf_USED;
a0d0e21e
LW
985 goto gotcha;
986 }
987 else
988 goto ret_no;
989 /*NOTREACHED*/
990
991 gotcha:
72311751
GS
992 if (rxtainted)
993 RX_MATCH_TAINTED_on(rx);
994 TAINT_IF(RX_MATCH_TAINTED(rx));
a0d0e21e
LW
995 if (gimme == G_ARRAY) {
996 I32 iters, i, len;
997
d9f97599 998 iters = rx->nparens;
a0d0e21e
LW
999 if (global && !iters)
1000 i = 1;
1001 else
1002 i = 0;
c277df42 1003 SPAGAIN; /* EVAL blocks could move the stack. */
a0d0e21e 1004 EXTEND(SP, iters + i);
bbce6d69 1005 EXTEND_MORTAL(iters + i);
a0d0e21e
LW
1006 for (i = !i; i <= iters; i++) {
1007 PUSHs(sv_newmortal());
1008 /*SUPPRESS 560*/
d9f97599
GS
1009 if ((s = rx->startp[i]) && rx->endp[i] ) {
1010 len = rx->endp[i] - s;
a0d0e21e
LW
1011 sv_setpvn(*SP, s, len);
1012 }
1013 }
1014 if (global) {
d9f97599
GS
1015 truebase = rx->subbeg;
1016 strend = rx->subend;
e60df1fa 1017 had_zerolen = (rx->startp[0] && rx->startp[0] == rx->endp[0]);
c277df42 1018 PUTBACK; /* EVAL blocks may use stack */
ad94a511 1019 r_flags |= REXEC_IGNOREPOS;
a0d0e21e
LW
1020 goto play_it_again;
1021 }
f7e33566 1022 else if (!iters)
bde848c5 1023 XPUSHs(&PL_sv_yes);
4633a7c4 1024 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1025 RETURN;
1026 }
1027 else {
1028 if (global) {
1029 MAGIC* mg = 0;
1030 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
1031 mg = mg_find(TARG, 'g');
1032 if (!mg) {
1033 sv_magic(TARG, (SV*)0, 'g', Nullch, 0);
1034 mg = mg_find(TARG, 'g');
1035 }
d9f97599
GS
1036 if (rx->startp[0]) {
1037 mg->mg_len = rx->endp[0] - rx->subbeg;
1038 if (rx->startp[0] == rx->endp[0])
748a9306
LW
1039 mg->mg_flags |= MGf_MINMATCH;
1040 else
1041 mg->mg_flags &= ~MGf_MINMATCH;
1042 }
a0d0e21e 1043 }
4633a7c4 1044 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1045 RETPUSHYES;
1046 }
1047
c277df42 1048yup: /* Confirmed by check_substr */
72311751
GS
1049 if (rxtainted)
1050 RX_MATCH_TAINTED_on(rx);
1051 TAINT_IF(RX_MATCH_TAINTED(rx));
d9f97599 1052 ++BmUSEFUL(rx->check_substr);
3280af22 1053 PL_curpm = pm;
a0d0e21e 1054 if (pm->op_pmflags & PMf_ONCE)
48c036b1 1055 pm->op_pmdynflags |= PMdf_USED;
d9f97599
GS
1056 Safefree(rx->subbase);
1057 rx->subbase = Nullch;
a0d0e21e 1058 if (global) {
d9f97599
GS
1059 rx->subbeg = truebase;
1060 rx->subend = strend;
1061 rx->startp[0] = s;
1062 rx->endp[0] = s + SvCUR(rx->check_substr);
a0d0e21e
LW
1063 goto gotcha;
1064 }
3280af22 1065 if (PL_sawampersand) {
a0d0e21e
LW
1066 char *tmps;
1067
d9f97599
GS
1068 tmps = rx->subbase = savepvn(t, strend-t);
1069 rx->subbeg = tmps;
1070 rx->subend = tmps + (strend-t);
1071 tmps = rx->startp[0] = tmps + (s - t);
1072 rx->endp[0] = tmps + SvCUR(rx->check_substr);
a0d0e21e 1073 }
4633a7c4 1074 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1075 RETPUSHYES;
1076
1077nope:
d9f97599
GS
1078 if (rx->check_substr)
1079 ++BmUSEFUL(rx->check_substr);
a0d0e21e
LW
1080
1081ret_no:
c90c0ff4 1082 if (global && !(pm->op_pmflags & PMf_CONTINUE)) {
a0d0e21e
LW
1083 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
1084 MAGIC* mg = mg_find(TARG, 'g');
1085 if (mg)
565764a8 1086 mg->mg_len = -1;
a0d0e21e
LW
1087 }
1088 }
4633a7c4 1089 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1090 if (gimme == G_ARRAY)
1091 RETURN;
1092 RETPUSHNO;
1093}
1094
1095OP *
8ac85365 1096do_readline(void)
a0d0e21e
LW
1097{
1098 dSP; dTARGETSTACKED;
1099 register SV *sv;
1100 STRLEN tmplen = 0;
1101 STRLEN offset;
760ac839 1102 PerlIO *fp;
3280af22 1103 register IO *io = GvIO(PL_last_in_gv);
533c011a 1104 register I32 type = PL_op->op_type;
54310121 1105 I32 gimme = GIMME_V;
e79b0511 1106 MAGIC *mg;
a0d0e21e 1107
33c27489 1108 if (mg = SvTIED_mg((SV*)PL_last_in_gv, 'q')) {
e79b0511 1109 PUSHMARK(SP);
33c27489 1110 XPUSHs(SvTIED_obj((SV*)PL_last_in_gv, mg));
e79b0511
PP
1111 PUTBACK;
1112 ENTER;
54310121 1113 perl_call_method("READLINE", gimme);
e79b0511
PP
1114 LEAVE;
1115 SPAGAIN;
54310121
PP
1116 if (gimme == G_SCALAR)
1117 SvSetMagicSV_nosteal(TARG, TOPs);
e79b0511
PP
1118 RETURN;
1119 }
a0d0e21e
LW
1120 fp = Nullfp;
1121 if (io) {
1122 fp = IoIFP(io);
1123 if (!fp) {
1124 if (IoFLAGS(io) & IOf_ARGV) {
1125 if (IoFLAGS(io) & IOf_START) {
1126 IoFLAGS(io) &= ~IOf_START;
1127 IoLINES(io) = 0;
3280af22 1128 if (av_len(GvAVn(PL_last_in_gv)) < 0) {
9d116dd7 1129 do_open(PL_last_in_gv,"-",1,FALSE,O_RDONLY,0,Nullfp);
3280af22
NIS
1130 sv_setpvn(GvSV(PL_last_in_gv), "-", 1);
1131 SvSETMAGIC(GvSV(PL_last_in_gv));
a2008d6d
GS
1132 fp = IoIFP(io);
1133 goto have_fp;
a0d0e21e
LW
1134 }
1135 }
3280af22 1136 fp = nextargv(PL_last_in_gv);
a0d0e21e 1137 if (!fp) { /* Note: fp != IoIFP(io) */
3280af22 1138 (void)do_close(PL_last_in_gv, FALSE); /* now it does*/
a0d0e21e
LW
1139 IoFLAGS(io) |= IOf_START;
1140 }
1141 }
1142 else if (type == OP_GLOB) {
1143 SV *tmpcmd = NEWSV(55, 0);
1144 SV *tmpglob = POPs;
1145 ENTER;
1146 SAVEFREESV(tmpcmd);
1147#ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
1148 /* since spawning off a process is a real performance hit */
1149 {
1150#include <descrip.h>
1151#include <lib$routines.h>
1152#include <nam.h>
1153#include <rmsdef.h>
1154 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
1155 char vmsspec[NAM$C_MAXRSS+1];
1156 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
1157 char tmpfnam[L_tmpnam] = "SYS$SCRATCH:";
1158 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
760ac839 1159 PerlIO *tmpfp;
a0d0e21e
LW
1160 STRLEN i;
1161 struct dsc$descriptor_s wilddsc
1162 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
1163 struct dsc$descriptor_vs rsdsc
1164 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
1165 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
1166
1167 /* We could find out if there's an explicit dev/dir or version
1168 by peeking into lib$find_file's internal context at
1169 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
1170 but that's unsupported, so I don't want to do it now and
1171 have it bite someone in the future. */
6ad3d225 1172 strcat(tmpfnam,PerlLIO_tmpnam(NULL));
a0d0e21e
LW
1173 cp = SvPV(tmpglob,i);
1174 for (; i; i--) {
1175 if (cp[i] == ';') hasver = 1;
1176 if (cp[i] == '.') {
1177 if (sts) hasver = 1;
1178 else sts = 1;
1179 }
1180 if (cp[i] == '/') {
1181 hasdir = isunix = 1;
1182 break;
748a9306 1183 }
a0d0e21e
LW
1184 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
1185 hasdir = 1;
1186 break;
1187 }
1188 }
760ac839 1189 if ((tmpfp = PerlIO_open(tmpfnam,"w+","fop=dlt")) != NULL) {
09b7f37c 1190 Stat_t st;
c6ed36e1 1191 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
09b7f37c
CB
1192 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
1193 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
a0d0e21e
LW
1194 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
1195 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
1196 &dfltdsc,NULL,NULL,NULL))&1)) {
1197 end = rstr + (unsigned long int) *rslt;
1198 if (!hasver) while (*end != ';') end--;
1199 *(end++) = '\n'; *end = '\0';
1200 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
1201 if (hasdir) {
f86702cc 1202 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
a0d0e21e
LW
1203 begin = rstr;
1204 }
1205 else {
1206 begin = end;
1207 while (*(--begin) != ']' && *begin != '>') ;
1208 ++begin;
1209 }
760ac839 1210 ok = (PerlIO_puts(tmpfp,begin) != EOF);
a0d0e21e
LW
1211 }
1212 if (cxt) (void)lib$find_file_end(&cxt);
748a9306
LW
1213 if (ok && sts != RMS$_NMF &&
1214 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
a0d0e21e 1215 if (!ok) {
c07a80fd
PP
1216 if (!(sts & 1)) {
1217 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
1218 }
760ac839 1219 PerlIO_close(tmpfp);
a0d0e21e
LW
1220 fp = NULL;
1221 }
1222 else {
760ac839 1223 PerlIO_rewind(tmpfp);
a0d0e21e
LW
1224 IoTYPE(io) = '<';
1225 IoIFP(io) = fp = tmpfp;
1e422769 1226 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
a0d0e21e
LW
1227 }
1228 }
1229 }
1230#else /* !VMS */
1231#ifdef DOSISH
67955e0c
PP
1232#ifdef OS2
1233 sv_setpv(tmpcmd, "for a in ");
1234 sv_catsv(tmpcmd, tmpglob);
1235 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
1236#else
39e571d4
ML
1237#ifdef DJGPP
1238 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
1239 sv_catsv(tmpcmd, tmpglob);
1240#else
a0d0e21e
LW
1241 sv_setpv(tmpcmd, "perlglob ");
1242 sv_catsv(tmpcmd, tmpglob);
1243 sv_catpv(tmpcmd, " |");
39e571d4 1244#endif /* !DJGPP */
67955e0c
PP
1245#endif /* !OS2 */
1246#else /* !DOSISH */
1247#if defined(CSH)
3280af22 1248 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
a0d0e21e
LW
1249 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
1250 sv_catsv(tmpcmd, tmpglob);
16d20bd9 1251 sv_catpv(tmpcmd, "' 2>/dev/null |");
a0d0e21e
LW
1252#else
1253 sv_setpv(tmpcmd, "echo ");
1254 sv_catsv(tmpcmd, tmpglob);
1255#if 'z' - 'a' == 25
1256 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
1257#else
1258 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
1259#endif
1260#endif /* !CSH */
67955e0c 1261#endif /* !DOSISH */
3280af22 1262 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
9d116dd7 1263 FALSE, O_RDONLY, 0, Nullfp);
a0d0e21e
LW
1264 fp = IoIFP(io);
1265#endif /* !VMS */
1266 LEAVE;
1267 }
1268 }
1269 else if (type == OP_GLOB)
1270 SP--;
1271 }
1272 if (!fp) {
599cee73
PM
1273 if (ckWARN(WARN_CLOSED) && io && !(IoFLAGS(io) & IOf_START))
1274 warner(WARN_CLOSED,
1275 "Read on closed filehandle <%s>", GvENAME(PL_last_in_gv));
54310121 1276 if (gimme == G_SCALAR) {
a0d0e21e
LW
1277 (void)SvOK_off(TARG);
1278 PUSHTARG;
1279 }
1280 RETURN;
1281 }
a2008d6d 1282 have_fp:
54310121 1283 if (gimme == G_SCALAR) {
a0d0e21e 1284 sv = TARG;
9607fc9c
PP
1285 if (SvROK(sv))
1286 sv_unref(sv);
a0d0e21e
LW
1287 (void)SvUPGRADE(sv, SVt_PV);
1288 tmplen = SvLEN(sv); /* remember if already alloced */
1289 if (!tmplen)
1290 Sv_Grow(sv, 80); /* try short-buffering it */
1291 if (type == OP_RCATLINE)
1292 offset = SvCUR(sv);
1293 else
1294 offset = 0;
1295 }
54310121
PP
1296 else {
1297 sv = sv_2mortal(NEWSV(57, 80));
1298 offset = 0;
1299 }
a0d0e21e
LW
1300 for (;;) {
1301 if (!sv_gets(sv, fp, offset)) {
760ac839 1302 PerlIO_clearerr(fp);
a0d0e21e 1303 if (IoFLAGS(io) & IOf_ARGV) {
3280af22 1304 fp = nextargv(PL_last_in_gv);
a0d0e21e
LW
1305 if (fp)
1306 continue;
3280af22 1307 (void)do_close(PL_last_in_gv, FALSE);
a0d0e21e
LW
1308 IoFLAGS(io) |= IOf_START;
1309 }
1310 else if (type == OP_GLOB) {
4eb79ab5
GS
1311 if (!do_close(PL_last_in_gv, FALSE) && ckWARN(WARN_CLOSED)) {
1312 warner(WARN_CLOSED,
1313 "glob failed (child exited with status %d%s)",
1314 STATUS_CURRENT >> 8,
cf494569 1315 (STATUS_CURRENT & 0x80) ? ", core dumped" : "");
4eb79ab5 1316 }
a0d0e21e 1317 }
54310121 1318 if (gimme == G_SCALAR) {
a0d0e21e
LW
1319 (void)SvOK_off(TARG);
1320 PUSHTARG;
1321 }
1322 RETURN;
1323 }
bbce6d69
PP
1324 /* This should not be marked tainted if the fp is marked clean */
1325 if (!(IoFLAGS(io) & IOf_UNTAINT)) {
1326 TAINT;
1327 SvTAINTED_on(sv);
1328 }
a0d0e21e 1329 IoLINES(io)++;
71be2cbc 1330 SvSETMAGIC(sv);
a0d0e21e 1331 XPUSHs(sv);
a0d0e21e
LW
1332 if (type == OP_GLOB) {
1333 char *tmps;
1334
3280af22 1335 if (SvCUR(sv) > 0 && SvCUR(PL_rs) > 0) {
c07a80fd 1336 tmps = SvEND(sv) - 1;
3280af22 1337 if (*tmps == *SvPVX(PL_rs)) {
c07a80fd
PP
1338 *tmps = '\0';
1339 SvCUR(sv)--;
1340 }
1341 }
a0d0e21e
LW
1342 for (tmps = SvPVX(sv); *tmps; tmps++)
1343 if (!isALPHA(*tmps) && !isDIGIT(*tmps) &&
1344 strchr("$&*(){}[]'\";\\|?<>~`", *tmps))
1345 break;
3280af22 1346 if (*tmps && PerlLIO_stat(SvPVX(sv), &PL_statbuf) < 0) {
a0d0e21e
LW
1347 (void)POPs; /* Unmatched wildcard? Chuck it... */
1348 continue;
1349 }
1350 }
54310121 1351 if (gimme == G_ARRAY) {
a0d0e21e
LW
1352 if (SvLEN(sv) - SvCUR(sv) > 20) {
1353 SvLEN_set(sv, SvCUR(sv)+1);
1354 Renew(SvPVX(sv), SvLEN(sv), char);
1355 }
1356 sv = sv_2mortal(NEWSV(58, 80));
1357 continue;
1358 }
54310121 1359 else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
a0d0e21e
LW
1360 /* try to reclaim a bit of scalar space (only on 1st alloc) */
1361 if (SvCUR(sv) < 60)
1362 SvLEN_set(sv, 80);
1363 else
1364 SvLEN_set(sv, SvCUR(sv)+40); /* allow some slop */
1365 Renew(SvPVX(sv), SvLEN(sv), char);
1366 }
1367 RETURN;
1368 }
1369}
1370
1371PP(pp_enter)
1372{
4e35701f 1373 djSP;
c09156bb 1374 register PERL_CONTEXT *cx;
533c011a 1375 I32 gimme = OP_GIMME(PL_op, -1);
a0d0e21e 1376
54310121
PP
1377 if (gimme == -1) {
1378 if (cxstack_ix >= 0)
1379 gimme = cxstack[cxstack_ix].blk_gimme;
1380 else
1381 gimme = G_SCALAR;
1382 }
a0d0e21e
LW
1383
1384 ENTER;
1385
1386 SAVETMPS;
924508f0 1387 PUSHBLOCK(cx, CXt_BLOCK, SP);
a0d0e21e
LW
1388
1389 RETURN;
1390}
1391
1392PP(pp_helem)
1393{
4e35701f 1394 djSP;
760ac839 1395 HE* he;
ae77835f 1396 SV **svp;
a0d0e21e 1397 SV *keysv = POPs;
a0d0e21e 1398 HV *hv = (HV*)POPs;
533c011a
NIS
1399 U32 lval = PL_op->op_flags & OPf_MOD;
1400 U32 defer = PL_op->op_private & OPpLVAL_DEFER;
be6c24e0 1401 SV *sv;
a0d0e21e 1402
ae77835f
MB
1403 if (SvTYPE(hv) == SVt_PVHV) {
1404 he = hv_fetch_ent(hv, keysv, lval && !defer, 0);
97fcbf96 1405 svp = he ? &HeVAL(he) : 0;
ae77835f
MB
1406 }
1407 else if (SvTYPE(hv) == SVt_PVAV) {
0ebe0038
SM
1408 if (PL_op->op_private & OPpLVAL_INTRO)
1409 DIE("Can't localize pseudo-hash element");
97fcbf96 1410 svp = avhv_fetch_ent((AV*)hv, keysv, lval && !defer, 0);
ae77835f 1411 }
c750a3ec 1412 else {
a0d0e21e 1413 RETPUSHUNDEF;
c750a3ec 1414 }
a0d0e21e 1415 if (lval) {
3280af22 1416 if (!svp || *svp == &PL_sv_undef) {
68dc0745
PP
1417 SV* lv;
1418 SV* key2;
2d8e6c8d
GS
1419 if (!defer) {
1420 STRLEN n_a;
1421 DIE(PL_no_helem, SvPV(keysv, n_a));
1422 }
68dc0745
PP
1423 lv = sv_newmortal();
1424 sv_upgrade(lv, SVt_PVLV);
1425 LvTYPE(lv) = 'y';
1426 sv_magic(lv, key2 = newSVsv(keysv), 'y', Nullch, 0);
1427 SvREFCNT_dec(key2); /* sv_magic() increments refcount */
1428 LvTARG(lv) = SvREFCNT_inc(hv);
1429 LvTARGLEN(lv) = 1;
1430 PUSHs(lv);
1431 RETURN;
1432 }
533c011a 1433 if (PL_op->op_private & OPpLVAL_INTRO) {
ae77835f 1434 if (HvNAME(hv) && isGV(*svp))
533c011a 1435 save_gp((GV*)*svp, !(PL_op->op_flags & OPf_SPECIAL));
5f05dabc 1436 else
161b7d16 1437 save_helem(hv, keysv, svp);
5f05dabc 1438 }
533c011a
NIS
1439 else if (PL_op->op_private & OPpDEREF)
1440 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
a0d0e21e 1441 }
3280af22 1442 sv = (svp ? *svp : &PL_sv_undef);
be6c24e0
GS
1443 /* This makes C<local $tied{foo} = $tied{foo}> possible.
1444 * Pushing the magical RHS on to the stack is useless, since
1445 * that magic is soon destined to be misled by the local(),
1446 * and thus the later pp_sassign() will fail to mg_get() the
1447 * old value. This should also cure problems with delayed
1448 * mg_get()s. GSAR 98-07-03 */
1449 if (!lval && SvGMAGICAL(sv))
1450 sv = sv_mortalcopy(sv);
1451 PUSHs(sv);
a0d0e21e
LW
1452 RETURN;
1453}
1454
1455PP(pp_leave)
1456{
4e35701f 1457 djSP;
c09156bb 1458 register PERL_CONTEXT *cx;
a0d0e21e
LW
1459 register SV **mark;
1460 SV **newsp;
1461 PMOP *newpm;
1462 I32 gimme;
1463
533c011a 1464 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e 1465 cx = &cxstack[cxstack_ix];
3280af22 1466 cx->blk_oldpm = PL_curpm; /* fake block should preserve $1 et al */
a0d0e21e
LW
1467 }
1468
1469 POPBLOCK(cx,newpm);
1470
533c011a 1471 gimme = OP_GIMME(PL_op, -1);
54310121
PP
1472 if (gimme == -1) {
1473 if (cxstack_ix >= 0)
1474 gimme = cxstack[cxstack_ix].blk_gimme;
1475 else
1476 gimme = G_SCALAR;
1477 }
a0d0e21e 1478
a1f49e72 1479 TAINT_NOT;
54310121
PP
1480 if (gimme == G_VOID)
1481 SP = newsp;
1482 else if (gimme == G_SCALAR) {
1483 MARK = newsp + 1;
1484 if (MARK <= SP)
1485 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
1486 *MARK = TOPs;
1487 else
1488 *MARK = sv_mortalcopy(TOPs);
a0d0e21e 1489 else {
54310121 1490 MEXTEND(mark,0);
3280af22 1491 *MARK = &PL_sv_undef;
a0d0e21e 1492 }
54310121 1493 SP = MARK;
a0d0e21e 1494 }
54310121 1495 else if (gimme == G_ARRAY) {
a1f49e72
CS
1496 /* in case LEAVE wipes old return values */
1497 for (mark = newsp + 1; mark <= SP; mark++) {
1498 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
a0d0e21e 1499 *mark = sv_mortalcopy(*mark);
a1f49e72
CS
1500 TAINT_NOT; /* Each item is independent */
1501 }
1502 }
a0d0e21e 1503 }
3280af22 1504 PL_curpm = newpm; /* Don't pop $1 et al till now */
a0d0e21e
LW
1505
1506 LEAVE;
1507
1508 RETURN;
1509}
1510
1511PP(pp_iter)
1512{
4e35701f 1513 djSP;
c09156bb 1514 register PERL_CONTEXT *cx;
5f05dabc 1515 SV* sv;
4633a7c4 1516 AV* av;
a0d0e21e 1517
924508f0 1518 EXTEND(SP, 1);
a0d0e21e 1519 cx = &cxstack[cxstack_ix];
6b35e009 1520 if (CxTYPE(cx) != CXt_LOOP)
a0d0e21e
LW
1521 DIE("panic: pp_iter");
1522
4633a7c4 1523 av = cx->blk_loop.iterary;
89ea2908
GA
1524 if (SvTYPE(av) != SVt_PVAV) {
1525 /* iterate ($min .. $max) */
1526 if (cx->blk_loop.iterlval) {
1527 /* string increment */
1528 register SV* cur = cx->blk_loop.iterlval;
1529 STRLEN maxlen;
1530 char *max = SvPV((SV*)av, maxlen);
1531 if (!SvNIOK(cur) && SvCUR(cur) <= maxlen) {
eaa5c2d6 1532#ifndef USE_THREADS /* don't risk potential race */
39ca2bb7
SM
1533 if (SvREFCNT(*cx->blk_loop.itervar) == 1
1534 && !SvMAGICAL(*cx->blk_loop.itervar))
1535 {
eaa5c2d6
GA
1536 /* safe to reuse old SV */
1537 sv_setsv(*cx->blk_loop.itervar, cur);
1538 }
1539 else
1540#endif
1541 {
1542 /* we need a fresh SV every time so that loop body sees a
1543 * completely new SV for closures/references to work as
1544 * they used to */
1545 SvREFCNT_dec(*cx->blk_loop.itervar);
1546 *cx->blk_loop.itervar = newSVsv(cur);
1547 }
89ea2908
GA
1548 if (strEQ(SvPVX(cur), max))
1549 sv_setiv(cur, 0); /* terminate next time */
1550 else
1551 sv_inc(cur);
1552 RETPUSHYES;
1553 }
1554 RETPUSHNO;
1555 }
1556 /* integer increment */
1557 if (cx->blk_loop.iterix > cx->blk_loop.itermax)
1558 RETPUSHNO;
7f61b687 1559
eaa5c2d6 1560#ifndef USE_THREADS /* don't risk potential race */
39ca2bb7
SM
1561 if (SvREFCNT(*cx->blk_loop.itervar) == 1
1562 && !SvMAGICAL(*cx->blk_loop.itervar))
1563 {
eaa5c2d6
GA
1564 /* safe to reuse old SV */
1565 sv_setiv(*cx->blk_loop.itervar, cx->blk_loop.iterix++);
1566 }
1567 else
1568#endif
1569 {
1570 /* we need a fresh SV every time so that loop body sees a
1571 * completely new SV for closures/references to work as they
1572 * used to */
1573 SvREFCNT_dec(*cx->blk_loop.itervar);
1574 *cx->blk_loop.itervar = newSViv(cx->blk_loop.iterix++);
1575 }
89ea2908
GA
1576 RETPUSHYES;
1577 }
1578
1579 /* iterate array */
3280af22 1580 if (cx->blk_loop.iterix >= (av == PL_curstack ? cx->blk_oldsp : AvFILL(av)))
4633a7c4 1581 RETPUSHNO;
a0d0e21e 1582
7a4c00b4 1583 SvREFCNT_dec(*cx->blk_loop.itervar);
a0d0e21e 1584
93965878
NIS
1585 if (sv = (SvMAGICAL(av))
1586 ? *av_fetch(av, ++cx->blk_loop.iterix, FALSE)
1587 : AvARRAY(av)[++cx->blk_loop.iterix])
a0d0e21e 1588 SvTEMP_off(sv);
a0d0e21e 1589 else
3280af22
NIS
1590 sv = &PL_sv_undef;
1591 if (av != PL_curstack && SvIMMORTAL(sv)) {
5f05dabc 1592 SV *lv = cx->blk_loop.iterlval;
71be2cbc
PP
1593 if (lv && SvREFCNT(lv) > 1) {
1594 SvREFCNT_dec(lv);
1595 lv = Nullsv;
1596 }
5f05dabc
PP
1597 if (lv)
1598 SvREFCNT_dec(LvTARG(lv));
1599 else {
68dc0745 1600 lv = cx->blk_loop.iterlval = NEWSV(26, 0);
5f05dabc 1601 sv_upgrade(lv, SVt_PVLV);
5f05dabc 1602 LvTYPE(lv) = 'y';
68dc0745 1603 sv_magic(lv, Nullsv, 'y', Nullch, 0);
5f05dabc
PP
1604 }
1605 LvTARG(lv) = SvREFCNT_inc(av);
1606 LvTARGOFF(lv) = cx->blk_loop.iterix;
8ac85365 1607 LvTARGLEN(lv) = (UV) -1;
5f05dabc
PP
1608 sv = (SV*)lv;
1609 }
a0d0e21e 1610
7a4c00b4 1611 *cx->blk_loop.itervar = SvREFCNT_inc(sv);
a0d0e21e
LW
1612 RETPUSHYES;
1613}
1614
1615PP(pp_subst)
1616{
4e35701f 1617 djSP; dTARG;
a0d0e21e
LW
1618 register PMOP *pm = cPMOP;
1619 PMOP *rpm = pm;
1620 register SV *dstr;
1621 register char *s;
1622 char *strend;
1623 register char *m;
1624 char *c;
1625 register char *d;
1626 STRLEN clen;
1627 I32 iters = 0;
1628 I32 maxiters;
1629 register I32 i;
1630 bool once;
71be2cbc 1631 bool rxtainted;
a0d0e21e 1632 char *orig;
22e551b9 1633 I32 r_flags;
d9f97599 1634 register REGEXP *rx = pm->op_pmregexp;
a0d0e21e
LW
1635 STRLEN len;
1636 int force_on_match = 0;
3280af22 1637 I32 oldsave = PL_savestack_ix;
c277df42 1638 I32 update_minmatch = 1;
a0d0e21e 1639
5cd24f17
PP
1640 /* known replacement string? */
1641 dstr = (pm->op_pmflags & PMf_CONST) ? POPs : Nullsv;
533c011a 1642 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e
LW
1643 TARG = POPs;
1644 else {
54b9620d 1645 TARG = DEFSV;
a0d0e21e 1646 EXTEND(SP,1);
8ec5e241 1647 }
68dc0745
PP
1648 if (SvREADONLY(TARG)
1649 || (SvTYPE(TARG) > SVt_PVLV
1650 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG))))
22c35a8c 1651 croak(PL_no_modify);
8ec5e241
NIS
1652 PUTBACK;
1653
a0d0e21e 1654 s = SvPV(TARG, len);
68dc0745 1655 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV)
a0d0e21e 1656 force_on_match = 1;
b3eb6a9b 1657 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
3280af22
NIS
1658 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
1659 if (PL_tainted)
b3eb6a9b 1660 rxtainted |= 2;
9212bbba 1661 TAINT_NOT;
a0d0e21e
LW
1662
1663 force_it:
1664 if (!pm || !s)
1665 DIE("panic: do_subst");
1666
1667 strend = s + len;
2beec16e
IZ
1668 maxiters = 2*(strend - s) + 10; /* We can match twice at each
1669 position, once with zero-length,
1670 second time with non-zero. */
a0d0e21e 1671
3280af22
NIS
1672 if (!rx->prelen && PL_curpm) {
1673 pm = PL_curpm;
d9f97599 1674 rx = pm->op_pmregexp;
a0d0e21e 1675 }
22e551b9 1676 r_flags = (rx->nparens || SvTEMP(TARG) || PL_sawampersand)
9d080a66 1677 ? REXEC_COPY_STR : 0;
22e551b9
IZ
1678 if (SvSCREAM(TARG) && rx->check_substr
1679 && SvTYPE(rx->check_substr) == SVt_PVBM
1680 && SvVALID(rx->check_substr))
1681 r_flags |= REXEC_SCREAM;
a0d0e21e 1682 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
3280af22
NIS
1683 SAVEINT(PL_multiline);
1684 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
a0d0e21e
LW
1685 }
1686 orig = m = s;
d9f97599
GS
1687 if (rx->check_substr) {
1688 if (!(rx->reganch & ROPT_NOSCAN)) { /* It floats. */
22e551b9 1689 if (r_flags & REXEC_SCREAM) {
c277df42 1690 I32 p = -1;
a0ed51b3 1691 char *b;
c277df42 1692
3280af22 1693 if (PL_screamfirst[BmRARE(rx->check_substr)] < 0)
a0d0e21e 1694 goto nope;
a0ed51b3 1695
dfe13c55 1696 b = (char*)HOP((U8*)s, rx->check_offset_min);
a0ed51b3 1697 if (!(s = screaminstr(TARG, rx->check_substr, b - s, 0, &p, 0)))
a0d0e21e
LW
1698 goto nope;
1699 }
a0ed51b3 1700 else if (!(s = fbm_instr((unsigned char*)HOP((U8*)s, rx->check_offset_min),
c277df42 1701 (unsigned char*)strend,
411d5715 1702 rx->check_substr, 0)))
a0d0e21e 1703 goto nope;
d9f97599
GS
1704 if (s && rx->check_offset_max < s - m) {
1705 ++BmUSEFUL(rx->check_substr);
dfe13c55 1706 s = (char*)HOP((U8*)s, -rx->check_offset_max);
a0d0e21e
LW
1707 }
1708 else
1709 s = m;
1710 }
c277df42
IZ
1711 /* Now checkstring is fixed, i.e. at fixed offset from the
1712 beginning of match, and the match is anchored at s. */
3280af22 1713 else if (!PL_multiline) { /* Anchored at beginning of string. */
c277df42 1714 I32 slen;
dfe13c55 1715 char *b = (char*)HOP((U8*)s, rx->check_offset_min);
a0ed51b3 1716 if (*SvPVX(rx->check_substr) != *b
d9f97599 1717 || ((slen = SvCUR(rx->check_substr)) > 1
a0ed51b3 1718 && memNE(SvPVX(rx->check_substr), b, slen)))
bbce6d69 1719 goto nope;
a0d0e21e 1720 }
a0ed51b3 1721 if (!(rx->reganch & ROPT_NAUGHTY) && --BmUSEFUL(rx->check_substr) < 0
d9f97599
GS
1722 && rx->check_substr == rx->float_substr) {
1723 SvREFCNT_dec(rx->check_substr);
1724 rx->check_substr = Nullsv; /* opt is being useless */
1725 rx->float_substr = Nullsv;
a0d0e21e
LW
1726 }
1727 }
71be2cbc
PP
1728
1729 /* only replace once? */
a0d0e21e 1730 once = !(rpm->op_pmflags & PMf_GLOBAL);
71be2cbc
PP
1731
1732 /* known replacement string? */
5cd24f17 1733 c = dstr ? SvPV(dstr, clen) : Nullch;
71be2cbc
PP
1734
1735 /* can do inplace substitution? */
22e551b9 1736 if (c && clen <= rx->minlen && (once || !(r_flags & REXEC_COPY_STR))
d9f97599 1737 && !(rx->reganch & ROPT_LOOKBEHIND_SEEN)) {
22e551b9 1738 if (!CALLREGEXEC(rx, s, strend, orig, 0, TARG, NULL, r_flags)) {
8ec5e241 1739 SPAGAIN;
3280af22 1740 PUSHs(&PL_sv_no);
71be2cbc
PP
1741 LEAVE_SCOPE(oldsave);
1742 RETURN;
1743 }
1744 if (force_on_match) {
1745 force_on_match = 0;
1746 s = SvPV_force(TARG, len);
1747 goto force_it;
1748 }
71be2cbc 1749 d = s;
3280af22 1750 PL_curpm = pm;
71be2cbc
PP
1751 SvSCREAM_off(TARG); /* disable possible screamer */
1752 if (once) {
48c036b1 1753 rxtainted |= RX_MATCH_TAINTED(rx);
d9f97599
GS
1754 if (rx->subbase) {
1755 m = orig + (rx->startp[0] - rx->subbase);
1756 d = orig + (rx->endp[0] - rx->subbase);
c277df42 1757 } else {
d9f97599
GS
1758 m = rx->startp[0];
1759 d = rx->endp[0];
c277df42 1760 }
71be2cbc
PP
1761 s = orig;
1762 if (m - s > strend - d) { /* faster to shorten from end */
1763 if (clen) {
1764 Copy(c, m, clen, char);
1765 m += clen;
a0d0e21e 1766 }
71be2cbc
PP
1767 i = strend - d;
1768 if (i > 0) {
1769 Move(d, m, i, char);
1770 m += i;
a0d0e21e 1771 }
71be2cbc
PP
1772 *m = '\0';
1773 SvCUR_set(TARG, m - s);
1774 }
1775 /*SUPPRESS 560*/
1776 else if (i = m - s) { /* faster from front */
1777 d -= clen;
1778 m = d;
1779 sv_chop(TARG, d-i);
1780 s += i;
1781 while (i--)
1782 *--d = *--s;
1783 if (clen)
1784 Copy(c, m, clen, char);
1785 }
1786 else if (clen) {
1787 d -= clen;
1788 sv_chop(TARG, d);
1789 Copy(c, d, clen, char);
1790 }
1791 else {
1792 sv_chop(TARG, d);
1793 }
48c036b1 1794 TAINT_IF(rxtainted & 1);
8ec5e241 1795 SPAGAIN;
3280af22 1796 PUSHs(&PL_sv_yes);
71be2cbc
PP
1797 }
1798 else {
71be2cbc
PP
1799 do {
1800 if (iters++ > maxiters)
1801 DIE("Substitution loop");
d9f97599
GS
1802 rxtainted |= RX_MATCH_TAINTED(rx);
1803 m = rx->startp[0];
71be2cbc
PP
1804 /*SUPPRESS 560*/
1805 if (i = m - s) {
1806 if (s != d)
1807 Move(s, d, i, char);
1808 d += i;
a0d0e21e 1809 }
71be2cbc
PP
1810 if (clen) {
1811 Copy(c, d, clen, char);
1812 d += clen;
1813 }
d9f97599 1814 s = rx->endp[0];
15e52e56 1815 } while (CALLREGEXEC(rx, s, strend, orig, s == m,
c277df42 1816 Nullsv, NULL, 0)); /* don't match same null twice */
71be2cbc
PP
1817 if (s != d) {
1818 i = strend - s;
1819 SvCUR_set(TARG, d - SvPVX(TARG) + i);
1820 Move(s, d, i+1, char); /* include the NUL */
a0d0e21e 1821 }
48c036b1 1822 TAINT_IF(rxtainted & 1);
8ec5e241 1823 SPAGAIN;
71be2cbc 1824 PUSHs(sv_2mortal(newSViv((I32)iters)));
a0d0e21e 1825 }
71be2cbc 1826 (void)SvPOK_only(TARG);
48c036b1 1827 TAINT_IF(rxtainted);
8ec5e241
NIS
1828 if (SvSMAGICAL(TARG)) {
1829 PUTBACK;
1830 mg_set(TARG);
1831 SPAGAIN;
1832 }
9212bbba 1833 SvTAINT(TARG);
71be2cbc
PP
1834 LEAVE_SCOPE(oldsave);
1835 RETURN;
a0d0e21e 1836 }
71be2cbc 1837
22e551b9 1838 if (CALLREGEXEC(rx, s, strend, orig, 0, TARG, NULL, r_flags)) {
a0d0e21e
LW
1839 if (force_on_match) {
1840 force_on_match = 0;
1841 s = SvPV_force(TARG, len);
1842 goto force_it;
1843 }
48c036b1 1844 rxtainted |= RX_MATCH_TAINTED(rx);
8ec5e241 1845 dstr = NEWSV(25, len);
a0d0e21e 1846 sv_setpvn(dstr, m, s-m);
3280af22 1847 PL_curpm = pm;
a0d0e21e 1848 if (!c) {
c09156bb 1849 register PERL_CONTEXT *cx;
8ec5e241 1850 SPAGAIN;
a0d0e21e
LW
1851 PUSHSUBST(cx);
1852 RETURNOP(cPMOP->op_pmreplroot);
1853 }
ad94a511 1854 r_flags |= REXEC_IGNOREPOS;
a0d0e21e
LW
1855 do {
1856 if (iters++ > maxiters)
1857 DIE("Substitution loop");
d9f97599
GS
1858 rxtainted |= RX_MATCH_TAINTED(rx);
1859 if (rx->subbase && rx->subbase != orig) {
a0d0e21e
LW
1860 m = s;
1861 s = orig;
d9f97599 1862 orig = rx->subbase;
a0d0e21e
LW
1863 s = orig + (m - s);
1864 strend = s + (strend - m);
1865 }
d9f97599 1866 m = rx->startp[0];
a0d0e21e 1867 sv_catpvn(dstr, s, m-s);
d9f97599 1868 s = rx->endp[0];
a0d0e21e
LW
1869 if (clen)
1870 sv_catpvn(dstr, c, clen);
1871 if (once)
1872 break;
ad94a511 1873 } while (CALLREGEXEC(rx, s, strend, orig, s == m, TARG, NULL, r_flags));
a0d0e21e 1874 sv_catpvn(dstr, s, strend - s);
748a9306 1875
4633a7c4 1876 (void)SvOOK_off(TARG);
cb0b1708 1877 Safefree(SvPVX(TARG));
748a9306
LW
1878 SvPVX(TARG) = SvPVX(dstr);
1879 SvCUR_set(TARG, SvCUR(dstr));
1880 SvLEN_set(TARG, SvLEN(dstr));
1881 SvPVX(dstr) = 0;
1882 sv_free(dstr);
1883
48c036b1 1884 TAINT_IF(rxtainted & 1);
f878fbec 1885 SPAGAIN;
48c036b1
GS
1886 PUSHs(sv_2mortal(newSViv((I32)iters)));
1887
a0d0e21e 1888 (void)SvPOK_only(TARG);
48c036b1 1889 TAINT_IF(rxtainted);
a0d0e21e 1890 SvSETMAGIC(TARG);
9212bbba 1891 SvTAINT(TARG);
4633a7c4 1892 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1893 RETURN;
1894 }
5cd24f17 1895 goto ret_no;
a0d0e21e
LW
1896
1897nope:
d9f97599 1898 ++BmUSEFUL(rx->check_substr);
5cd24f17 1899
8ec5e241
NIS
1900ret_no:
1901 SPAGAIN;
3280af22 1902 PUSHs(&PL_sv_no);
4633a7c4 1903 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1904 RETURN;
1905}
1906
1907PP(pp_grepwhile)
1908{
4e35701f 1909 djSP;
a0d0e21e
LW
1910
1911 if (SvTRUEx(POPs))
3280af22
NIS
1912 PL_stack_base[PL_markstack_ptr[-1]++] = PL_stack_base[*PL_markstack_ptr];
1913 ++*PL_markstack_ptr;
a0d0e21e
LW
1914 LEAVE; /* exit inner scope */
1915
1916 /* All done yet? */
3280af22 1917 if (PL_stack_base + *PL_markstack_ptr > SP) {
a0d0e21e 1918 I32 items;
54310121 1919 I32 gimme = GIMME_V;
a0d0e21e
LW
1920
1921 LEAVE; /* exit outer scope */
1922 (void)POPMARK; /* pop src */
3280af22 1923 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
a0d0e21e 1924 (void)POPMARK; /* pop dst */
3280af22 1925 SP = PL_stack_base + POPMARK; /* pop original mark */
54310121 1926 if (gimme == G_SCALAR) {
a0d0e21e
LW
1927 dTARGET;
1928 XPUSHi(items);
a0d0e21e 1929 }
54310121
PP
1930 else if (gimme == G_ARRAY)
1931 SP += items;
a0d0e21e
LW
1932 RETURN;
1933 }
1934 else {
1935 SV *src;
1936
1937 ENTER; /* enter inner scope */
3280af22 1938 SAVESPTR(PL_curpm);
a0d0e21e 1939
3280af22 1940 src = PL_stack_base[*PL_markstack_ptr];
a0d0e21e 1941 SvTEMP_off(src);
54b9620d 1942 DEFSV = src;
a0d0e21e
LW
1943
1944 RETURNOP(cLOGOP->op_other);
1945 }
1946}
1947
1948PP(pp_leavesub)
1949{
4e35701f 1950 djSP;
a0d0e21e
LW
1951 SV **mark;
1952 SV **newsp;
1953 PMOP *newpm;
1954 I32 gimme;
c09156bb 1955 register PERL_CONTEXT *cx;
f86702cc 1956 struct block_sub cxsub;
a0d0e21e
LW
1957
1958 POPBLOCK(cx,newpm);
f86702cc
PP
1959 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1960
a1f49e72 1961 TAINT_NOT;
a0d0e21e
LW
1962 if (gimme == G_SCALAR) {
1963 MARK = newsp + 1;
a29cdaf0
IZ
1964 if (MARK <= SP) {
1965 if (cxsub.cv && CvDEPTH(cxsub.cv) > 1) {
1966 if (SvTEMP(TOPs)) {
1967 *MARK = SvREFCNT_inc(TOPs);
1968 FREETMPS;
1969 sv_2mortal(*MARK);
1970 } else {
1971 FREETMPS;
1972 *MARK = sv_mortalcopy(TOPs);
1973 }
1974 } else
1975 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
1976 } else {
f86702cc 1977 MEXTEND(MARK, 0);
3280af22 1978 *MARK = &PL_sv_undef;
a0d0e21e
LW
1979 }
1980 SP = MARK;
1981 }
54310121 1982 else if (gimme == G_ARRAY) {
f86702cc 1983 for (MARK = newsp + 1; MARK <= SP; MARK++) {
a1f49e72 1984 if (!SvTEMP(*MARK)) {
f86702cc 1985 *MARK = sv_mortalcopy(*MARK);
a1f49e72
CS
1986 TAINT_NOT; /* Each item is independent */
1987 }
f86702cc 1988 }
a0d0e21e 1989 }
f86702cc
PP
1990 PUTBACK;
1991
1992 POPSUB2(); /* Stack values are safe: release CV and @_ ... */
3280af22 1993 PL_curpm = newpm; /* ... and pop $1 et al */
a0d0e21e
LW
1994
1995 LEAVE;
a0d0e21e
LW
1996 return pop_return();
1997}
1998
76e3520e 1999STATIC CV *
8ac85365 2000get_db_sub(SV **svp, CV *cv)
3de9ffa1
MB
2001{
2002 dTHR;
3280af22 2003 SV *dbsv = GvSV(PL_DBsub);
491527d0
GS
2004
2005 if (!PERLDB_SUB_NN) {
2006 GV *gv = CvGV(cv);
2007
2008 save_item(dbsv);
2009 if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED))
2010 || strEQ(GvNAME(gv), "END")
2011 || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */
2012 !( (SvTYPE(*svp) == SVt_PVGV) && (GvCV((GV*)*svp) == cv)
2013 && (gv = (GV*)*svp) ))) {
2014 /* Use GV from the stack as a fallback. */
2015 /* GV is potentially non-unique, or contain different CV. */
2016 sv_setsv(dbsv, newRV((SV*)cv));
2017 }
2018 else {
2019 gv_efullname3(dbsv, gv, Nullch);
2020 }
3de9ffa1
MB
2021 }
2022 else {
491527d0
GS
2023 SvUPGRADE(dbsv, SVt_PVIV);
2024 SvIOK_on(dbsv);
2025 SAVEIV(SvIVX(dbsv));
2026 SvIVX(dbsv) = (IV)cv; /* Do it the quickest way */
3de9ffa1 2027 }
491527d0 2028
3de9ffa1 2029 if (CvXSUB(cv))
3280af22
NIS
2030 PL_curcopdb = PL_curcop;
2031 cv = GvCV(PL_DBsub);
3de9ffa1
MB
2032 return cv;
2033}
2034
a0d0e21e
LW
2035PP(pp_entersub)
2036{
4e35701f 2037 djSP; dPOPss;
a0d0e21e
LW
2038 GV *gv;
2039 HV *stash;
2040 register CV *cv;
c09156bb 2041 register PERL_CONTEXT *cx;
5d94fbed 2042 I32 gimme;
533c011a 2043 bool hasargs = (PL_op->op_flags & OPf_STACKED) != 0;
a0d0e21e
LW
2044
2045 if (!sv)
2046 DIE("Not a CODE reference");
2047 switch (SvTYPE(sv)) {
2048 default:
2049 if (!SvROK(sv)) {
748a9306 2050 char *sym;
2d8e6c8d 2051 STRLEN n_a;
748a9306 2052
3280af22 2053 if (sv == &PL_sv_yes) { /* unfound import, ignore */
fb73857a 2054 if (hasargs)
3280af22 2055 SP = PL_stack_base + POPMARK;
a0d0e21e 2056 RETURN;
fb73857a 2057 }
15ff848f
CS
2058 if (SvGMAGICAL(sv)) {
2059 mg_get(sv);
2060 sym = SvPOKp(sv) ? SvPVX(sv) : Nullch;
2061 }
2062 else
2d8e6c8d 2063 sym = SvPV(sv, n_a);
15ff848f 2064 if (!sym)
22c35a8c 2065 DIE(PL_no_usym, "a subroutine");
533c011a 2066 if (PL_op->op_private & HINT_STRICT_REFS)
22c35a8c 2067 DIE(PL_no_symref, sym, "a subroutine");
748a9306 2068 cv = perl_get_cv(sym, TRUE);
a0d0e21e
LW
2069 break;
2070 }
f5284f61
IZ
2071 {
2072 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
2073 tryAMAGICunDEREF(to_cv);
2074 }
a0d0e21e
LW
2075 cv = (CV*)SvRV(sv);
2076 if (SvTYPE(cv) == SVt_PVCV)
2077 break;
2078 /* FALL THROUGH */
2079 case SVt_PVHV:
2080 case SVt_PVAV:
2081 DIE("Not a CODE reference");
2082 case SVt_PVCV:
2083 cv = (CV*)sv;
2084 break;
2085 case SVt_PVGV:
8ebc5c01 2086 if (!(cv = GvCVu((GV*)sv)))
a0d0e21e
LW
2087 cv = sv_2cv(sv, &stash, &gv, TRUE);
2088 break;
2089 }
2090
2091 ENTER;
2092 SAVETMPS;
2093
2094 retry:
2095 if (!cv)
2096 DIE("Not a CODE reference");
2097
2098 if (!CvROOT(cv) && !CvXSUB(cv)) {
44a8e56a 2099 GV* autogv;
22239a37 2100 SV* sub_name;
44a8e56a
PP
2101
2102 /* anonymous or undef'd function leaves us no recourse */
2103 if (CvANON(cv) || !(gv = CvGV(cv)))
2104 DIE("Undefined subroutine called");
2105 /* autoloaded stub? */
2106 if (cv != GvCV(gv)) {
2107 cv = GvCV(gv);
2108 goto retry;
a0d0e21e 2109 }
44a8e56a 2110 /* should call AUTOLOAD now? */
54310121
PP
2111 if ((autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
2112 FALSE)))
2113 {
44a8e56a
PP
2114 cv = GvCV(autogv);
2115 goto retry;
a0d0e21e 2116 }
44a8e56a 2117 /* sorry */
22239a37
NIS
2118 sub_name = sv_newmortal();
2119 gv_efullname3(sub_name, gv, Nullch);
2120 DIE("Undefined subroutine &%s called", SvPVX(sub_name));
a0d0e21e
LW
2121 }
2122
54310121 2123 gimme = GIMME_V;
533c011a 2124 if ((PL_op->op_private & OPpENTERSUB_DB) && GvCV(PL_DBsub) && !CvNODEBUG(cv))
4f01c5a5 2125 cv = get_db_sub(&sv, cv);
3de9ffa1
MB
2126 if (!cv)
2127 DIE("No DBsub routine");
a0d0e21e 2128
11343788 2129#ifdef USE_THREADS
3de9ffa1
MB
2130 /*
2131 * First we need to check if the sub or method requires locking.
458fb581
MB
2132 * If so, we gain a lock on the CV, the first argument or the
2133 * stash (for static methods), as appropriate. This has to be
2134 * inline because for FAKE_THREADS, COND_WAIT inlines code to
2135 * reschedule by returning a new op.
3de9ffa1 2136 */
11343788 2137 MUTEX_LOCK(CvMUTEXP(cv));
77a005ab
MB
2138 if (CvFLAGS(cv) & CVf_LOCKED) {
2139 MAGIC *mg;
2140 if (CvFLAGS(cv) & CVf_METHOD) {
533c011a
NIS
2141 if (SP > PL_stack_base + TOPMARK)
2142 sv = *(PL_stack_base + TOPMARK + 1);
77a005ab
MB
2143 else {
2144 MUTEX_UNLOCK(CvMUTEXP(cv));
2145 croak("no argument for locked method call");
2146 }
2147 if (SvROK(sv))
2148 sv = SvRV(sv);
458fb581
MB
2149 else {
2150 STRLEN len;
2151 char *stashname = SvPV(sv, len);
2152 sv = (SV*)gv_stashpvn(stashname, len, TRUE);
2153 }
77a005ab
MB
2154 }
2155 else {
2156 sv = (SV*)cv;
2157 }
2158 MUTEX_UNLOCK(CvMUTEXP(cv));
2159 mg = condpair_magic(sv);
2160 MUTEX_LOCK(MgMUTEXP(mg));
2161 if (MgOWNER(mg) == thr)
2162 MUTEX_UNLOCK(MgMUTEXP(mg));
2163 else {
2164 while (MgOWNER(mg))
2165 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
2166 MgOWNER(mg) = thr;
8b73bbec 2167 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: pp_entersub lock %p\n",
1fd28e87 2168 thr, sv);)
77a005ab
MB
2169 MUTEX_UNLOCK(MgMUTEXP(mg));
2170 save_destructor(unlock_condpair, sv);
11343788 2171 }
77a005ab 2172 MUTEX_LOCK(CvMUTEXP(cv));
11343788 2173 }
3de9ffa1
MB
2174 /*
2175 * Now we have permission to enter the sub, we must distinguish
2176 * four cases. (0) It's an XSUB (in which case we don't care
2177 * about ownership); (1) it's ours already (and we're recursing);
2178 * (2) it's free (but we may already be using a cached clone);
2179 * (3) another thread owns it. Case (1) is easy: we just use it.
2180 * Case (2) means we look for a clone--if we have one, use it
2181 * otherwise grab ownership of cv. Case (3) means we look for a
2182 * clone (for non-XSUBs) and have to create one if we don't
2183 * already have one.
2184 * Why look for a clone in case (2) when we could just grab
2185 * ownership of cv straight away? Well, we could be recursing,
2186 * i.e. we originally tried to enter cv while another thread
2187 * owned it (hence we used a clone) but it has been freed up
2188 * and we're now recursing into it. It may or may not be "better"
2189 * to use the clone but at least CvDEPTH can be trusted.
2190 */
2191 if (CvOWNER(cv) == thr || CvXSUB(cv))
2192 MUTEX_UNLOCK(CvMUTEXP(cv));
11343788 2193 else {
3de9ffa1
MB
2194 /* Case (2) or (3) */
2195 SV **svp;
2196
11343788 2197 /*
3de9ffa1
MB
2198 * XXX Might it be better to release CvMUTEXP(cv) while we
2199 * do the hv_fetch? We might find someone has pinched it
2200 * when we look again, in which case we would be in case
2201 * (3) instead of (2) so we'd have to clone. Would the fact
2202 * that we released the mutex more quickly make up for this?
2203 */
b099ddc0 2204 if ((svp = hv_fetch(thr->cvcache, (char *)cv, sizeof(cv), FALSE)))
6ee623d5 2205 {
3de9ffa1 2206 /* We already have a clone to use */
11343788 2207 MUTEX_UNLOCK(CvMUTEXP(cv));
3de9ffa1 2208 cv = *(CV**)svp;
8b73bbec 2209 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
1fd28e87
MB
2210 "entersub: %p already has clone %p:%s\n",
2211 thr, cv, SvPEEK((SV*)cv)));
3de9ffa1
MB
2212 CvOWNER(cv) = thr;
2213 SvREFCNT_inc(cv);
2214 if (CvDEPTH(cv) == 0)
2215 SAVEDESTRUCTOR(unset_cvowner, (void*) cv);
2216 }
11343788 2217 else {
3de9ffa1
MB
2218 /* (2) => grab ownership of cv. (3) => make clone */
2219 if (!CvOWNER(cv)) {
2220 CvOWNER(cv) = thr;
2221 SvREFCNT_inc(cv);
11343788 2222 MUTEX_UNLOCK(CvMUTEXP(cv));
8b73bbec 2223 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
1fd28e87
MB
2224 "entersub: %p grabbing %p:%s in stash %s\n",
2225 thr, cv, SvPEEK((SV*)cv), CvSTASH(cv) ?
3de9ffa1
MB
2226 HvNAME(CvSTASH(cv)) : "(none)"));
2227 } else {
2228 /* Make a new clone. */
2229 CV *clonecv;
2230 SvREFCNT_inc(cv); /* don't let it vanish from under us */
2231 MUTEX_UNLOCK(CvMUTEXP(cv));
8b73bbec 2232 DEBUG_S((PerlIO_printf(PerlIO_stderr(),
1fd28e87
MB
2233 "entersub: %p cloning %p:%s\n",
2234 thr, cv, SvPEEK((SV*)cv))));
3de9ffa1
MB
2235 /*
2236 * We're creating a new clone so there's no race
2237 * between the original MUTEX_UNLOCK and the
2238 * SvREFCNT_inc since no one will be trying to undef
2239 * it out from underneath us. At least, I don't think
2240 * there's a race...
2241 */
2242 clonecv = cv_clone(cv);
2243 SvREFCNT_dec(cv); /* finished with this */
199100c8 2244 hv_store(thr->cvcache, (char*)cv, sizeof(cv), (SV*)clonecv,0);
3de9ffa1
MB
2245 CvOWNER(clonecv) = thr;
2246 cv = clonecv;
11343788 2247 SvREFCNT_inc(cv);
11343788 2248 }
8b73bbec 2249 DEBUG_S(if (CvDEPTH(cv) != 0)
3de9ffa1
MB
2250 PerlIO_printf(PerlIO_stderr(), "depth %ld != 0\n",
2251 CvDEPTH(cv)););
2252 SAVEDESTRUCTOR(unset_cvowner, (void*) cv);
11343788 2253 }
3de9ffa1 2254 }
11343788
MB
2255#endif /* USE_THREADS */
2256
a0d0e21e
LW
2257 if (CvXSUB(cv)) {
2258 if (CvOLDSTYLE(cv)) {
ecfc5424 2259 I32 (*fp3)_((int,int,int));
a0d0e21e
LW
2260 dMARK;
2261 register I32 items = SP - MARK;
67955e0c 2262 /* We dont worry to copy from @_. */
924508f0
GS
2263 while (SP > mark) {
2264 SP[1] = SP[0];
2265 SP--;
a0d0e21e 2266 }
3280af22 2267 PL_stack_sp = mark + 1;
ecfc5424
AD
2268 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
2269 items = (*fp3)(CvXSUBANY(cv).any_i32,
3280af22 2270 MARK - PL_stack_base + 1,
ecfc5424 2271 items);
3280af22 2272 PL_stack_sp = PL_stack_base + items;
a0d0e21e
LW
2273 }
2274 else {
748a9306
LW
2275 I32 markix = TOPMARK;
2276
a0d0e21e 2277 PUTBACK;
67955e0c
PP
2278
2279 if (!hasargs) {
2280 /* Need to copy @_ to stack. Alternative may be to
2281 * switch stack to @_, and copy return values
2282 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
6d4ff0d2
MB
2283 AV* av;
2284 I32 items;
2285#ifdef USE_THREADS
533c011a 2286 av = (AV*)PL_curpad[0];
6d4ff0d2 2287#else
3280af22 2288 av = GvAV(PL_defgv);
6d4ff0d2 2289#endif /* USE_THREADS */
93965878 2290 items = AvFILLp(av) + 1; /* @_ is not tieable */
67955e0c
PP
2291
2292 if (items) {
2293 /* Mark is at the end of the stack. */
924508f0
GS
2294 EXTEND(SP, items);
2295 Copy(AvARRAY(av), SP + 1, items, SV*);
2296 SP += items;
67955e0c
PP
2297 PUTBACK ;
2298 }
2299 }
3280af22 2300 if (PL_curcopdb) { /* We assume that the first
67955e0c
PP
2301 XSUB in &DB::sub is the
2302 called one. */
3280af22
NIS
2303 SAVESPTR(PL_curcop);
2304 PL_curcop = PL_curcopdb;
2305 PL_curcopdb = NULL;
67955e0c
PP
2306 }
2307 /* Do we need to open block here? XXXX */
1d583055 2308 (void)(*CvXSUB(cv))(cv _PERL_OBJECT_THIS);
748a9306
LW
2309
2310 /* Enforce some sanity in scalar context. */
3280af22
NIS
2311 if (gimme == G_SCALAR && ++markix != PL_stack_sp - PL_stack_base ) {
2312 if (markix > PL_stack_sp - PL_stack_base)
2313 *(PL_stack_base + markix) = &PL_sv_undef;
748a9306 2314 else
3280af22
NIS
2315 *(PL_stack_base + markix) = *PL_stack_sp;
2316 PL_stack_sp = PL_stack_base + markix;
748a9306 2317 }
a0d0e21e
LW
2318 }
2319 LEAVE;
2320 return NORMAL;
2321 }
2322 else {
2323 dMARK;
2324 register I32 items = SP - MARK;
a0d0e21e
LW
2325 AV* padlist = CvPADLIST(cv);
2326 SV** svp = AvARRAY(padlist);
533c011a 2327 push_return(PL_op->op_next);
a0d0e21e
LW
2328 PUSHBLOCK(cx, CXt_SUB, MARK);
2329 PUSHSUB(cx);
2330 CvDEPTH(cv)++;
6b35e009
GS
2331 /* XXX This would be a natural place to set C<PL_compcv = cv> so
2332 * that eval'' ops within this sub know the correct lexical space.
2333 * Owing the speed considerations, we choose to search for the cv
2334 * in doeval() instead.
2335 */
a0d0e21e
LW
2336 if (CvDEPTH(cv) < 2)
2337 (void)SvREFCNT_inc(cv);
2338 else { /* save temporaries on recursion? */
93965878 2339 if (CvDEPTH(cv) > AvFILLp(padlist)) {
a0d0e21e
LW
2340 AV *av;
2341 AV *newpad = newAV();
4aa0a1f7 2342 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
93965878 2343 I32 ix = AvFILLp((AV*)svp[1]);
a0d0e21e 2344 svp = AvARRAY(svp[0]);
748a9306 2345 for ( ;ix > 0; ix--) {
3280af22 2346 if (svp[ix] != &PL_sv_undef) {
748a9306 2347 char *name = SvPVX(svp[ix]);
5f05dabc
PP
2348 if ((SvFLAGS(svp[ix]) & SVf_FAKE) /* outer lexical? */
2349 || *name == '&') /* anonymous code? */
2350 {
2351 av_store(newpad, ix, SvREFCNT_inc(oldpad[ix]));
748a9306
LW
2352 }
2353 else { /* our own lexical */
2354 if (*name == '@')
2355 av_store(newpad, ix, sv = (SV*)newAV());
2356 else if (*name == '%')
2357 av_store(newpad, ix, sv = (SV*)newHV());
2358 else
2359 av_store(newpad, ix, sv = NEWSV(0,0));
2360 SvPADMY_on(sv);
2361 }
a0d0e21e
LW
2362 }
2363 else {
748a9306 2364 av_store(newpad, ix, sv = NEWSV(0,0));
a0d0e21e
LW
2365 SvPADTMP_on(sv);
2366 }
2367 }
2368 av = newAV(); /* will be @_ */
2369 av_extend(av, 0);
2370 av_store(newpad, 0, (SV*)av);
2371 AvFLAGS(av) = AVf_REIFY;
2372 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
93965878 2373 AvFILLp(padlist) = CvDEPTH(cv);
a0d0e21e
LW
2374 svp = AvARRAY(padlist);
2375 }
2376 }
6d4ff0d2
MB
2377#ifdef USE_THREADS
2378 if (!hasargs) {
533c011a 2379 AV* av = (AV*)PL_curpad[0];
6d4ff0d2 2380
93965878 2381 items = AvFILLp(av) + 1;
6d4ff0d2
MB
2382 if (items) {
2383 /* Mark is at the end of the stack. */
924508f0
GS
2384 EXTEND(SP, items);
2385 Copy(AvARRAY(av), SP + 1, items, SV*);
2386 SP += items;
6d4ff0d2
MB
2387 PUTBACK ;
2388 }
2389 }
2390#endif /* USE_THREADS */
3280af22
NIS
2391 SAVESPTR(PL_curpad);
2392 PL_curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
6d4ff0d2
MB
2393#ifndef USE_THREADS
2394 if (hasargs)
2395#endif /* USE_THREADS */
2396 {
2397 AV* av;
a0d0e21e
LW
2398 SV** ary;
2399
77a005ab 2400#if 0
8b73bbec 2401 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
0f15f207 2402 "%p entersub preparing @_\n", thr));
77a005ab 2403#endif
3280af22 2404 av = (AV*)PL_curpad[0];
a0d0e21e
LW
2405 if (AvREAL(av)) {
2406 av_clear(av);
2407 AvREAL_off(av);
2408 }
6d4ff0d2 2409#ifndef USE_THREADS
3280af22
NIS
2410 cx->blk_sub.savearray = GvAV(PL_defgv);
2411 GvAV(PL_defgv) = (AV*)SvREFCNT_inc(av);
6d4ff0d2
MB
2412#endif /* USE_THREADS */
2413 cx->blk_sub.argarray = av;
a0d0e21e
LW
2414 ++MARK;
2415
2416 if (items > AvMAX(av) + 1) {
2417 ary = AvALLOC(av);
2418 if (AvARRAY(av) != ary) {
2419 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2420 SvPVX(av) = (char*)ary;
2421 }
2422 if (items > AvMAX(av) + 1) {
2423 AvMAX(av) = items - 1;
2424 Renew(ary,items,SV*);
2425 AvALLOC(av) = ary;
2426 SvPVX(av) = (char*)ary;
2427 }
2428 }
2429 Copy(MARK,AvARRAY(av),items,SV*);
93965878 2430 AvFILLp(av) = items - 1;
a0d0e21e
LW
2431
2432 while (items--) {
2433 if (*MARK)
2434 SvTEMP_off(*MARK);
2435 MARK++;
2436 }
2437 }
4a925ff6
GS
2438 /* warning must come *after* we fully set up the context
2439 * stuff so that __WARN__ handlers can safely dounwind()
2440 * if they want to
2441 */
2442 if (CvDEPTH(cv) == 100 && ckWARN(WARN_RECURSION)
2443 && !(PERLDB_SUB && cv == GvCV(PL_DBsub)))
2444 sub_crush_depth(cv);
77a005ab 2445#if 0
8b73bbec 2446 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
0f15f207 2447 "%p entersub returning %p\n", thr, CvSTART(cv)));
77a005ab 2448#endif
a0d0e21e
LW
2449 RETURNOP(CvSTART(cv));
2450 }
2451}
2452
44a8e56a 2453void
8ac85365 2454sub_crush_depth(CV *cv)
44a8e56a
PP
2455{
2456 if (CvANON(cv))
599cee73 2457 warner(WARN_RECURSION, "Deep recursion on anonymous subroutine");
44a8e56a
PP
2458 else {
2459 SV* tmpstr = sv_newmortal();
2460 gv_efullname3(tmpstr, CvGV(cv), Nullch);
599cee73
PM
2461 warner(WARN_RECURSION, "Deep recursion on subroutine \"%s\"",
2462 SvPVX(tmpstr));
44a8e56a
PP
2463 }
2464}
2465
a0d0e21e
LW
2466PP(pp_aelem)
2467{
4e35701f 2468 djSP;
a0d0e21e 2469 SV** svp;
748a9306 2470 I32 elem = POPi;
68dc0745 2471 AV* av = (AV*)POPs;
533c011a
NIS
2472 U32 lval = PL_op->op_flags & OPf_MOD;
2473 U32 defer = (PL_op->op_private & OPpLVAL_DEFER) && (elem > AvFILL(av));
be6c24e0 2474 SV *sv;
a0d0e21e 2475
748a9306 2476 if (elem > 0)
3280af22 2477 elem -= PL_curcop->cop_arybase;
a0d0e21e
LW
2478 if (SvTYPE(av) != SVt_PVAV)
2479 RETPUSHUNDEF;
68dc0745 2480 svp = av_fetch(av, elem, lval && !defer);
a0d0e21e 2481 if (lval) {
3280af22 2482 if (!svp || *svp == &PL_sv_undef) {
68dc0745
PP
2483 SV* lv;
2484 if (!defer)
22c35a8c 2485 DIE(PL_no_aelem, elem);
68dc0745
PP
2486 lv = sv_newmortal();
2487 sv_upgrade(lv, SVt_PVLV);
2488 LvTYPE(lv) = 'y';
2489 sv_magic(lv, Nullsv, 'y', Nullch, 0);
2490 LvTARG(lv) = SvREFCNT_inc(av);
2491 LvTARGOFF(lv) = elem;
2492 LvTARGLEN(lv) = 1;
2493 PUSHs(lv);
2494 RETURN;
2495 }
533c011a 2496 if (PL_op->op_private & OPpLVAL_INTRO)
161b7d16 2497 save_aelem(av, elem, svp);
533c011a
NIS
2498 else if (PL_op->op_private & OPpDEREF)
2499 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
a0d0e21e 2500 }
3280af22 2501 sv = (svp ? *svp : &PL_sv_undef);
be6c24e0
GS
2502 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
2503 sv = sv_mortalcopy(sv);
2504 PUSHs(sv);
a0d0e21e
LW
2505 RETURN;
2506}
2507
02a9e968 2508void
8ac85365 2509vivify_ref(SV *sv, U32 to_what)
02a9e968
CS
2510{
2511 if (SvGMAGICAL(sv))
2512 mg_get(sv);
2513 if (!SvOK(sv)) {
2514 if (SvREADONLY(sv))
22c35a8c 2515 croak(PL_no_modify);
5f05dabc
PP
2516 if (SvTYPE(sv) < SVt_RV)
2517 sv_upgrade(sv, SVt_RV);
2518 else if (SvTYPE(sv) >= SVt_PV) {
2519 (void)SvOOK_off(sv);
2520 Safefree(SvPVX(sv));
2521 SvLEN(sv) = SvCUR(sv) = 0;
2522 }
68dc0745 2523 switch (to_what) {
5f05dabc 2524 case OPpDEREF_SV:
8c52afec 2525 SvRV(sv) = NEWSV(355,0);
5f05dabc
PP
2526 break;
2527 case OPpDEREF_AV:
2528 SvRV(sv) = (SV*)newAV();
2529 break;
2530 case OPpDEREF_HV:
2531 SvRV(sv) = (SV*)newHV();
2532 break;
2533 }
02a9e968
CS
2534 SvROK_on(sv);
2535 SvSETMAGIC(sv);
2536 }
2537}
2538
a0d0e21e
LW
2539PP(pp_method)
2540{
4e35701f 2541 djSP;
a0d0e21e
LW
2542 SV* sv;
2543 SV* ob;
2544 GV* gv;
56304f61
CS
2545 HV* stash;
2546 char* name;
ac91690f
CS
2547 char* packname;
2548 STRLEN packlen;
a0d0e21e 2549
fb73857a
PP
2550 if (SvROK(TOPs)) {
2551 sv = SvRV(TOPs);
2552 if (SvTYPE(sv) == SVt_PVCV) {
2553 SETs(sv);
2554 RETURN;
2555 }
2556 }
2557
2d8e6c8d 2558 name = SvPV(TOPs, packlen);
3280af22 2559 sv = *(PL_stack_base + TOPMARK + 1);
a0d0e21e 2560
16d20bd9
AD
2561 if (SvGMAGICAL(sv))
2562 mg_get(sv);
a0d0e21e 2563 if (SvROK(sv))
16d20bd9 2564 ob = (SV*)SvRV(sv);
a0d0e21e
LW
2565 else {
2566 GV* iogv;
a0d0e21e 2567
56304f61 2568 packname = Nullch;
a0d0e21e 2569 if (!SvOK(sv) ||
56304f61 2570 !(packname = SvPV(sv, packlen)) ||
a0d0e21e
LW
2571 !(iogv = gv_fetchpv(packname, FALSE, SVt_PVIO)) ||
2572 !(ob=(SV*)GvIO(iogv)))
2573 {
834a4ddd
LW
2574 if (!packname ||
2575 ((*(U8*)packname >= 0xc0 && IN_UTF8)
b86a2fa7 2576 ? !isIDFIRST_utf8((U8*)packname)
834a4ddd
LW
2577 : !isIDFIRST(*packname)
2578 ))
2579 {
72b5445b
GS
2580 DIE("Can't call method \"%s\" %s", name,
2581 SvOK(sv)? "without a package or object reference"
2582 : "on an undefined value");
834a4ddd 2583 }
56304f61 2584 stash = gv_stashpvn(packname, packlen, TRUE);
ac91690f 2585 goto fetch;
a0d0e21e 2586 }
3280af22 2587 *(PL_stack_base + TOPMARK + 1) = sv_2mortal(newRV((SV*)iogv));
a0d0e21e
LW
2588 }
2589
ac91690f 2590 if (!ob || !SvOBJECT(ob))
a0d0e21e 2591 DIE("Can't call method \"%s\" on unblessed reference", name);
a0d0e21e 2592
56304f61 2593 stash = SvSTASH(ob);
a0d0e21e 2594
ac91690f 2595 fetch:
ac91690f 2596 gv = gv_fetchmethod(stash, name);
56304f61
CS
2597 if (!gv) {
2598 char* leaf = name;
2599 char* sep = Nullch;
2600 char* p;
2601
2602 for (p = name; *p; p++) {
2603 if (*p == '\'')
2604 sep = p, leaf = p + 1;
2605 else if (*p == ':' && *(p + 1) == ':')
2606 sep = p, leaf = p + 2;
2607 }
2608 if (!sep || ((sep - name) == 5 && strnEQ(name, "SUPER", 5))) {
3280af22 2609 packname = HvNAME(sep ? PL_curcop->cop_stash : stash);
56304f61
CS
2610 packlen = strlen(packname);
2611 }
2612 else {
2613 packname = name;
2614 packlen = sep - name;
2615 }
2616 DIE("Can't locate object method \"%s\" via package \"%.*s\"",
2617 leaf, (int)packlen, packname);
2618 }
8ebc5c01 2619 SETs(isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv);
a0d0e21e
LW
2620 RETURN;
2621}
22239a37 2622