This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
remove duplicate code and an extra branch in sv_setsv() and
[perl5.git] / pp_hot.c
CommitLineData
a0d0e21e
LW
1/* pp_hot.c
2 *
4eb8286e 3 * Copyright (c) 1991-1999, 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:
6fc92669
GS
736 if (SvIMMORTAL(sv)) {
737 if (relem <= lastrelem)
738 relem++;
739 break;
a0d0e21e
LW
740 }
741 if (relem <= lastrelem) {
742 sv_setsv(sv, *relem);
743 *(relem++) = sv;
744 }
745 else
3280af22 746 sv_setsv(sv, &PL_sv_undef);
a0d0e21e
LW
747 SvSETMAGIC(sv);
748 break;
749 }
750 }
3280af22
NIS
751 if (PL_delaymagic & ~DM_DELAY) {
752 if (PL_delaymagic & DM_UID) {
a0d0e21e 753#ifdef HAS_SETRESUID
b28d0864 754 (void)setresuid(PL_uid,PL_euid,(Uid_t)-1);
56febc5e
AD
755#else
756# ifdef HAS_SETREUID
3280af22 757 (void)setreuid(PL_uid,PL_euid);
56febc5e
AD
758# else
759# ifdef HAS_SETRUID
b28d0864
NIS
760 if ((PL_delaymagic & DM_UID) == DM_RUID) {
761 (void)setruid(PL_uid);
762 PL_delaymagic &= ~DM_RUID;
a0d0e21e 763 }
56febc5e
AD
764# endif /* HAS_SETRUID */
765# ifdef HAS_SETEUID
b28d0864
NIS
766 if ((PL_delaymagic & DM_UID) == DM_EUID) {
767 (void)seteuid(PL_uid);
768 PL_delaymagic &= ~DM_EUID;
a0d0e21e 769 }
56febc5e 770# endif /* HAS_SETEUID */
b28d0864
NIS
771 if (PL_delaymagic & DM_UID) {
772 if (PL_uid != PL_euid)
a0d0e21e 773 DIE("No setreuid available");
b28d0864 774 (void)PerlProc_setuid(PL_uid);
a0d0e21e 775 }
56febc5e
AD
776# endif /* HAS_SETREUID */
777#endif /* HAS_SETRESUID */
3280af22
NIS
778 PL_uid = (int)PerlProc_getuid();
779 PL_euid = (int)PerlProc_geteuid();
a0d0e21e 780 }
3280af22 781 if (PL_delaymagic & DM_GID) {
a0d0e21e 782#ifdef HAS_SETRESGID
b28d0864 783 (void)setresgid(PL_gid,PL_egid,(Gid_t)-1);
56febc5e
AD
784#else
785# ifdef HAS_SETREGID
3280af22 786 (void)setregid(PL_gid,PL_egid);
56febc5e
AD
787# else
788# ifdef HAS_SETRGID
b28d0864
NIS
789 if ((PL_delaymagic & DM_GID) == DM_RGID) {
790 (void)setrgid(PL_gid);
791 PL_delaymagic &= ~DM_RGID;
a0d0e21e 792 }
56febc5e
AD
793# endif /* HAS_SETRGID */
794# ifdef HAS_SETEGID
b28d0864
NIS
795 if ((PL_delaymagic & DM_GID) == DM_EGID) {
796 (void)setegid(PL_gid);
797 PL_delaymagic &= ~DM_EGID;
a0d0e21e 798 }
56febc5e 799# endif /* HAS_SETEGID */
b28d0864
NIS
800 if (PL_delaymagic & DM_GID) {
801 if (PL_gid != PL_egid)
a0d0e21e 802 DIE("No setregid available");
b28d0864 803 (void)PerlProc_setgid(PL_gid);
a0d0e21e 804 }
56febc5e
AD
805# endif /* HAS_SETREGID */
806#endif /* HAS_SETRESGID */
3280af22
NIS
807 PL_gid = (int)PerlProc_getgid();
808 PL_egid = (int)PerlProc_getegid();
a0d0e21e 809 }
3280af22 810 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
a0d0e21e 811 }
3280af22 812 PL_delaymagic = 0;
54310121
PP
813
814 gimme = GIMME_V;
815 if (gimme == G_VOID)
816 SP = firstrelem - 1;
817 else if (gimme == G_SCALAR) {
818 dTARGET;
819 SP = firstrelem;
820 SETi(lastrelem - firstrelem + 1);
821 }
822 else {
a0d0e21e
LW
823 if (ary || hash)
824 SP = lastrelem;
825 else
826 SP = firstrelem + (lastlelem - firstlelem);
0c8c7a05 827 lelem = firstlelem + (relem - firstrelem);
5f05dabc 828 while (relem <= SP)
3280af22 829 *relem++ = (lelem <= lastlelem) ? *lelem++ : &PL_sv_undef;
a0d0e21e 830 }
54310121 831 RETURN;
a0d0e21e
LW
832}
833
8782bef2
GB
834PP(pp_qr)
835{
836 djSP;
837 register PMOP *pm = cPMOP;
838 SV *rv = sv_newmortal();
57668c4d 839 SV *sv = newSVrv(rv, "Regexp");
8782bef2
GB
840 sv_magic(sv,(SV*)ReREFCNT_inc(pm->op_pmregexp),'r',0,0);
841 RETURNX(PUSHs(rv));
842}
843
a0d0e21e
LW
844PP(pp_match)
845{
4e35701f 846 djSP; dTARG;
a0d0e21e
LW
847 register PMOP *pm = cPMOP;
848 register char *t;
849 register char *s;
850 char *strend;
851 I32 global;
0ef3e39e 852 I32 r_flags = 0;
a0d0e21e 853 char *truebase;
d9f97599 854 register REGEXP *rx = pm->op_pmregexp;
b3eb6a9b 855 bool rxtainted;
a0d0e21e
LW
856 I32 gimme = GIMME;
857 STRLEN len;
748a9306 858 I32 minmatch = 0;
3280af22 859 I32 oldsave = PL_savestack_ix;
f86702cc 860 I32 update_minmatch = 1;
e60df1fa 861 I32 had_zerolen = 0;
a0d0e21e 862
533c011a 863 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e
LW
864 TARG = POPs;
865 else {
54b9620d 866 TARG = DEFSV;
a0d0e21e
LW
867 EXTEND(SP,1);
868 }
c277df42 869 PUTBACK; /* EVAL blocks need stack_sp. */
a0d0e21e
LW
870 s = SvPV(TARG, len);
871 strend = s + len;
872 if (!s)
873 DIE("panic: do_match");
b3eb6a9b 874 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
3280af22 875 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
9212bbba 876 TAINT_NOT;
a0d0e21e 877
48c036b1 878 if (pm->op_pmdynflags & PMdf_USED) {
c277df42 879 failure:
a0d0e21e
LW
880 if (gimme == G_ARRAY)
881 RETURN;
882 RETPUSHNO;
883 }
884
3280af22
NIS
885 if (!rx->prelen && PL_curpm) {
886 pm = PL_curpm;
d9f97599 887 rx = pm->op_pmregexp;
a0d0e21e 888 }
d9f97599 889 if (rx->minlen > len) goto failure;
c277df42 890
a0d0e21e 891 truebase = t = s;
ad94a511
IZ
892
893 /* XXXX What part of this is needed with true \G-support? */
a0d0e21e 894 if (global = pm->op_pmflags & PMf_GLOBAL) {
d9f97599 895 rx->startp[0] = 0;
a0d0e21e
LW
896 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
897 MAGIC* mg = mg_find(TARG, 'g');
565764a8 898 if (mg && mg->mg_len >= 0) {
b7a35066
IZ
899 if (!(rx->reganch & ROPT_GPOS_SEEN))
900 rx->endp[0] = rx->startp[0] = s + mg->mg_len;
0ef3e39e
HS
901 else if (rx->reganch & ROPT_ANCH_GPOS) {
902 r_flags |= REXEC_IGNOREPOS;
903 rx->endp[0] = rx->startp[0] = s + mg->mg_len;
904 }
748a9306 905 minmatch = (mg->mg_flags & MGf_MINMATCH);
f86702cc 906 update_minmatch = 0;
748a9306 907 }
a0d0e21e
LW
908 }
909 }
0ef3e39e
HS
910 if ((gimme != G_ARRAY && !global && rx->nparens)
911 || SvTEMP(TARG) || PL_sawampersand)
912 r_flags |= REXEC_COPY_STR;
22e551b9
IZ
913 if (SvSCREAM(TARG) && rx->check_substr
914 && SvTYPE(rx->check_substr) == SVt_PVBM
915 && SvVALID(rx->check_substr))
916 r_flags |= REXEC_SCREAM;
917
a0d0e21e 918 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
3280af22
NIS
919 SAVEINT(PL_multiline);
920 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
a0d0e21e
LW
921 }
922
923play_it_again:
d9f97599
GS
924 if (global && rx->startp[0]) {
925 t = s = rx->endp[0];
926 if ((s + rx->minlen) > strend)
a0d0e21e 927 goto nope;
f86702cc 928 if (update_minmatch++)
e60df1fa 929 minmatch = had_zerolen;
a0d0e21e 930 }
d9f97599
GS
931 if (rx->check_substr) {
932 if (!(rx->reganch & ROPT_NOSCAN)) { /* Floating checkstring. */
22e551b9 933 if (r_flags & REXEC_SCREAM) {
c277df42 934 I32 p = -1;
a0ed51b3 935 char *b;
c277df42 936
3280af22 937 if (PL_screamfirst[BmRARE(rx->check_substr)] < 0)
a0d0e21e 938 goto nope;
a0ed51b3 939
dfe13c55 940 b = (char*)HOP((U8*)s, rx->check_offset_min);
a0ed51b3 941 if (!(s = screaminstr(TARG, rx->check_substr, b - s, 0, &p, 0)))
a0d0e21e 942 goto nope;
a0ed51b3
LW
943
944 if ((rx->reganch & ROPT_CHECK_ALL)
3280af22 945 && !PL_sawampersand && !SvTAIL(rx->check_substr))
a0d0e21e
LW
946 goto yup;
947 }
a0ed51b3 948 else if (!(s = fbm_instr((unsigned char*)HOP((U8*)s, rx->check_offset_min),
c277df42 949 (unsigned char*)strend,
411d5715 950 rx->check_substr, 0)))
a0d0e21e 951 goto nope;
3280af22 952 else if ((rx->reganch & ROPT_CHECK_ALL) && !PL_sawampersand)
a0d0e21e 953 goto yup;
ee027f1d 954 if (s && rx->check_offset_max < s - t) {
d9f97599 955 ++BmUSEFUL(rx->check_substr);
dfe13c55 956 s = (char*)HOP((U8*)s, -rx->check_offset_max);
a0d0e21e
LW
957 }
958 else
959 s = t;
960 }
c277df42
IZ
961 /* Now checkstring is fixed, i.e. at fixed offset from the
962 beginning of match, and the match is anchored at s. */
3280af22 963 else if (!PL_multiline) { /* Anchored near beginning of string. */
c277df42 964 I32 slen;
dfe13c55 965 char *b = (char*)HOP((U8*)s, rx->check_offset_min);
a0ed51b3 966 if (*SvPVX(rx->check_substr) != *b
d9f97599 967 || ((slen = SvCUR(rx->check_substr)) > 1
a0ed51b3 968 && memNE(SvPVX(rx->check_substr), b, slen)))
bbce6d69 969 goto nope;
a0d0e21e 970 }
a0ed51b3 971 if (!(rx->reganch & ROPT_NAUGHTY) && --BmUSEFUL(rx->check_substr) < 0
d9f97599
GS
972 && rx->check_substr == rx->float_substr) {
973 SvREFCNT_dec(rx->check_substr);
974 rx->check_substr = Nullsv; /* opt is being useless */
975 rx->float_substr = Nullsv;
a0d0e21e
LW
976 }
977 }
22e551b9 978 if (CALLREGEXEC(rx, s, strend, truebase, minmatch, TARG, NULL, r_flags))
bbce6d69 979 {
3280af22 980 PL_curpm = pm;
a0d0e21e 981 if (pm->op_pmflags & PMf_ONCE)
48c036b1 982 pm->op_pmdynflags |= PMdf_USED;
a0d0e21e
LW
983 goto gotcha;
984 }
985 else
986 goto ret_no;
987 /*NOTREACHED*/
988
989 gotcha:
72311751
GS
990 if (rxtainted)
991 RX_MATCH_TAINTED_on(rx);
992 TAINT_IF(RX_MATCH_TAINTED(rx));
a0d0e21e
LW
993 if (gimme == G_ARRAY) {
994 I32 iters, i, len;
995
d9f97599 996 iters = rx->nparens;
a0d0e21e
LW
997 if (global && !iters)
998 i = 1;
999 else
1000 i = 0;
c277df42 1001 SPAGAIN; /* EVAL blocks could move the stack. */
a0d0e21e 1002 EXTEND(SP, iters + i);
bbce6d69 1003 EXTEND_MORTAL(iters + i);
a0d0e21e
LW
1004 for (i = !i; i <= iters; i++) {
1005 PUSHs(sv_newmortal());
1006 /*SUPPRESS 560*/
d9f97599
GS
1007 if ((s = rx->startp[i]) && rx->endp[i] ) {
1008 len = rx->endp[i] - s;
a0d0e21e
LW
1009 sv_setpvn(*SP, s, len);
1010 }
1011 }
1012 if (global) {
d9f97599
GS
1013 truebase = rx->subbeg;
1014 strend = rx->subend;
e60df1fa 1015 had_zerolen = (rx->startp[0] && rx->startp[0] == rx->endp[0]);
c277df42 1016 PUTBACK; /* EVAL blocks may use stack */
ad94a511 1017 r_flags |= REXEC_IGNOREPOS;
a0d0e21e
LW
1018 goto play_it_again;
1019 }
f7e33566 1020 else if (!iters)
bde848c5 1021 XPUSHs(&PL_sv_yes);
4633a7c4 1022 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1023 RETURN;
1024 }
1025 else {
1026 if (global) {
1027 MAGIC* mg = 0;
1028 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
1029 mg = mg_find(TARG, 'g');
1030 if (!mg) {
1031 sv_magic(TARG, (SV*)0, 'g', Nullch, 0);
1032 mg = mg_find(TARG, 'g');
1033 }
d9f97599
GS
1034 if (rx->startp[0]) {
1035 mg->mg_len = rx->endp[0] - rx->subbeg;
1036 if (rx->startp[0] == rx->endp[0])
748a9306
LW
1037 mg->mg_flags |= MGf_MINMATCH;
1038 else
1039 mg->mg_flags &= ~MGf_MINMATCH;
1040 }
a0d0e21e 1041 }
4633a7c4 1042 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1043 RETPUSHYES;
1044 }
1045
c277df42 1046yup: /* Confirmed by check_substr */
72311751
GS
1047 if (rxtainted)
1048 RX_MATCH_TAINTED_on(rx);
1049 TAINT_IF(RX_MATCH_TAINTED(rx));
d9f97599 1050 ++BmUSEFUL(rx->check_substr);
3280af22 1051 PL_curpm = pm;
a0d0e21e 1052 if (pm->op_pmflags & PMf_ONCE)
48c036b1 1053 pm->op_pmdynflags |= PMdf_USED;
d9f97599
GS
1054 Safefree(rx->subbase);
1055 rx->subbase = Nullch;
a0d0e21e 1056 if (global) {
d9f97599
GS
1057 rx->subbeg = truebase;
1058 rx->subend = strend;
1059 rx->startp[0] = s;
1060 rx->endp[0] = s + SvCUR(rx->check_substr);
a0d0e21e
LW
1061 goto gotcha;
1062 }
3280af22 1063 if (PL_sawampersand) {
a0d0e21e
LW
1064 char *tmps;
1065
d9f97599
GS
1066 tmps = rx->subbase = savepvn(t, strend-t);
1067 rx->subbeg = tmps;
1068 rx->subend = tmps + (strend-t);
1069 tmps = rx->startp[0] = tmps + (s - t);
1070 rx->endp[0] = tmps + SvCUR(rx->check_substr);
a0d0e21e 1071 }
4633a7c4 1072 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1073 RETPUSHYES;
1074
1075nope:
d9f97599
GS
1076 if (rx->check_substr)
1077 ++BmUSEFUL(rx->check_substr);
a0d0e21e
LW
1078
1079ret_no:
c90c0ff4 1080 if (global && !(pm->op_pmflags & PMf_CONTINUE)) {
a0d0e21e
LW
1081 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
1082 MAGIC* mg = mg_find(TARG, 'g');
1083 if (mg)
565764a8 1084 mg->mg_len = -1;
a0d0e21e
LW
1085 }
1086 }
4633a7c4 1087 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1088 if (gimme == G_ARRAY)
1089 RETURN;
1090 RETPUSHNO;
1091}
1092
1093OP *
8ac85365 1094do_readline(void)
a0d0e21e
LW
1095{
1096 dSP; dTARGETSTACKED;
1097 register SV *sv;
1098 STRLEN tmplen = 0;
1099 STRLEN offset;
760ac839 1100 PerlIO *fp;
3280af22 1101 register IO *io = GvIO(PL_last_in_gv);
533c011a 1102 register I32 type = PL_op->op_type;
54310121 1103 I32 gimme = GIMME_V;
e79b0511 1104 MAGIC *mg;
a0d0e21e 1105
33c27489 1106 if (mg = SvTIED_mg((SV*)PL_last_in_gv, 'q')) {
e79b0511 1107 PUSHMARK(SP);
33c27489 1108 XPUSHs(SvTIED_obj((SV*)PL_last_in_gv, mg));
e79b0511
PP
1109 PUTBACK;
1110 ENTER;
54310121 1111 perl_call_method("READLINE", gimme);
e79b0511
PP
1112 LEAVE;
1113 SPAGAIN;
54310121
PP
1114 if (gimme == G_SCALAR)
1115 SvSetMagicSV_nosteal(TARG, TOPs);
e79b0511
PP
1116 RETURN;
1117 }
a0d0e21e
LW
1118 fp = Nullfp;
1119 if (io) {
1120 fp = IoIFP(io);
1121 if (!fp) {
1122 if (IoFLAGS(io) & IOf_ARGV) {
1123 if (IoFLAGS(io) & IOf_START) {
1124 IoFLAGS(io) &= ~IOf_START;
1125 IoLINES(io) = 0;
3280af22 1126 if (av_len(GvAVn(PL_last_in_gv)) < 0) {
9d116dd7 1127 do_open(PL_last_in_gv,"-",1,FALSE,O_RDONLY,0,Nullfp);
3280af22
NIS
1128 sv_setpvn(GvSV(PL_last_in_gv), "-", 1);
1129 SvSETMAGIC(GvSV(PL_last_in_gv));
a2008d6d
GS
1130 fp = IoIFP(io);
1131 goto have_fp;
a0d0e21e
LW
1132 }
1133 }
3280af22 1134 fp = nextargv(PL_last_in_gv);
a0d0e21e 1135 if (!fp) { /* Note: fp != IoIFP(io) */
3280af22 1136 (void)do_close(PL_last_in_gv, FALSE); /* now it does*/
a0d0e21e
LW
1137 IoFLAGS(io) |= IOf_START;
1138 }
1139 }
1140 else if (type == OP_GLOB) {
1141 SV *tmpcmd = NEWSV(55, 0);
1142 SV *tmpglob = POPs;
1143 ENTER;
1144 SAVEFREESV(tmpcmd);
1145#ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
1146 /* since spawning off a process is a real performance hit */
1147 {
1148#include <descrip.h>
1149#include <lib$routines.h>
1150#include <nam.h>
1151#include <rmsdef.h>
1152 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
1153 char vmsspec[NAM$C_MAXRSS+1];
1154 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
1155 char tmpfnam[L_tmpnam] = "SYS$SCRATCH:";
1156 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
760ac839 1157 PerlIO *tmpfp;
a0d0e21e
LW
1158 STRLEN i;
1159 struct dsc$descriptor_s wilddsc
1160 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
1161 struct dsc$descriptor_vs rsdsc
1162 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
1163 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
1164
1165 /* We could find out if there's an explicit dev/dir or version
1166 by peeking into lib$find_file's internal context at
1167 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
1168 but that's unsupported, so I don't want to do it now and
1169 have it bite someone in the future. */
6ad3d225 1170 strcat(tmpfnam,PerlLIO_tmpnam(NULL));
a0d0e21e
LW
1171 cp = SvPV(tmpglob,i);
1172 for (; i; i--) {
1173 if (cp[i] == ';') hasver = 1;
1174 if (cp[i] == '.') {
1175 if (sts) hasver = 1;
1176 else sts = 1;
1177 }
1178 if (cp[i] == '/') {
1179 hasdir = isunix = 1;
1180 break;
748a9306 1181 }
a0d0e21e
LW
1182 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
1183 hasdir = 1;
1184 break;
1185 }
1186 }
760ac839 1187 if ((tmpfp = PerlIO_open(tmpfnam,"w+","fop=dlt")) != NULL) {
09b7f37c 1188 Stat_t st;
c6ed36e1 1189 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
09b7f37c
CB
1190 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
1191 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
a0d0e21e
LW
1192 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
1193 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
1194 &dfltdsc,NULL,NULL,NULL))&1)) {
1195 end = rstr + (unsigned long int) *rslt;
1196 if (!hasver) while (*end != ';') end--;
1197 *(end++) = '\n'; *end = '\0';
1198 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
1199 if (hasdir) {
f86702cc 1200 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
a0d0e21e
LW
1201 begin = rstr;
1202 }
1203 else {
1204 begin = end;
1205 while (*(--begin) != ']' && *begin != '>') ;
1206 ++begin;
1207 }
760ac839 1208 ok = (PerlIO_puts(tmpfp,begin) != EOF);
a0d0e21e
LW
1209 }
1210 if (cxt) (void)lib$find_file_end(&cxt);
748a9306
LW
1211 if (ok && sts != RMS$_NMF &&
1212 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
a0d0e21e 1213 if (!ok) {
c07a80fd
PP
1214 if (!(sts & 1)) {
1215 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
1216 }
760ac839 1217 PerlIO_close(tmpfp);
a0d0e21e
LW
1218 fp = NULL;
1219 }
1220 else {
760ac839 1221 PerlIO_rewind(tmpfp);
a0d0e21e
LW
1222 IoTYPE(io) = '<';
1223 IoIFP(io) = fp = tmpfp;
1e422769 1224 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
a0d0e21e
LW
1225 }
1226 }
1227 }
1228#else /* !VMS */
1229#ifdef DOSISH
67955e0c
PP
1230#ifdef OS2
1231 sv_setpv(tmpcmd, "for a in ");
1232 sv_catsv(tmpcmd, tmpglob);
1233 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
1234#else
39e571d4
ML
1235#ifdef DJGPP
1236 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
1237 sv_catsv(tmpcmd, tmpglob);
1238#else
a0d0e21e
LW
1239 sv_setpv(tmpcmd, "perlglob ");
1240 sv_catsv(tmpcmd, tmpglob);
1241 sv_catpv(tmpcmd, " |");
39e571d4 1242#endif /* !DJGPP */
67955e0c
PP
1243#endif /* !OS2 */
1244#else /* !DOSISH */
1245#if defined(CSH)
3280af22 1246 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
a0d0e21e
LW
1247 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
1248 sv_catsv(tmpcmd, tmpglob);
16d20bd9 1249 sv_catpv(tmpcmd, "' 2>/dev/null |");
a0d0e21e
LW
1250#else
1251 sv_setpv(tmpcmd, "echo ");
1252 sv_catsv(tmpcmd, tmpglob);
1253#if 'z' - 'a' == 25
1254 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
1255#else
1256 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
1257#endif
1258#endif /* !CSH */
67955e0c 1259#endif /* !DOSISH */
3280af22 1260 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
9d116dd7 1261 FALSE, O_RDONLY, 0, Nullfp);
a0d0e21e
LW
1262 fp = IoIFP(io);
1263#endif /* !VMS */
1264 LEAVE;
1265 }
1266 }
1267 else if (type == OP_GLOB)
1268 SP--;
1269 }
1270 if (!fp) {
599cee73
PM
1271 if (ckWARN(WARN_CLOSED) && io && !(IoFLAGS(io) & IOf_START))
1272 warner(WARN_CLOSED,
1273 "Read on closed filehandle <%s>", GvENAME(PL_last_in_gv));
54310121 1274 if (gimme == G_SCALAR) {
a0d0e21e
LW
1275 (void)SvOK_off(TARG);
1276 PUSHTARG;
1277 }
1278 RETURN;
1279 }
a2008d6d 1280 have_fp:
54310121 1281 if (gimme == G_SCALAR) {
a0d0e21e 1282 sv = TARG;
9607fc9c
PP
1283 if (SvROK(sv))
1284 sv_unref(sv);
a0d0e21e
LW
1285 (void)SvUPGRADE(sv, SVt_PV);
1286 tmplen = SvLEN(sv); /* remember if already alloced */
1287 if (!tmplen)
1288 Sv_Grow(sv, 80); /* try short-buffering it */
1289 if (type == OP_RCATLINE)
1290 offset = SvCUR(sv);
1291 else
1292 offset = 0;
1293 }
54310121
PP
1294 else {
1295 sv = sv_2mortal(NEWSV(57, 80));
1296 offset = 0;
1297 }
fbad3eb5
GS
1298
1299/* flip-flop EOF state for a snarfed empty file */
1300#define SNARF_EOF(gimme,rs,io,sv) \
1301 ((gimme != G_SCALAR || SvCUR(sv) \
1302 || (IoFLAGS(io) & IOf_NOLINE) || IoLINES(io) || !RsSNARF(rs)) \
1303 ? ((IoFLAGS(io) &= ~IOf_NOLINE), TRUE) \
1304 : ((IoFLAGS(io) |= IOf_NOLINE), FALSE))
1305
a0d0e21e 1306 for (;;) {
fbad3eb5
GS
1307 if (!sv_gets(sv, fp, offset)
1308 && (type == OP_GLOB || SNARF_EOF(gimme, PL_rs, io, sv)))
1309 {
760ac839 1310 PerlIO_clearerr(fp);
a0d0e21e 1311 if (IoFLAGS(io) & IOf_ARGV) {
3280af22 1312 fp = nextargv(PL_last_in_gv);
a0d0e21e
LW
1313 if (fp)
1314 continue;
3280af22 1315 (void)do_close(PL_last_in_gv, FALSE);
a0d0e21e
LW
1316 IoFLAGS(io) |= IOf_START;
1317 }
1318 else if (type == OP_GLOB) {
4eb79ab5
GS
1319 if (!do_close(PL_last_in_gv, FALSE) && ckWARN(WARN_CLOSED)) {
1320 warner(WARN_CLOSED,
1321 "glob failed (child exited with status %d%s)",
1322 STATUS_CURRENT >> 8,
cf494569 1323 (STATUS_CURRENT & 0x80) ? ", core dumped" : "");
4eb79ab5 1324 }
a0d0e21e 1325 }
54310121 1326 if (gimme == G_SCALAR) {
a0d0e21e
LW
1327 (void)SvOK_off(TARG);
1328 PUSHTARG;
1329 }
1330 RETURN;
1331 }
bbce6d69
PP
1332 /* This should not be marked tainted if the fp is marked clean */
1333 if (!(IoFLAGS(io) & IOf_UNTAINT)) {
1334 TAINT;
1335 SvTAINTED_on(sv);
1336 }
a0d0e21e 1337 IoLINES(io)++;
71be2cbc 1338 SvSETMAGIC(sv);
a0d0e21e 1339 XPUSHs(sv);
a0d0e21e
LW
1340 if (type == OP_GLOB) {
1341 char *tmps;
1342
3280af22 1343 if (SvCUR(sv) > 0 && SvCUR(PL_rs) > 0) {
c07a80fd 1344 tmps = SvEND(sv) - 1;
3280af22 1345 if (*tmps == *SvPVX(PL_rs)) {
c07a80fd
PP
1346 *tmps = '\0';
1347 SvCUR(sv)--;
1348 }
1349 }
a0d0e21e
LW
1350 for (tmps = SvPVX(sv); *tmps; tmps++)
1351 if (!isALPHA(*tmps) && !isDIGIT(*tmps) &&
1352 strchr("$&*(){}[]'\";\\|?<>~`", *tmps))
1353 break;
3280af22 1354 if (*tmps && PerlLIO_stat(SvPVX(sv), &PL_statbuf) < 0) {
a0d0e21e
LW
1355 (void)POPs; /* Unmatched wildcard? Chuck it... */
1356 continue;
1357 }
1358 }
54310121 1359 if (gimme == G_ARRAY) {
a0d0e21e
LW
1360 if (SvLEN(sv) - SvCUR(sv) > 20) {
1361 SvLEN_set(sv, SvCUR(sv)+1);
1362 Renew(SvPVX(sv), SvLEN(sv), char);
1363 }
1364 sv = sv_2mortal(NEWSV(58, 80));
1365 continue;
1366 }
54310121 1367 else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
a0d0e21e
LW
1368 /* try to reclaim a bit of scalar space (only on 1st alloc) */
1369 if (SvCUR(sv) < 60)
1370 SvLEN_set(sv, 80);
1371 else
1372 SvLEN_set(sv, SvCUR(sv)+40); /* allow some slop */
1373 Renew(SvPVX(sv), SvLEN(sv), char);
1374 }
1375 RETURN;
1376 }
1377}
1378
1379PP(pp_enter)
1380{
4e35701f 1381 djSP;
c09156bb 1382 register PERL_CONTEXT *cx;
533c011a 1383 I32 gimme = OP_GIMME(PL_op, -1);
a0d0e21e 1384
54310121
PP
1385 if (gimme == -1) {
1386 if (cxstack_ix >= 0)
1387 gimme = cxstack[cxstack_ix].blk_gimme;
1388 else
1389 gimme = G_SCALAR;
1390 }
a0d0e21e
LW
1391
1392 ENTER;
1393
1394 SAVETMPS;
924508f0 1395 PUSHBLOCK(cx, CXt_BLOCK, SP);
a0d0e21e
LW
1396
1397 RETURN;
1398}
1399
1400PP(pp_helem)
1401{
4e35701f 1402 djSP;
760ac839 1403 HE* he;
ae77835f 1404 SV **svp;
a0d0e21e 1405 SV *keysv = POPs;
a0d0e21e 1406 HV *hv = (HV*)POPs;
533c011a
NIS
1407 U32 lval = PL_op->op_flags & OPf_MOD;
1408 U32 defer = PL_op->op_private & OPpLVAL_DEFER;
be6c24e0 1409 SV *sv;
a0d0e21e 1410
ae77835f
MB
1411 if (SvTYPE(hv) == SVt_PVHV) {
1412 he = hv_fetch_ent(hv, keysv, lval && !defer, 0);
97fcbf96 1413 svp = he ? &HeVAL(he) : 0;
ae77835f
MB
1414 }
1415 else if (SvTYPE(hv) == SVt_PVAV) {
0ebe0038
SM
1416 if (PL_op->op_private & OPpLVAL_INTRO)
1417 DIE("Can't localize pseudo-hash element");
97fcbf96 1418 svp = avhv_fetch_ent((AV*)hv, keysv, lval && !defer, 0);
ae77835f 1419 }
c750a3ec 1420 else {
a0d0e21e 1421 RETPUSHUNDEF;
c750a3ec 1422 }
a0d0e21e 1423 if (lval) {
3280af22 1424 if (!svp || *svp == &PL_sv_undef) {
68dc0745
PP
1425 SV* lv;
1426 SV* key2;
2d8e6c8d
GS
1427 if (!defer) {
1428 STRLEN n_a;
1429 DIE(PL_no_helem, SvPV(keysv, n_a));
1430 }
68dc0745
PP
1431 lv = sv_newmortal();
1432 sv_upgrade(lv, SVt_PVLV);
1433 LvTYPE(lv) = 'y';
1434 sv_magic(lv, key2 = newSVsv(keysv), 'y', Nullch, 0);
1435 SvREFCNT_dec(key2); /* sv_magic() increments refcount */
1436 LvTARG(lv) = SvREFCNT_inc(hv);
1437 LvTARGLEN(lv) = 1;
1438 PUSHs(lv);
1439 RETURN;
1440 }
533c011a 1441 if (PL_op->op_private & OPpLVAL_INTRO) {
ae77835f 1442 if (HvNAME(hv) && isGV(*svp))
533c011a 1443 save_gp((GV*)*svp, !(PL_op->op_flags & OPf_SPECIAL));
5f05dabc 1444 else
161b7d16 1445 save_helem(hv, keysv, svp);
5f05dabc 1446 }
533c011a
NIS
1447 else if (PL_op->op_private & OPpDEREF)
1448 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
a0d0e21e 1449 }
3280af22 1450 sv = (svp ? *svp : &PL_sv_undef);
be6c24e0
GS
1451 /* This makes C<local $tied{foo} = $tied{foo}> possible.
1452 * Pushing the magical RHS on to the stack is useless, since
1453 * that magic is soon destined to be misled by the local(),
1454 * and thus the later pp_sassign() will fail to mg_get() the
1455 * old value. This should also cure problems with delayed
1456 * mg_get()s. GSAR 98-07-03 */
1457 if (!lval && SvGMAGICAL(sv))
1458 sv = sv_mortalcopy(sv);
1459 PUSHs(sv);
a0d0e21e
LW
1460 RETURN;
1461}
1462
1463PP(pp_leave)
1464{
4e35701f 1465 djSP;
c09156bb 1466 register PERL_CONTEXT *cx;
a0d0e21e
LW
1467 register SV **mark;
1468 SV **newsp;
1469 PMOP *newpm;
1470 I32 gimme;
1471
533c011a 1472 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e 1473 cx = &cxstack[cxstack_ix];
3280af22 1474 cx->blk_oldpm = PL_curpm; /* fake block should preserve $1 et al */
a0d0e21e
LW
1475 }
1476
1477 POPBLOCK(cx,newpm);
1478
533c011a 1479 gimme = OP_GIMME(PL_op, -1);
54310121
PP
1480 if (gimme == -1) {
1481 if (cxstack_ix >= 0)
1482 gimme = cxstack[cxstack_ix].blk_gimme;
1483 else
1484 gimme = G_SCALAR;
1485 }
a0d0e21e 1486
a1f49e72 1487 TAINT_NOT;
54310121
PP
1488 if (gimme == G_VOID)
1489 SP = newsp;
1490 else if (gimme == G_SCALAR) {
1491 MARK = newsp + 1;
1492 if (MARK <= SP)
1493 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
1494 *MARK = TOPs;
1495 else
1496 *MARK = sv_mortalcopy(TOPs);
a0d0e21e 1497 else {
54310121 1498 MEXTEND(mark,0);
3280af22 1499 *MARK = &PL_sv_undef;
a0d0e21e 1500 }
54310121 1501 SP = MARK;
a0d0e21e 1502 }
54310121 1503 else if (gimme == G_ARRAY) {
a1f49e72
CS
1504 /* in case LEAVE wipes old return values */
1505 for (mark = newsp + 1; mark <= SP; mark++) {
1506 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
a0d0e21e 1507 *mark = sv_mortalcopy(*mark);
a1f49e72
CS
1508 TAINT_NOT; /* Each item is independent */
1509 }
1510 }
a0d0e21e 1511 }
3280af22 1512 PL_curpm = newpm; /* Don't pop $1 et al till now */
a0d0e21e
LW
1513
1514 LEAVE;
1515
1516 RETURN;
1517}
1518
1519PP(pp_iter)
1520{
4e35701f 1521 djSP;
c09156bb 1522 register PERL_CONTEXT *cx;
5f05dabc 1523 SV* sv;
4633a7c4 1524 AV* av;
a0d0e21e 1525
924508f0 1526 EXTEND(SP, 1);
a0d0e21e 1527 cx = &cxstack[cxstack_ix];
6b35e009 1528 if (CxTYPE(cx) != CXt_LOOP)
a0d0e21e
LW
1529 DIE("panic: pp_iter");
1530
4633a7c4 1531 av = cx->blk_loop.iterary;
89ea2908
GA
1532 if (SvTYPE(av) != SVt_PVAV) {
1533 /* iterate ($min .. $max) */
1534 if (cx->blk_loop.iterlval) {
1535 /* string increment */
1536 register SV* cur = cx->blk_loop.iterlval;
1537 STRLEN maxlen;
1538 char *max = SvPV((SV*)av, maxlen);
1539 if (!SvNIOK(cur) && SvCUR(cur) <= maxlen) {
eaa5c2d6 1540#ifndef USE_THREADS /* don't risk potential race */
39ca2bb7
SM
1541 if (SvREFCNT(*cx->blk_loop.itervar) == 1
1542 && !SvMAGICAL(*cx->blk_loop.itervar))
1543 {
eaa5c2d6
GA
1544 /* safe to reuse old SV */
1545 sv_setsv(*cx->blk_loop.itervar, cur);
1546 }
1547 else
1548#endif
1549 {
1550 /* we need a fresh SV every time so that loop body sees a
1551 * completely new SV for closures/references to work as
1552 * they used to */
1553 SvREFCNT_dec(*cx->blk_loop.itervar);
1554 *cx->blk_loop.itervar = newSVsv(cur);
1555 }
89ea2908
GA
1556 if (strEQ(SvPVX(cur), max))
1557 sv_setiv(cur, 0); /* terminate next time */
1558 else
1559 sv_inc(cur);
1560 RETPUSHYES;
1561 }
1562 RETPUSHNO;
1563 }
1564 /* integer increment */
1565 if (cx->blk_loop.iterix > cx->blk_loop.itermax)
1566 RETPUSHNO;
7f61b687 1567
eaa5c2d6 1568#ifndef USE_THREADS /* don't risk potential race */
39ca2bb7
SM
1569 if (SvREFCNT(*cx->blk_loop.itervar) == 1
1570 && !SvMAGICAL(*cx->blk_loop.itervar))
1571 {
eaa5c2d6
GA
1572 /* safe to reuse old SV */
1573 sv_setiv(*cx->blk_loop.itervar, cx->blk_loop.iterix++);
1574 }
1575 else
1576#endif
1577 {
1578 /* we need a fresh SV every time so that loop body sees a
1579 * completely new SV for closures/references to work as they
1580 * used to */
1581 SvREFCNT_dec(*cx->blk_loop.itervar);
1582 *cx->blk_loop.itervar = newSViv(cx->blk_loop.iterix++);
1583 }
89ea2908
GA
1584 RETPUSHYES;
1585 }
1586
1587 /* iterate array */
3280af22 1588 if (cx->blk_loop.iterix >= (av == PL_curstack ? cx->blk_oldsp : AvFILL(av)))
4633a7c4 1589 RETPUSHNO;
a0d0e21e 1590
7a4c00b4 1591 SvREFCNT_dec(*cx->blk_loop.itervar);
a0d0e21e 1592
93965878
NIS
1593 if (sv = (SvMAGICAL(av))
1594 ? *av_fetch(av, ++cx->blk_loop.iterix, FALSE)
1595 : AvARRAY(av)[++cx->blk_loop.iterix])
a0d0e21e 1596 SvTEMP_off(sv);
a0d0e21e 1597 else
3280af22
NIS
1598 sv = &PL_sv_undef;
1599 if (av != PL_curstack && SvIMMORTAL(sv)) {
5f05dabc 1600 SV *lv = cx->blk_loop.iterlval;
71be2cbc
PP
1601 if (lv && SvREFCNT(lv) > 1) {
1602 SvREFCNT_dec(lv);
1603 lv = Nullsv;
1604 }
5f05dabc
PP
1605 if (lv)
1606 SvREFCNT_dec(LvTARG(lv));
1607 else {
68dc0745 1608 lv = cx->blk_loop.iterlval = NEWSV(26, 0);
5f05dabc 1609 sv_upgrade(lv, SVt_PVLV);
5f05dabc 1610 LvTYPE(lv) = 'y';
68dc0745 1611 sv_magic(lv, Nullsv, 'y', Nullch, 0);
5f05dabc
PP
1612 }
1613 LvTARG(lv) = SvREFCNT_inc(av);
1614 LvTARGOFF(lv) = cx->blk_loop.iterix;
8ac85365 1615 LvTARGLEN(lv) = (UV) -1;
5f05dabc
PP
1616 sv = (SV*)lv;
1617 }
a0d0e21e 1618
7a4c00b4 1619 *cx->blk_loop.itervar = SvREFCNT_inc(sv);
a0d0e21e
LW
1620 RETPUSHYES;
1621}
1622
1623PP(pp_subst)
1624{
4e35701f 1625 djSP; dTARG;
a0d0e21e
LW
1626 register PMOP *pm = cPMOP;
1627 PMOP *rpm = pm;
1628 register SV *dstr;
1629 register char *s;
1630 char *strend;
1631 register char *m;
1632 char *c;
1633 register char *d;
1634 STRLEN clen;
1635 I32 iters = 0;
1636 I32 maxiters;
1637 register I32 i;
1638 bool once;
71be2cbc 1639 bool rxtainted;
a0d0e21e 1640 char *orig;
22e551b9 1641 I32 r_flags;
d9f97599 1642 register REGEXP *rx = pm->op_pmregexp;
a0d0e21e
LW
1643 STRLEN len;
1644 int force_on_match = 0;
3280af22 1645 I32 oldsave = PL_savestack_ix;
c277df42 1646 I32 update_minmatch = 1;
a0d0e21e 1647
5cd24f17
PP
1648 /* known replacement string? */
1649 dstr = (pm->op_pmflags & PMf_CONST) ? POPs : Nullsv;
533c011a 1650 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e
LW
1651 TARG = POPs;
1652 else {
54b9620d 1653 TARG = DEFSV;
a0d0e21e 1654 EXTEND(SP,1);
8ec5e241 1655 }
68dc0745
PP
1656 if (SvREADONLY(TARG)
1657 || (SvTYPE(TARG) > SVt_PVLV
1658 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG))))
22c35a8c 1659 croak(PL_no_modify);
8ec5e241
NIS
1660 PUTBACK;
1661
a0d0e21e 1662 s = SvPV(TARG, len);
68dc0745 1663 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV)
a0d0e21e 1664 force_on_match = 1;
b3eb6a9b 1665 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
3280af22
NIS
1666 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
1667 if (PL_tainted)
b3eb6a9b 1668 rxtainted |= 2;
9212bbba 1669 TAINT_NOT;
a0d0e21e
LW
1670
1671 force_it:
1672 if (!pm || !s)
1673 DIE("panic: do_subst");
1674
1675 strend = s + len;
2beec16e
IZ
1676 maxiters = 2*(strend - s) + 10; /* We can match twice at each
1677 position, once with zero-length,
1678 second time with non-zero. */
a0d0e21e 1679
3280af22
NIS
1680 if (!rx->prelen && PL_curpm) {
1681 pm = PL_curpm;
d9f97599 1682 rx = pm->op_pmregexp;
a0d0e21e 1683 }
22e551b9 1684 r_flags = (rx->nparens || SvTEMP(TARG) || PL_sawampersand)
9d080a66 1685 ? REXEC_COPY_STR : 0;
22e551b9
IZ
1686 if (SvSCREAM(TARG) && rx->check_substr
1687 && SvTYPE(rx->check_substr) == SVt_PVBM
1688 && SvVALID(rx->check_substr))
1689 r_flags |= REXEC_SCREAM;
a0d0e21e 1690 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
3280af22
NIS
1691 SAVEINT(PL_multiline);
1692 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
a0d0e21e
LW
1693 }
1694 orig = m = s;
d9f97599
GS
1695 if (rx->check_substr) {
1696 if (!(rx->reganch & ROPT_NOSCAN)) { /* It floats. */
22e551b9 1697 if (r_flags & REXEC_SCREAM) {
c277df42 1698 I32 p = -1;
a0ed51b3 1699 char *b;
c277df42 1700
3280af22 1701 if (PL_screamfirst[BmRARE(rx->check_substr)] < 0)
a0d0e21e 1702 goto nope;
a0ed51b3 1703
dfe13c55 1704 b = (char*)HOP((U8*)s, rx->check_offset_min);
a0ed51b3 1705 if (!(s = screaminstr(TARG, rx->check_substr, b - s, 0, &p, 0)))
a0d0e21e
LW
1706 goto nope;
1707 }
a0ed51b3 1708 else if (!(s = fbm_instr((unsigned char*)HOP((U8*)s, rx->check_offset_min),
c277df42 1709 (unsigned char*)strend,
411d5715 1710 rx->check_substr, 0)))
a0d0e21e 1711 goto nope;
d9f97599
GS
1712 if (s && rx->check_offset_max < s - m) {
1713 ++BmUSEFUL(rx->check_substr);
dfe13c55 1714 s = (char*)HOP((U8*)s, -rx->check_offset_max);
a0d0e21e
LW
1715 }
1716 else
1717 s = m;
1718 }
c277df42
IZ
1719 /* Now checkstring is fixed, i.e. at fixed offset from the
1720 beginning of match, and the match is anchored at s. */
3280af22 1721 else if (!PL_multiline) { /* Anchored at beginning of string. */
c277df42 1722 I32 slen;
dfe13c55 1723 char *b = (char*)HOP((U8*)s, rx->check_offset_min);
a0ed51b3 1724 if (*SvPVX(rx->check_substr) != *b
d9f97599 1725 || ((slen = SvCUR(rx->check_substr)) > 1
a0ed51b3 1726 && memNE(SvPVX(rx->check_substr), b, slen)))
bbce6d69 1727 goto nope;
a0d0e21e 1728 }
a0ed51b3 1729 if (!(rx->reganch & ROPT_NAUGHTY) && --BmUSEFUL(rx->check_substr) < 0
d9f97599
GS
1730 && rx->check_substr == rx->float_substr) {
1731 SvREFCNT_dec(rx->check_substr);
1732 rx->check_substr = Nullsv; /* opt is being useless */
1733 rx->float_substr = Nullsv;
a0d0e21e
LW
1734 }
1735 }
71be2cbc
PP
1736
1737 /* only replace once? */
a0d0e21e 1738 once = !(rpm->op_pmflags & PMf_GLOBAL);
71be2cbc
PP
1739
1740 /* known replacement string? */
5cd24f17 1741 c = dstr ? SvPV(dstr, clen) : Nullch;
71be2cbc
PP
1742
1743 /* can do inplace substitution? */
22e551b9 1744 if (c && clen <= rx->minlen && (once || !(r_flags & REXEC_COPY_STR))
d9f97599 1745 && !(rx->reganch & ROPT_LOOKBEHIND_SEEN)) {
22e551b9 1746 if (!CALLREGEXEC(rx, s, strend, orig, 0, TARG, NULL, r_flags)) {
8ec5e241 1747 SPAGAIN;
3280af22 1748 PUSHs(&PL_sv_no);
71be2cbc
PP
1749 LEAVE_SCOPE(oldsave);
1750 RETURN;
1751 }
1752 if (force_on_match) {
1753 force_on_match = 0;
1754 s = SvPV_force(TARG, len);
1755 goto force_it;
1756 }
71be2cbc 1757 d = s;
3280af22 1758 PL_curpm = pm;
71be2cbc
PP
1759 SvSCREAM_off(TARG); /* disable possible screamer */
1760 if (once) {
48c036b1 1761 rxtainted |= RX_MATCH_TAINTED(rx);
d9f97599
GS
1762 if (rx->subbase) {
1763 m = orig + (rx->startp[0] - rx->subbase);
1764 d = orig + (rx->endp[0] - rx->subbase);
c277df42 1765 } else {
d9f97599
GS
1766 m = rx->startp[0];
1767 d = rx->endp[0];
c277df42 1768 }
71be2cbc
PP
1769 s = orig;
1770 if (m - s > strend - d) { /* faster to shorten from end */
1771 if (clen) {
1772 Copy(c, m, clen, char);
1773 m += clen;
a0d0e21e 1774 }
71be2cbc
PP
1775 i = strend - d;
1776 if (i > 0) {
1777 Move(d, m, i, char);
1778 m += i;
a0d0e21e 1779 }
71be2cbc
PP
1780 *m = '\0';
1781 SvCUR_set(TARG, m - s);
1782 }
1783 /*SUPPRESS 560*/
1784 else if (i = m - s) { /* faster from front */
1785 d -= clen;
1786 m = d;
1787 sv_chop(TARG, d-i);
1788 s += i;
1789 while (i--)
1790 *--d = *--s;
1791 if (clen)
1792 Copy(c, m, clen, char);
1793 }
1794 else if (clen) {
1795 d -= clen;
1796 sv_chop(TARG, d);
1797 Copy(c, d, clen, char);
1798 }
1799 else {
1800 sv_chop(TARG, d);
1801 }
48c036b1 1802 TAINT_IF(rxtainted & 1);
8ec5e241 1803 SPAGAIN;
3280af22 1804 PUSHs(&PL_sv_yes);
71be2cbc
PP
1805 }
1806 else {
71be2cbc
PP
1807 do {
1808 if (iters++ > maxiters)
1809 DIE("Substitution loop");
d9f97599
GS
1810 rxtainted |= RX_MATCH_TAINTED(rx);
1811 m = rx->startp[0];
71be2cbc
PP
1812 /*SUPPRESS 560*/
1813 if (i = m - s) {
1814 if (s != d)
1815 Move(s, d, i, char);
1816 d += i;
a0d0e21e 1817 }
71be2cbc
PP
1818 if (clen) {
1819 Copy(c, d, clen, char);
1820 d += clen;
1821 }
d9f97599 1822 s = rx->endp[0];
15e52e56 1823 } while (CALLREGEXEC(rx, s, strend, orig, s == m,
c277df42 1824 Nullsv, NULL, 0)); /* don't match same null twice */
71be2cbc
PP
1825 if (s != d) {
1826 i = strend - s;
1827 SvCUR_set(TARG, d - SvPVX(TARG) + i);
1828 Move(s, d, i+1, char); /* include the NUL */
a0d0e21e 1829 }
48c036b1 1830 TAINT_IF(rxtainted & 1);
8ec5e241 1831 SPAGAIN;
71be2cbc 1832 PUSHs(sv_2mortal(newSViv((I32)iters)));
a0d0e21e 1833 }
71be2cbc 1834 (void)SvPOK_only(TARG);
48c036b1 1835 TAINT_IF(rxtainted);
8ec5e241
NIS
1836 if (SvSMAGICAL(TARG)) {
1837 PUTBACK;
1838 mg_set(TARG);
1839 SPAGAIN;
1840 }
9212bbba 1841 SvTAINT(TARG);
71be2cbc
PP
1842 LEAVE_SCOPE(oldsave);
1843 RETURN;
a0d0e21e 1844 }
71be2cbc 1845
22e551b9 1846 if (CALLREGEXEC(rx, s, strend, orig, 0, TARG, NULL, r_flags)) {
a0d0e21e
LW
1847 if (force_on_match) {
1848 force_on_match = 0;
1849 s = SvPV_force(TARG, len);
1850 goto force_it;
1851 }
48c036b1 1852 rxtainted |= RX_MATCH_TAINTED(rx);
8ec5e241 1853 dstr = NEWSV(25, len);
a0d0e21e 1854 sv_setpvn(dstr, m, s-m);
3280af22 1855 PL_curpm = pm;
a0d0e21e 1856 if (!c) {
c09156bb 1857 register PERL_CONTEXT *cx;
8ec5e241 1858 SPAGAIN;
a0d0e21e
LW
1859 PUSHSUBST(cx);
1860 RETURNOP(cPMOP->op_pmreplroot);
1861 }
ad94a511 1862 r_flags |= REXEC_IGNOREPOS;
a0d0e21e
LW
1863 do {
1864 if (iters++ > maxiters)
1865 DIE("Substitution loop");
d9f97599
GS
1866 rxtainted |= RX_MATCH_TAINTED(rx);
1867 if (rx->subbase && rx->subbase != orig) {
a0d0e21e
LW
1868 m = s;
1869 s = orig;
d9f97599 1870 orig = rx->subbase;
a0d0e21e
LW
1871 s = orig + (m - s);
1872 strend = s + (strend - m);
1873 }
d9f97599 1874 m = rx->startp[0];
a0d0e21e 1875 sv_catpvn(dstr, s, m-s);
d9f97599 1876 s = rx->endp[0];
a0d0e21e
LW
1877 if (clen)
1878 sv_catpvn(dstr, c, clen);
1879 if (once)
1880 break;
ad94a511 1881 } while (CALLREGEXEC(rx, s, strend, orig, s == m, TARG, NULL, r_flags));
a0d0e21e 1882 sv_catpvn(dstr, s, strend - s);
748a9306 1883
4633a7c4 1884 (void)SvOOK_off(TARG);
cb0b1708 1885 Safefree(SvPVX(TARG));
748a9306
LW
1886 SvPVX(TARG) = SvPVX(dstr);
1887 SvCUR_set(TARG, SvCUR(dstr));
1888 SvLEN_set(TARG, SvLEN(dstr));
1889 SvPVX(dstr) = 0;
1890 sv_free(dstr);
1891
48c036b1 1892 TAINT_IF(rxtainted & 1);
f878fbec 1893 SPAGAIN;
48c036b1
GS
1894 PUSHs(sv_2mortal(newSViv((I32)iters)));
1895
a0d0e21e 1896 (void)SvPOK_only(TARG);
48c036b1 1897 TAINT_IF(rxtainted);
a0d0e21e 1898 SvSETMAGIC(TARG);
9212bbba 1899 SvTAINT(TARG);
4633a7c4 1900 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1901 RETURN;
1902 }
5cd24f17 1903 goto ret_no;
a0d0e21e
LW
1904
1905nope:
d9f97599 1906 ++BmUSEFUL(rx->check_substr);
5cd24f17 1907
8ec5e241
NIS
1908ret_no:
1909 SPAGAIN;
3280af22 1910 PUSHs(&PL_sv_no);
4633a7c4 1911 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1912 RETURN;
1913}
1914
1915PP(pp_grepwhile)
1916{
4e35701f 1917 djSP;
a0d0e21e
LW
1918
1919 if (SvTRUEx(POPs))
3280af22
NIS
1920 PL_stack_base[PL_markstack_ptr[-1]++] = PL_stack_base[*PL_markstack_ptr];
1921 ++*PL_markstack_ptr;
a0d0e21e
LW
1922 LEAVE; /* exit inner scope */
1923
1924 /* All done yet? */
3280af22 1925 if (PL_stack_base + *PL_markstack_ptr > SP) {
a0d0e21e 1926 I32 items;
54310121 1927 I32 gimme = GIMME_V;
a0d0e21e
LW
1928
1929 LEAVE; /* exit outer scope */
1930 (void)POPMARK; /* pop src */
3280af22 1931 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
a0d0e21e 1932 (void)POPMARK; /* pop dst */
3280af22 1933 SP = PL_stack_base + POPMARK; /* pop original mark */
54310121 1934 if (gimme == G_SCALAR) {
a0d0e21e
LW
1935 dTARGET;
1936 XPUSHi(items);
a0d0e21e 1937 }
54310121
PP
1938 else if (gimme == G_ARRAY)
1939 SP += items;
a0d0e21e
LW
1940 RETURN;
1941 }
1942 else {
1943 SV *src;
1944
1945 ENTER; /* enter inner scope */
3280af22 1946 SAVESPTR(PL_curpm);
a0d0e21e 1947
3280af22 1948 src = PL_stack_base[*PL_markstack_ptr];
a0d0e21e 1949 SvTEMP_off(src);
54b9620d 1950 DEFSV = src;
a0d0e21e
LW
1951
1952 RETURNOP(cLOGOP->op_other);
1953 }
1954}
1955
1956PP(pp_leavesub)
1957{
4e35701f 1958 djSP;
a0d0e21e
LW
1959 SV **mark;
1960 SV **newsp;
1961 PMOP *newpm;
1962 I32 gimme;
c09156bb 1963 register PERL_CONTEXT *cx;
f86702cc 1964 struct block_sub cxsub;
a0d0e21e
LW
1965
1966 POPBLOCK(cx,newpm);
f86702cc
PP
1967 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1968
a1f49e72 1969 TAINT_NOT;
a0d0e21e
LW
1970 if (gimme == G_SCALAR) {
1971 MARK = newsp + 1;
a29cdaf0
IZ
1972 if (MARK <= SP) {
1973 if (cxsub.cv && CvDEPTH(cxsub.cv) > 1) {
1974 if (SvTEMP(TOPs)) {
1975 *MARK = SvREFCNT_inc(TOPs);
1976 FREETMPS;
1977 sv_2mortal(*MARK);
1978 } else {
1979 FREETMPS;
1980 *MARK = sv_mortalcopy(TOPs);
1981 }
1982 } else
1983 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
1984 } else {
f86702cc 1985 MEXTEND(MARK, 0);
3280af22 1986 *MARK = &PL_sv_undef;
a0d0e21e
LW
1987 }
1988 SP = MARK;
1989 }
54310121 1990 else if (gimme == G_ARRAY) {
f86702cc 1991 for (MARK = newsp + 1; MARK <= SP; MARK++) {
a1f49e72 1992 if (!SvTEMP(*MARK)) {
f86702cc 1993 *MARK = sv_mortalcopy(*MARK);
a1f49e72
CS
1994 TAINT_NOT; /* Each item is independent */
1995 }
f86702cc 1996 }
a0d0e21e 1997 }
f86702cc
PP
1998 PUTBACK;
1999
2000 POPSUB2(); /* Stack values are safe: release CV and @_ ... */
3280af22 2001 PL_curpm = newpm; /* ... and pop $1 et al */
a0d0e21e
LW
2002
2003 LEAVE;
a0d0e21e
LW
2004 return pop_return();
2005}
2006
76e3520e 2007STATIC CV *
8ac85365 2008get_db_sub(SV **svp, CV *cv)
3de9ffa1
MB
2009{
2010 dTHR;
3280af22 2011 SV *dbsv = GvSV(PL_DBsub);
491527d0
GS
2012
2013 if (!PERLDB_SUB_NN) {
2014 GV *gv = CvGV(cv);
2015
2016 save_item(dbsv);
2017 if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED))
2018 || strEQ(GvNAME(gv), "END")
2019 || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */
2020 !( (SvTYPE(*svp) == SVt_PVGV) && (GvCV((GV*)*svp) == cv)
2021 && (gv = (GV*)*svp) ))) {
2022 /* Use GV from the stack as a fallback. */
2023 /* GV is potentially non-unique, or contain different CV. */
2024 sv_setsv(dbsv, newRV((SV*)cv));
2025 }
2026 else {
2027 gv_efullname3(dbsv, gv, Nullch);
2028 }
3de9ffa1
MB
2029 }
2030 else {
491527d0
GS
2031 SvUPGRADE(dbsv, SVt_PVIV);
2032 SvIOK_on(dbsv);
2033 SAVEIV(SvIVX(dbsv));
2034 SvIVX(dbsv) = (IV)cv; /* Do it the quickest way */
3de9ffa1 2035 }
491527d0 2036
3de9ffa1 2037 if (CvXSUB(cv))
3280af22
NIS
2038 PL_curcopdb = PL_curcop;
2039 cv = GvCV(PL_DBsub);
3de9ffa1
MB
2040 return cv;
2041}
2042
a0d0e21e
LW
2043PP(pp_entersub)
2044{
4e35701f 2045 djSP; dPOPss;
a0d0e21e
LW
2046 GV *gv;
2047 HV *stash;
2048 register CV *cv;
c09156bb 2049 register PERL_CONTEXT *cx;
5d94fbed 2050 I32 gimme;
533c011a 2051 bool hasargs = (PL_op->op_flags & OPf_STACKED) != 0;
a0d0e21e
LW
2052
2053 if (!sv)
2054 DIE("Not a CODE reference");
2055 switch (SvTYPE(sv)) {
2056 default:
2057 if (!SvROK(sv)) {
748a9306 2058 char *sym;
2d8e6c8d 2059 STRLEN n_a;
748a9306 2060
3280af22 2061 if (sv == &PL_sv_yes) { /* unfound import, ignore */
fb73857a 2062 if (hasargs)
3280af22 2063 SP = PL_stack_base + POPMARK;
a0d0e21e 2064 RETURN;
fb73857a 2065 }
15ff848f
CS
2066 if (SvGMAGICAL(sv)) {
2067 mg_get(sv);
2068 sym = SvPOKp(sv) ? SvPVX(sv) : Nullch;
2069 }
2070 else
2d8e6c8d 2071 sym = SvPV(sv, n_a);
15ff848f 2072 if (!sym)
22c35a8c 2073 DIE(PL_no_usym, "a subroutine");
533c011a 2074 if (PL_op->op_private & HINT_STRICT_REFS)
22c35a8c 2075 DIE(PL_no_symref, sym, "a subroutine");
748a9306 2076 cv = perl_get_cv(sym, TRUE);
a0d0e21e
LW
2077 break;
2078 }
f5284f61
IZ
2079 {
2080 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
2081 tryAMAGICunDEREF(to_cv);
2082 }
a0d0e21e
LW
2083 cv = (CV*)SvRV(sv);
2084 if (SvTYPE(cv) == SVt_PVCV)
2085 break;
2086 /* FALL THROUGH */
2087 case SVt_PVHV:
2088 case SVt_PVAV:
2089 DIE("Not a CODE reference");
2090 case SVt_PVCV:
2091 cv = (CV*)sv;
2092 break;
2093 case SVt_PVGV:
8ebc5c01 2094 if (!(cv = GvCVu((GV*)sv)))
a0d0e21e
LW
2095 cv = sv_2cv(sv, &stash, &gv, TRUE);
2096 break;
2097 }
2098
2099 ENTER;
2100 SAVETMPS;
2101
2102 retry:
2103 if (!cv)
2104 DIE("Not a CODE reference");
2105
2106 if (!CvROOT(cv) && !CvXSUB(cv)) {
44a8e56a 2107 GV* autogv;
22239a37 2108 SV* sub_name;
44a8e56a
PP
2109
2110 /* anonymous or undef'd function leaves us no recourse */
2111 if (CvANON(cv) || !(gv = CvGV(cv)))
2112 DIE("Undefined subroutine called");
2113 /* autoloaded stub? */
2114 if (cv != GvCV(gv)) {
2115 cv = GvCV(gv);
2116 goto retry;
a0d0e21e 2117 }
44a8e56a 2118 /* should call AUTOLOAD now? */
54310121
PP
2119 if ((autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
2120 FALSE)))
2121 {
44a8e56a
PP
2122 cv = GvCV(autogv);
2123 goto retry;
a0d0e21e 2124 }
44a8e56a 2125 /* sorry */
22239a37
NIS
2126 sub_name = sv_newmortal();
2127 gv_efullname3(sub_name, gv, Nullch);
2128 DIE("Undefined subroutine &%s called", SvPVX(sub_name));
a0d0e21e
LW
2129 }
2130
54310121 2131 gimme = GIMME_V;
533c011a 2132 if ((PL_op->op_private & OPpENTERSUB_DB) && GvCV(PL_DBsub) && !CvNODEBUG(cv))
4f01c5a5 2133 cv = get_db_sub(&sv, cv);
3de9ffa1
MB
2134 if (!cv)
2135 DIE("No DBsub routine");
a0d0e21e 2136
11343788 2137#ifdef USE_THREADS
3de9ffa1
MB
2138 /*
2139 * First we need to check if the sub or method requires locking.
458fb581
MB
2140 * If so, we gain a lock on the CV, the first argument or the
2141 * stash (for static methods), as appropriate. This has to be
2142 * inline because for FAKE_THREADS, COND_WAIT inlines code to
2143 * reschedule by returning a new op.
3de9ffa1 2144 */
11343788 2145 MUTEX_LOCK(CvMUTEXP(cv));
77a005ab
MB
2146 if (CvFLAGS(cv) & CVf_LOCKED) {
2147 MAGIC *mg;
2148 if (CvFLAGS(cv) & CVf_METHOD) {
533c011a
NIS
2149 if (SP > PL_stack_base + TOPMARK)
2150 sv = *(PL_stack_base + TOPMARK + 1);
77a005ab 2151 else {
13e08037
GS
2152 AV *av = (AV*)PL_curpad[0];
2153 if (hasargs || !av || AvFILLp(av) < 0
2154 || !(sv = AvARRAY(av)[0]))
2155 {
2156 MUTEX_UNLOCK(CvMUTEXP(cv));
2157 croak("no argument for locked method call");
2158 }
77a005ab
MB
2159 }
2160 if (SvROK(sv))
2161 sv = SvRV(sv);
458fb581
MB
2162 else {
2163 STRLEN len;
2164 char *stashname = SvPV(sv, len);
2165 sv = (SV*)gv_stashpvn(stashname, len, TRUE);
2166 }
77a005ab
MB
2167 }
2168 else {
2169 sv = (SV*)cv;
2170 }
2171 MUTEX_UNLOCK(CvMUTEXP(cv));
2172 mg = condpair_magic(sv);
2173 MUTEX_LOCK(MgMUTEXP(mg));
2174 if (MgOWNER(mg) == thr)
2175 MUTEX_UNLOCK(MgMUTEXP(mg));
2176 else {
2177 while (MgOWNER(mg))
2178 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
2179 MgOWNER(mg) = thr;
8b73bbec 2180 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: pp_entersub lock %p\n",
1fd28e87 2181 thr, sv);)
77a005ab
MB
2182 MUTEX_UNLOCK(MgMUTEXP(mg));
2183 save_destructor(unlock_condpair, sv);
11343788 2184 }
77a005ab 2185 MUTEX_LOCK(CvMUTEXP(cv));
11343788 2186 }
3de9ffa1
MB
2187 /*
2188 * Now we have permission to enter the sub, we must distinguish
2189 * four cases. (0) It's an XSUB (in which case we don't care
2190 * about ownership); (1) it's ours already (and we're recursing);
2191 * (2) it's free (but we may already be using a cached clone);
2192 * (3) another thread owns it. Case (1) is easy: we just use it.
2193 * Case (2) means we look for a clone--if we have one, use it
2194 * otherwise grab ownership of cv. Case (3) means we look for a
2195 * clone (for non-XSUBs) and have to create one if we don't
2196 * already have one.
2197 * Why look for a clone in case (2) when we could just grab
2198 * ownership of cv straight away? Well, we could be recursing,
2199 * i.e. we originally tried to enter cv while another thread
2200 * owned it (hence we used a clone) but it has been freed up
2201 * and we're now recursing into it. It may or may not be "better"
2202 * to use the clone but at least CvDEPTH can be trusted.
2203 */
2204 if (CvOWNER(cv) == thr || CvXSUB(cv))
2205 MUTEX_UNLOCK(CvMUTEXP(cv));
11343788 2206 else {
3de9ffa1
MB
2207 /* Case (2) or (3) */
2208 SV **svp;
2209
11343788 2210 /*
3de9ffa1
MB
2211 * XXX Might it be better to release CvMUTEXP(cv) while we
2212 * do the hv_fetch? We might find someone has pinched it
2213 * when we look again, in which case we would be in case
2214 * (3) instead of (2) so we'd have to clone. Would the fact
2215 * that we released the mutex more quickly make up for this?
2216 */
b099ddc0 2217 if ((svp = hv_fetch(thr->cvcache, (char *)cv, sizeof(cv), FALSE)))
6ee623d5 2218 {
3de9ffa1 2219 /* We already have a clone to use */
11343788 2220 MUTEX_UNLOCK(CvMUTEXP(cv));
3de9ffa1 2221 cv = *(CV**)svp;
8b73bbec 2222 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
1fd28e87
MB
2223 "entersub: %p already has clone %p:%s\n",
2224 thr, cv, SvPEEK((SV*)cv)));
3de9ffa1
MB
2225 CvOWNER(cv) = thr;
2226 SvREFCNT_inc(cv);
2227 if (CvDEPTH(cv) == 0)
2228 SAVEDESTRUCTOR(unset_cvowner, (void*) cv);
2229 }
11343788 2230 else {
3de9ffa1
MB
2231 /* (2) => grab ownership of cv. (3) => make clone */
2232 if (!CvOWNER(cv)) {
2233 CvOWNER(cv) = thr;
2234 SvREFCNT_inc(cv);
11343788 2235 MUTEX_UNLOCK(CvMUTEXP(cv));
8b73bbec 2236 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
1fd28e87
MB
2237 "entersub: %p grabbing %p:%s in stash %s\n",
2238 thr, cv, SvPEEK((SV*)cv), CvSTASH(cv) ?
3de9ffa1
MB
2239 HvNAME(CvSTASH(cv)) : "(none)"));
2240 } else {
2241 /* Make a new clone. */
2242 CV *clonecv;
2243 SvREFCNT_inc(cv); /* don't let it vanish from under us */
2244 MUTEX_UNLOCK(CvMUTEXP(cv));
8b73bbec 2245 DEBUG_S((PerlIO_printf(PerlIO_stderr(),
1fd28e87
MB
2246 "entersub: %p cloning %p:%s\n",
2247 thr, cv, SvPEEK((SV*)cv))));
3de9ffa1
MB
2248 /*
2249 * We're creating a new clone so there's no race
2250 * between the original MUTEX_UNLOCK and the
2251 * SvREFCNT_inc since no one will be trying to undef
2252 * it out from underneath us. At least, I don't think
2253 * there's a race...
2254 */
2255 clonecv = cv_clone(cv);
2256 SvREFCNT_dec(cv); /* finished with this */
199100c8 2257 hv_store(thr->cvcache, (char*)cv, sizeof(cv), (SV*)clonecv,0);
3de9ffa1
MB
2258 CvOWNER(clonecv) = thr;
2259 cv = clonecv;
11343788 2260 SvREFCNT_inc(cv);
11343788 2261 }
8b73bbec 2262 DEBUG_S(if (CvDEPTH(cv) != 0)
3de9ffa1
MB
2263 PerlIO_printf(PerlIO_stderr(), "depth %ld != 0\n",
2264 CvDEPTH(cv)););
2265 SAVEDESTRUCTOR(unset_cvowner, (void*) cv);
11343788 2266 }
3de9ffa1 2267 }
11343788
MB
2268#endif /* USE_THREADS */
2269
a0d0e21e
LW
2270 if (CvXSUB(cv)) {
2271 if (CvOLDSTYLE(cv)) {
ecfc5424 2272 I32 (*fp3)_((int,int,int));
a0d0e21e
LW
2273 dMARK;
2274 register I32 items = SP - MARK;
67955e0c 2275 /* We dont worry to copy from @_. */
924508f0
GS
2276 while (SP > mark) {
2277 SP[1] = SP[0];
2278 SP--;
a0d0e21e 2279 }
3280af22 2280 PL_stack_sp = mark + 1;
ecfc5424
AD
2281 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
2282 items = (*fp3)(CvXSUBANY(cv).any_i32,
3280af22 2283 MARK - PL_stack_base + 1,
ecfc5424 2284 items);
3280af22 2285 PL_stack_sp = PL_stack_base + items;
a0d0e21e
LW
2286 }
2287 else {
748a9306
LW
2288 I32 markix = TOPMARK;
2289
a0d0e21e 2290 PUTBACK;
67955e0c
PP
2291
2292 if (!hasargs) {
2293 /* Need to copy @_ to stack. Alternative may be to
2294 * switch stack to @_, and copy return values
2295 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
6d4ff0d2
MB
2296 AV* av;
2297 I32 items;
2298#ifdef USE_THREADS
533c011a 2299 av = (AV*)PL_curpad[0];
6d4ff0d2 2300#else
3280af22 2301 av = GvAV(PL_defgv);
6d4ff0d2 2302#endif /* USE_THREADS */
93965878 2303 items = AvFILLp(av) + 1; /* @_ is not tieable */
67955e0c
PP
2304
2305 if (items) {
2306 /* Mark is at the end of the stack. */
924508f0
GS
2307 EXTEND(SP, items);
2308 Copy(AvARRAY(av), SP + 1, items, SV*);
2309 SP += items;
67955e0c
PP
2310 PUTBACK ;
2311 }
2312 }
3280af22 2313 if (PL_curcopdb) { /* We assume that the first
67955e0c
PP
2314 XSUB in &DB::sub is the
2315 called one. */
3280af22
NIS
2316 SAVESPTR(PL_curcop);
2317 PL_curcop = PL_curcopdb;
2318 PL_curcopdb = NULL;
67955e0c
PP
2319 }
2320 /* Do we need to open block here? XXXX */
1d583055 2321 (void)(*CvXSUB(cv))(cv _PERL_OBJECT_THIS);
748a9306
LW
2322
2323 /* Enforce some sanity in scalar context. */
3280af22
NIS
2324 if (gimme == G_SCALAR && ++markix != PL_stack_sp - PL_stack_base ) {
2325 if (markix > PL_stack_sp - PL_stack_base)
2326 *(PL_stack_base + markix) = &PL_sv_undef;
748a9306 2327 else
3280af22
NIS
2328 *(PL_stack_base + markix) = *PL_stack_sp;
2329 PL_stack_sp = PL_stack_base + markix;
748a9306 2330 }
a0d0e21e
LW
2331 }
2332 LEAVE;
2333 return NORMAL;
2334 }
2335 else {
2336 dMARK;
2337 register I32 items = SP - MARK;
a0d0e21e
LW
2338 AV* padlist = CvPADLIST(cv);
2339 SV** svp = AvARRAY(padlist);
533c011a 2340 push_return(PL_op->op_next);
a0d0e21e
LW
2341 PUSHBLOCK(cx, CXt_SUB, MARK);
2342 PUSHSUB(cx);
2343 CvDEPTH(cv)++;
6b35e009
GS
2344 /* XXX This would be a natural place to set C<PL_compcv = cv> so
2345 * that eval'' ops within this sub know the correct lexical space.
2346 * Owing the speed considerations, we choose to search for the cv
2347 * in doeval() instead.
2348 */
a0d0e21e
LW
2349 if (CvDEPTH(cv) < 2)
2350 (void)SvREFCNT_inc(cv);
2351 else { /* save temporaries on recursion? */
93965878 2352 if (CvDEPTH(cv) > AvFILLp(padlist)) {
a0d0e21e
LW
2353 AV *av;
2354 AV *newpad = newAV();
4aa0a1f7 2355 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
93965878 2356 I32 ix = AvFILLp((AV*)svp[1]);
a0d0e21e 2357 svp = AvARRAY(svp[0]);
748a9306 2358 for ( ;ix > 0; ix--) {
3280af22 2359 if (svp[ix] != &PL_sv_undef) {
748a9306 2360 char *name = SvPVX(svp[ix]);
5f05dabc
PP
2361 if ((SvFLAGS(svp[ix]) & SVf_FAKE) /* outer lexical? */
2362 || *name == '&') /* anonymous code? */
2363 {
2364 av_store(newpad, ix, SvREFCNT_inc(oldpad[ix]));
748a9306
LW
2365 }
2366 else { /* our own lexical */
2367 if (*name == '@')
2368 av_store(newpad, ix, sv = (SV*)newAV());
2369 else if (*name == '%')
2370 av_store(newpad, ix, sv = (SV*)newHV());
2371 else
2372 av_store(newpad, ix, sv = NEWSV(0,0));
2373 SvPADMY_on(sv);
2374 }
a0d0e21e
LW
2375 }
2376 else {
748a9306 2377 av_store(newpad, ix, sv = NEWSV(0,0));
a0d0e21e
LW
2378 SvPADTMP_on(sv);
2379 }
2380 }
2381 av = newAV(); /* will be @_ */
2382 av_extend(av, 0);
2383 av_store(newpad, 0, (SV*)av);
2384 AvFLAGS(av) = AVf_REIFY;
2385 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
93965878 2386 AvFILLp(padlist) = CvDEPTH(cv);
a0d0e21e
LW
2387 svp = AvARRAY(padlist);
2388 }
2389 }
6d4ff0d2
MB
2390#ifdef USE_THREADS
2391 if (!hasargs) {
533c011a 2392 AV* av = (AV*)PL_curpad[0];
6d4ff0d2 2393
93965878 2394 items = AvFILLp(av) + 1;
6d4ff0d2
MB
2395 if (items) {
2396 /* Mark is at the end of the stack. */
924508f0
GS
2397 EXTEND(SP, items);
2398 Copy(AvARRAY(av), SP + 1, items, SV*);
2399 SP += items;
6d4ff0d2
MB
2400 PUTBACK ;
2401 }
2402 }
2403#endif /* USE_THREADS */
3280af22
NIS
2404 SAVESPTR(PL_curpad);
2405 PL_curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
6d4ff0d2
MB
2406#ifndef USE_THREADS
2407 if (hasargs)
2408#endif /* USE_THREADS */
2409 {
2410 AV* av;
a0d0e21e
LW
2411 SV** ary;
2412
77a005ab 2413#if 0
8b73bbec 2414 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
0f15f207 2415 "%p entersub preparing @_\n", thr));
77a005ab 2416#endif
3280af22 2417 av = (AV*)PL_curpad[0];
a0d0e21e
LW
2418 if (AvREAL(av)) {
2419 av_clear(av);
2420 AvREAL_off(av);
2421 }
6d4ff0d2 2422#ifndef USE_THREADS
3280af22
NIS
2423 cx->blk_sub.savearray = GvAV(PL_defgv);
2424 GvAV(PL_defgv) = (AV*)SvREFCNT_inc(av);
6d4ff0d2
MB
2425#endif /* USE_THREADS */
2426 cx->blk_sub.argarray = av;
a0d0e21e
LW
2427 ++MARK;
2428
2429 if (items > AvMAX(av) + 1) {
2430 ary = AvALLOC(av);
2431 if (AvARRAY(av) != ary) {
2432 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2433 SvPVX(av) = (char*)ary;
2434 }
2435 if (items > AvMAX(av) + 1) {
2436 AvMAX(av) = items - 1;
2437 Renew(ary,items,SV*);
2438 AvALLOC(av) = ary;
2439 SvPVX(av) = (char*)ary;
2440 }
2441 }
2442 Copy(MARK,AvARRAY(av),items,SV*);
93965878 2443 AvFILLp(av) = items - 1;
a0d0e21e
LW
2444
2445 while (items--) {
2446 if (*MARK)
2447 SvTEMP_off(*MARK);
2448 MARK++;
2449 }
2450 }
4a925ff6
GS
2451 /* warning must come *after* we fully set up the context
2452 * stuff so that __WARN__ handlers can safely dounwind()
2453 * if they want to
2454 */
2455 if (CvDEPTH(cv) == 100 && ckWARN(WARN_RECURSION)
2456 && !(PERLDB_SUB && cv == GvCV(PL_DBsub)))
2457 sub_crush_depth(cv);
77a005ab 2458#if 0
8b73bbec 2459 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
0f15f207 2460 "%p entersub returning %p\n", thr, CvSTART(cv)));
77a005ab 2461#endif
a0d0e21e
LW
2462 RETURNOP(CvSTART(cv));
2463 }
2464}
2465
44a8e56a 2466void
8ac85365 2467sub_crush_depth(CV *cv)
44a8e56a
PP
2468{
2469 if (CvANON(cv))
599cee73 2470 warner(WARN_RECURSION, "Deep recursion on anonymous subroutine");
44a8e56a
PP
2471 else {
2472 SV* tmpstr = sv_newmortal();
2473 gv_efullname3(tmpstr, CvGV(cv), Nullch);
599cee73
PM
2474 warner(WARN_RECURSION, "Deep recursion on subroutine \"%s\"",
2475 SvPVX(tmpstr));
44a8e56a
PP
2476 }
2477}
2478
a0d0e21e
LW
2479PP(pp_aelem)
2480{
4e35701f 2481 djSP;
a0d0e21e 2482 SV** svp;
748a9306 2483 I32 elem = POPi;
68dc0745 2484 AV* av = (AV*)POPs;
533c011a
NIS
2485 U32 lval = PL_op->op_flags & OPf_MOD;
2486 U32 defer = (PL_op->op_private & OPpLVAL_DEFER) && (elem > AvFILL(av));
be6c24e0 2487 SV *sv;
a0d0e21e 2488
748a9306 2489 if (elem > 0)
3280af22 2490 elem -= PL_curcop->cop_arybase;
a0d0e21e
LW
2491 if (SvTYPE(av) != SVt_PVAV)
2492 RETPUSHUNDEF;
68dc0745 2493 svp = av_fetch(av, elem, lval && !defer);
a0d0e21e 2494 if (lval) {
3280af22 2495 if (!svp || *svp == &PL_sv_undef) {
68dc0745
PP
2496 SV* lv;
2497 if (!defer)
22c35a8c 2498 DIE(PL_no_aelem, elem);
68dc0745
PP
2499 lv = sv_newmortal();
2500 sv_upgrade(lv, SVt_PVLV);
2501 LvTYPE(lv) = 'y';
2502 sv_magic(lv, Nullsv, 'y', Nullch, 0);
2503 LvTARG(lv) = SvREFCNT_inc(av);
2504 LvTARGOFF(lv) = elem;
2505 LvTARGLEN(lv) = 1;
2506 PUSHs(lv);
2507 RETURN;
2508 }
533c011a 2509 if (PL_op->op_private & OPpLVAL_INTRO)
161b7d16 2510 save_aelem(av, elem, svp);
533c011a
NIS
2511 else if (PL_op->op_private & OPpDEREF)
2512 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
a0d0e21e 2513 }
3280af22 2514 sv = (svp ? *svp : &PL_sv_undef);
be6c24e0
GS
2515 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
2516 sv = sv_mortalcopy(sv);
2517 PUSHs(sv);
a0d0e21e
LW
2518 RETURN;
2519}
2520
02a9e968 2521void
8ac85365 2522vivify_ref(SV *sv, U32 to_what)
02a9e968
CS
2523{
2524 if (SvGMAGICAL(sv))
2525 mg_get(sv);
2526 if (!SvOK(sv)) {
2527 if (SvREADONLY(sv))
22c35a8c 2528 croak(PL_no_modify);
5f05dabc
PP
2529 if (SvTYPE(sv) < SVt_RV)
2530 sv_upgrade(sv, SVt_RV);
2531 else if (SvTYPE(sv) >= SVt_PV) {
2532 (void)SvOOK_off(sv);
2533 Safefree(SvPVX(sv));
2534 SvLEN(sv) = SvCUR(sv) = 0;
2535 }
68dc0745 2536 switch (to_what) {
5f05dabc 2537 case OPpDEREF_SV:
8c52afec 2538 SvRV(sv) = NEWSV(355,0);
5f05dabc
PP
2539 break;
2540 case OPpDEREF_AV:
2541 SvRV(sv) = (SV*)newAV();
2542 break;
2543 case OPpDEREF_HV:
2544 SvRV(sv) = (SV*)newHV();
2545 break;
2546 }
02a9e968
CS
2547 SvROK_on(sv);
2548 SvSETMAGIC(sv);
2549 }
2550}
2551
a0d0e21e
LW
2552PP(pp_method)
2553{
4e35701f 2554 djSP;
a0d0e21e
LW
2555 SV* sv;
2556 SV* ob;
2557 GV* gv;
56304f61
CS
2558 HV* stash;
2559 char* name;
ac91690f
CS
2560 char* packname;
2561 STRLEN packlen;
a0d0e21e 2562
fb73857a
PP
2563 if (SvROK(TOPs)) {
2564 sv = SvRV(TOPs);
2565 if (SvTYPE(sv) == SVt_PVCV) {
2566 SETs(sv);
2567 RETURN;
2568 }
2569 }
2570
2d8e6c8d 2571 name = SvPV(TOPs, packlen);
3280af22 2572 sv = *(PL_stack_base + TOPMARK + 1);
a0d0e21e 2573
16d20bd9
AD
2574 if (SvGMAGICAL(sv))
2575 mg_get(sv);
a0d0e21e 2576 if (SvROK(sv))
16d20bd9 2577 ob = (SV*)SvRV(sv);
a0d0e21e
LW
2578 else {
2579 GV* iogv;
a0d0e21e 2580
56304f61 2581 packname = Nullch;
a0d0e21e 2582 if (!SvOK(sv) ||
56304f61 2583 !(packname = SvPV(sv, packlen)) ||
a0d0e21e
LW
2584 !(iogv = gv_fetchpv(packname, FALSE, SVt_PVIO)) ||
2585 !(ob=(SV*)GvIO(iogv)))
2586 {
834a4ddd
LW
2587 if (!packname ||
2588 ((*(U8*)packname >= 0xc0 && IN_UTF8)
b86a2fa7 2589 ? !isIDFIRST_utf8((U8*)packname)
834a4ddd
LW
2590 : !isIDFIRST(*packname)
2591 ))
2592 {
72b5445b
GS
2593 DIE("Can't call method \"%s\" %s", name,
2594 SvOK(sv)? "without a package or object reference"
2595 : "on an undefined value");
834a4ddd 2596 }
56304f61 2597 stash = gv_stashpvn(packname, packlen, TRUE);
ac91690f 2598 goto fetch;
a0d0e21e 2599 }
3280af22 2600 *(PL_stack_base + TOPMARK + 1) = sv_2mortal(newRV((SV*)iogv));
a0d0e21e
LW
2601 }
2602
ac91690f 2603 if (!ob || !SvOBJECT(ob))
a0d0e21e 2604 DIE("Can't call method \"%s\" on unblessed reference", name);
a0d0e21e 2605
56304f61 2606 stash = SvSTASH(ob);
a0d0e21e 2607
ac91690f 2608 fetch:
ac91690f 2609 gv = gv_fetchmethod(stash, name);
56304f61
CS
2610 if (!gv) {
2611 char* leaf = name;
2612 char* sep = Nullch;
2613 char* p;
2614
2615 for (p = name; *p; p++) {
2616 if (*p == '\'')
2617 sep = p, leaf = p + 1;
2618 else if (*p == ':' && *(p + 1) == ':')
2619 sep = p, leaf = p + 2;
2620 }
2621 if (!sep || ((sep - name) == 5 && strnEQ(name, "SUPER", 5))) {
3280af22 2622 packname = HvNAME(sep ? PL_curcop->cop_stash : stash);
56304f61
CS
2623 packlen = strlen(packname);
2624 }
2625 else {
2626 packname = name;
2627 packlen = sep - name;
2628 }
2629 DIE("Can't locate object method \"%s\" via package \"%.*s\"",
2630 leaf, (int)packlen, packname);
2631 }
8ebc5c01 2632 SETs(isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv);
a0d0e21e
LW
2633 RETURN;
2634}
22239a37 2635