This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Win32 boot_DynaLoader problem in 99
[perl5.git] / pp_ctl.c
CommitLineData
a0d0e21e
LW
1/* pp_ctl.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 * Now far ahead the Road has gone,
12 * And I must follow, if I can,
13 * Pursuing it with eager feet,
14 * Until it joins some larger way
15 * Where many paths and errands meet.
16 * And whither then? I cannot say.
17 */
18
19#include "EXTERN.h"
20#include "perl.h"
21
22#ifndef WORD_ALIGN
23#define WORD_ALIGN sizeof(U16)
24#endif
25
54310121 26#define DOCATCH(o) ((CATCH_GET == TRUE) ? docatch(o) : (o))
1e422769
PP
27
28static OP *docatch _((OP *o));
a0d0e21e 29static OP *doeval _((int gimme));
fc36a67e 30static OP *dofindlabel _((OP *op, char *label, OP **opstack, OP **oplimit));
a0d0e21e
LW
31static void doparseform _((SV *sv));
32static I32 dopoptoeval _((I32 startingblock));
33static I32 dopoptolabel _((char *label));
34static I32 dopoptoloop _((I32 startingblock));
35static I32 dopoptosub _((I32 startingblock));
36static void save_lines _((AV *array, SV *sv));
a0d0e21e 37static int sortcv _((const void *, const void *));
bbce6d69
PP
38static int sortcmp _((const void *, const void *));
39static int sortcmp_locale _((const void *, const void *));
a0d0e21e
LW
40
41static I32 sortcxix;
42
43PP(pp_wantarray)
44{
45 dSP;
46 I32 cxix;
47 EXTEND(SP, 1);
48
49 cxix = dopoptosub(cxstack_ix);
50 if (cxix < 0)
51 RETPUSHUNDEF;
52
54310121
PP
53 switch (cxstack[cxix].blk_gimme) {
54 case G_ARRAY:
a0d0e21e 55 RETPUSHYES;
54310121 56 case G_SCALAR:
a0d0e21e 57 RETPUSHNO;
54310121
PP
58 default:
59 RETPUSHUNDEF;
60 }
a0d0e21e
LW
61}
62
63PP(pp_regcmaybe)
64{
65 return NORMAL;
66}
67
68PP(pp_regcomp) {
69 dSP;
70 register PMOP *pm = (PMOP*)cLOGOP->op_other;
71 register char *t;
72 SV *tmpstr;
73 STRLEN len;
74
75 tmpstr = POPs;
76 t = SvPV(tmpstr, len);
77
4633a7c4
LW
78 /* JMR: Check against the last compiled regexp */
79 if ( ! pm->op_pmregexp || ! pm->op_pmregexp->precomp
80 || strnNE(pm->op_pmregexp->precomp, t, len)
81 || pm->op_pmregexp->precomp[len]) {
82 if (pm->op_pmregexp) {
83 pregfree(pm->op_pmregexp);
84 pm->op_pmregexp = Null(REGEXP*); /* crucial if regcomp aborts */
85 }
a0d0e21e 86
4633a7c4
LW
87 pm->op_pmflags = pm->op_pmpermflags; /* reset case sensitivity */
88 pm->op_pmregexp = pregcomp(t, t + len, pm);
89 }
a0d0e21e
LW
90
91 if (!pm->op_pmregexp->prelen && curpm)
92 pm = curpm;
93 else if (strEQ("\\s+", pm->op_pmregexp->precomp))
94 pm->op_pmflags |= PMf_WHITE;
95
96 if (pm->op_pmflags & PMf_KEEP) {
a0d0e21e
LW
97 pm->op_pmflags &= ~PMf_RUNTIME; /* no point compiling again */
98 hoistmust(pm);
99 cLOGOP->op_first->op_next = op->op_next;
a0d0e21e
LW
100 }
101 RETURN;
102}
103
104PP(pp_substcont)
105{
106 dSP;
107 register PMOP *pm = (PMOP*) cLOGOP->op_other;
108 register CONTEXT *cx = &cxstack[cxstack_ix];
109 register SV *dstr = cx->sb_dstr;
110 register char *s = cx->sb_s;
111 register char *m = cx->sb_m;
112 char *orig = cx->sb_orig;
c07a80fd 113 register REGEXP *rx = cx->sb_rx;
a0d0e21e
LW
114
115 if (cx->sb_iters++) {
116 if (cx->sb_iters > cx->sb_maxiters)
117 DIE("Substitution loop");
118
71be2cbc
PP
119 if (!cx->sb_rxtainted)
120 cx->sb_rxtainted = SvTAINTED(TOPs);
a0d0e21e 121 sv_catsv(dstr, POPs);
a0d0e21e
LW
122
123 /* Are we done */
e50aee73 124 if (cx->sb_once || !pregexec(rx, s, cx->sb_strend, orig,
a0d0e21e
LW
125 s == m, Nullsv, cx->sb_safebase))
126 {
127 SV *targ = cx->sb_targ;
128 sv_catpvn(dstr, s, cx->sb_strend - s);
748a9306 129
9212bbba
CS
130 TAINT_IF(cx->sb_rxtainted || rx->exec_tainted);
131
4633a7c4 132 (void)SvOOK_off(targ);
cb0b1708 133 Safefree(SvPVX(targ));
748a9306
LW
134 SvPVX(targ) = SvPVX(dstr);
135 SvCUR_set(targ, SvCUR(dstr));
136 SvLEN_set(targ, SvLEN(dstr));
137 SvPVX(dstr) = 0;
138 sv_free(dstr);
a0d0e21e
LW
139 (void)SvPOK_only(targ);
140 SvSETMAGIC(targ);
9212bbba 141 SvTAINT(targ);
5cd24f17 142
a0d0e21e 143 PUSHs(sv_2mortal(newSViv((I32)cx->sb_iters - 1)));
4633a7c4 144 LEAVE_SCOPE(cx->sb_oldsave);
a0d0e21e
LW
145 POPSUBST(cx);
146 RETURNOP(pm->op_next);
147 }
148 }
149 if (rx->subbase && rx->subbase != orig) {
150 m = s;
151 s = orig;
152 cx->sb_orig = orig = rx->subbase;
153 s = orig + (m - s);
154 cx->sb_strend = s + (cx->sb_strend - m);
155 }
156 cx->sb_m = m = rx->startp[0];
157 sv_catpvn(dstr, s, m-s);
158 cx->sb_s = rx->endp[0];
71be2cbc 159 cx->sb_rxtainted |= rx->exec_tainted;
a0d0e21e
LW
160 RETURNOP(pm->op_pmreplstart);
161}
162
163PP(pp_formline)
164{
165 dSP; dMARK; dORIGMARK;
166 register SV *form = *++MARK;
167 register U16 *fpc;
168 register char *t;
169 register char *f;
170 register char *s;
171 register char *send;
172 register I32 arg;
173 register SV *sv;
174 char *item;
175 I32 itemsize;
176 I32 fieldsize;
177 I32 lines = 0;
178 bool chopspace = (strchr(chopset, ' ') != Nullch);
179 char *chophere;
180 char *linemark;
a0d0e21e
LW
181 double value;
182 bool gotsome;
183 STRLEN len;
184
55497cff 185 if (!SvMAGICAL(form) || !SvCOMPILED(form)) {
a0d0e21e
LW
186 SvREADONLY_off(form);
187 doparseform(form);
188 }
189
190 SvPV_force(formtarget, len);
191 t = SvGROW(formtarget, len + SvCUR(form) + 1); /* XXX SvCUR bad */
192 t += len;
193 f = SvPV(form, len);
194 /* need to jump to the next word */
195 s = f + len + WORD_ALIGN - SvCUR(form) % WORD_ALIGN;
196
197 fpc = (U16*)s;
198
199 for (;;) {
200 DEBUG_f( {
201 char *name = "???";
202 arg = -1;
203 switch (*fpc) {
204 case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
205 case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
206 case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
207 case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
208 case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
209
210 case FF_CHECKNL: name = "CHECKNL"; break;
211 case FF_CHECKCHOP: name = "CHECKCHOP"; break;
212 case FF_SPACE: name = "SPACE"; break;
213 case FF_HALFSPACE: name = "HALFSPACE"; break;
214 case FF_ITEM: name = "ITEM"; break;
215 case FF_CHOP: name = "CHOP"; break;
216 case FF_LINEGLOB: name = "LINEGLOB"; break;
217 case FF_NEWLINE: name = "NEWLINE"; break;
218 case FF_MORE: name = "MORE"; break;
219 case FF_LINEMARK: name = "LINEMARK"; break;
220 case FF_END: name = "END"; break;
221 }
222 if (arg >= 0)
760ac839 223 PerlIO_printf(PerlIO_stderr(), "%-16s%ld\n", name, (long) arg);
a0d0e21e 224 else
760ac839 225 PerlIO_printf(PerlIO_stderr(), "%-16s\n", name);
a0d0e21e
LW
226 } )
227 switch (*fpc++) {
228 case FF_LINEMARK:
229 linemark = t;
a0d0e21e
LW
230 lines++;
231 gotsome = FALSE;
232 break;
233
234 case FF_LITERAL:
235 arg = *fpc++;
236 while (arg--)
237 *t++ = *f++;
238 break;
239
240 case FF_SKIP:
241 f += *fpc++;
242 break;
243
244 case FF_FETCH:
245 arg = *fpc++;
246 f += arg;
247 fieldsize = arg;
248
249 if (MARK < SP)
250 sv = *++MARK;
251 else {
252 sv = &sv_no;
253 if (dowarn)
254 warn("Not enough format arguments");
255 }
256 break;
257
258 case FF_CHECKNL:
259 item = s = SvPV(sv, len);
260 itemsize = len;
261 if (itemsize > fieldsize)
262 itemsize = fieldsize;
263 send = chophere = s + itemsize;
264 while (s < send) {
265 if (*s & ~31)
266 gotsome = TRUE;
267 else if (*s == '\n')
268 break;
269 s++;
270 }
271 itemsize = s - item;
272 break;
273
274 case FF_CHECKCHOP:
275 item = s = SvPV(sv, len);
276 itemsize = len;
277 if (itemsize <= fieldsize) {
278 send = chophere = s + itemsize;
279 while (s < send) {
280 if (*s == '\r') {
281 itemsize = s - item;
282 break;
283 }
284 if (*s++ & ~31)
285 gotsome = TRUE;
286 }
287 }
288 else {
289 itemsize = fieldsize;
290 send = chophere = s + itemsize;
291 while (s < send || (s == send && isSPACE(*s))) {
292 if (isSPACE(*s)) {
293 if (chopspace)
294 chophere = s;
295 if (*s == '\r')
296 break;
297 }
298 else {
299 if (*s & ~31)
300 gotsome = TRUE;
301 if (strchr(chopset, *s))
302 chophere = s + 1;
303 }
304 s++;
305 }
306 itemsize = chophere - item;
307 }
308 break;
309
310 case FF_SPACE:
311 arg = fieldsize - itemsize;
312 if (arg) {
313 fieldsize -= arg;
314 while (arg-- > 0)
315 *t++ = ' ';
316 }
317 break;
318
319 case FF_HALFSPACE:
320 arg = fieldsize - itemsize;
321 if (arg) {
322 arg /= 2;
323 fieldsize -= arg;
324 while (arg-- > 0)
325 *t++ = ' ';
326 }
327 break;
328
329 case FF_ITEM:
330 arg = itemsize;
331 s = item;
332 while (arg--) {
333#if 'z' - 'a' != 25
334 int ch = *t++ = *s++;
335 if (!iscntrl(ch))
336 t[-1] = ' ';
337#else
338 if ( !((*t++ = *s++) & ~31) )
339 t[-1] = ' ';
340#endif
341
342 }
343 break;
344
345 case FF_CHOP:
346 s = chophere;
347 if (chopspace) {
348 while (*s && isSPACE(*s))
349 s++;
350 }
351 sv_chop(sv,s);
352 break;
353
354 case FF_LINEGLOB:
355 item = s = SvPV(sv, len);
356 itemsize = len;
357 if (itemsize) {
358 gotsome = TRUE;
359 send = s + itemsize;
360 while (s < send) {
361 if (*s++ == '\n') {
362 if (s == send)
363 itemsize--;
364 else
365 lines++;
366 }
367 }
368 SvCUR_set(formtarget, t - SvPVX(formtarget));
369 sv_catpvn(formtarget, item, itemsize);
370 SvGROW(formtarget, SvCUR(formtarget) + SvCUR(form) + 1);
371 t = SvPVX(formtarget) + SvCUR(formtarget);
372 }
373 break;
374
375 case FF_DECIMAL:
376 /* If the field is marked with ^ and the value is undefined,
377 blank it out. */
378 arg = *fpc++;
379 if ((arg & 512) && !SvOK(sv)) {
380 arg = fieldsize;
381 while (arg--)
382 *t++ = ' ';
383 break;
384 }
385 gotsome = TRUE;
386 value = SvNV(sv);
bbce6d69 387 /* Formats aren't yet marked for locales, so assume "yes". */
36477c24 388 SET_NUMERIC_LOCAL();
a0d0e21e
LW
389 if (arg & 256) {
390 sprintf(t, "%#*.*f", (int) fieldsize, (int) arg & 255, value);
391 } else {
392 sprintf(t, "%*.0f", (int) fieldsize, value);
393 }
394 t += fieldsize;
395 break;
396
397 case FF_NEWLINE:
398 f++;
399 while (t-- > linemark && *t == ' ') ;
400 t++;
401 *t++ = '\n';
402 break;
403
404 case FF_BLANK:
405 arg = *fpc++;
406 if (gotsome) {
407 if (arg) { /* repeat until fields exhausted? */
408 *t = '\0';
409 SvCUR_set(formtarget, t - SvPVX(formtarget));
410 lines += FmLINES(formtarget);
411 if (lines == 200) {
412 arg = t - linemark;
413 if (strnEQ(linemark, linemark - arg, arg))
414 DIE("Runaway format");
415 }
416 FmLINES(formtarget) = lines;
417 SP = ORIGMARK;
418 RETURNOP(cLISTOP->op_first);
419 }
420 }
421 else {
422 t = linemark;
423 lines--;
424 }
425 break;
426
427 case FF_MORE:
428 if (itemsize) {
429 arg = fieldsize - itemsize;
430 if (arg) {
431 fieldsize -= arg;
432 while (arg-- > 0)
433 *t++ = ' ';
434 }
435 s = t - 3;
436 if (strnEQ(s," ",3)) {
437 while (s > SvPVX(formtarget) && isSPACE(s[-1]))
438 s--;
439 }
440 *s++ = '.';
441 *s++ = '.';
442 *s++ = '.';
443 }
444 break;
445
446 case FF_END:
447 *t = '\0';
448 SvCUR_set(formtarget, t - SvPVX(formtarget));
449 FmLINES(formtarget) += lines;
450 SP = ORIGMARK;
451 RETPUSHYES;
452 }
453 }
454}
455
456PP(pp_grepstart)
457{
458 dSP;
459 SV *src;
460
461 if (stack_base + *markstack_ptr == sp) {
462 (void)POPMARK;
54310121 463 if (GIMME_V == G_SCALAR)
a0d0e21e
LW
464 XPUSHs(&sv_no);
465 RETURNOP(op->op_next->op_next);
466 }
467 stack_sp = stack_base + *markstack_ptr + 1;
468 pp_pushmark(); /* push dst */
469 pp_pushmark(); /* push src */
470 ENTER; /* enter outer scope */
471
472 SAVETMPS;
473 SAVESPTR(GvSV(defgv));
474
475 ENTER; /* enter inner scope */
476 SAVESPTR(curpm);
477
478 src = stack_base[*markstack_ptr];
479 SvTEMP_off(src);
480 GvSV(defgv) = src;
481
482 PUTBACK;
483 if (op->op_type == OP_MAPSTART)
484 pp_pushmark(); /* push top */
485 return ((LOGOP*)op->op_next)->op_other;
486}
487
488PP(pp_mapstart)
489{
490 DIE("panic: mapstart"); /* uses grepstart */
491}
492
493PP(pp_mapwhile)
494{
495 dSP;
496 I32 diff = (sp - stack_base) - *markstack_ptr;
497 I32 count;
498 I32 shift;
499 SV** src;
500 SV** dst;
501
502 ++markstack_ptr[-1];
503 if (diff) {
504 if (diff > markstack_ptr[-1] - markstack_ptr[-2]) {
505 shift = diff - (markstack_ptr[-1] - markstack_ptr[-2]);
506 count = (sp - stack_base) - markstack_ptr[-1] + 2;
507
508 EXTEND(sp,shift);
509 src = sp;
510 dst = (sp += shift);
511 markstack_ptr[-1] += shift;
512 *markstack_ptr += shift;
513 while (--count)
514 *dst-- = *src--;
515 }
516 dst = stack_base + (markstack_ptr[-2] += diff) - 1;
517 ++diff;
518 while (--diff)
519 *dst-- = SvTEMP(TOPs) ? POPs : sv_mortalcopy(POPs);
520 }
521 LEAVE; /* exit inner scope */
522
523 /* All done yet? */
524 if (markstack_ptr[-1] > *markstack_ptr) {
525 I32 items;
54310121 526 I32 gimme = GIMME_V;
a0d0e21e
LW
527
528 (void)POPMARK; /* pop top */
529 LEAVE; /* exit outer scope */
530 (void)POPMARK; /* pop src */
531 items = --*markstack_ptr - markstack_ptr[-1];
532 (void)POPMARK; /* pop dst */
533 SP = stack_base + POPMARK; /* pop original mark */
54310121 534 if (gimme == G_SCALAR) {
a0d0e21e
LW
535 dTARGET;
536 XPUSHi(items);
a0d0e21e 537 }
54310121
PP
538 else if (gimme == G_ARRAY)
539 SP += items;
a0d0e21e
LW
540 RETURN;
541 }
542 else {
543 SV *src;
544
545 ENTER; /* enter inner scope */
546 SAVESPTR(curpm);
547
548 src = stack_base[markstack_ptr[-1]];
549 SvTEMP_off(src);
550 GvSV(defgv) = src;
551
552 RETURNOP(cLOGOP->op_other);
553 }
554}
555
556
557PP(pp_sort)
558{
559 dSP; dMARK; dORIGMARK;
560 register SV **up;
561 SV **myorigmark = ORIGMARK;
562 register I32 max;
563 HV *stash;
564 GV *gv;
565 CV *cv;
566 I32 gimme = GIMME;
567 OP* nextop = op->op_next;
568
569 if (gimme != G_ARRAY) {
570 SP = MARK;
571 RETPUSHUNDEF;
572 }
573
574 if (op->op_flags & OPf_STACKED) {
575 ENTER;
576 if (op->op_flags & OPf_SPECIAL) {
577 OP *kid = cLISTOP->op_first->op_sibling; /* pass pushmark */
578 kid = kUNOP->op_first; /* pass rv2gv */
579 kid = kUNOP->op_first; /* pass leave */
580 sortcop = kid->op_next;
581 stash = curcop->cop_stash;
582 }
583 else {
584 cv = sv_2cv(*++MARK, &stash, &gv, 0);
585 if (!(cv && CvROOT(cv))) {
586 if (gv) {
587 SV *tmpstr = sv_newmortal();
e5cf08de 588 gv_efullname3(tmpstr, gv, Nullch);
a0d0e21e
LW
589 if (cv && CvXSUB(cv))
590 DIE("Xsub \"%s\" called in sort", SvPVX(tmpstr));
591 DIE("Undefined sort subroutine \"%s\" called",
592 SvPVX(tmpstr));
593 }
594 if (cv) {
595 if (CvXSUB(cv))
596 DIE("Xsub called in sort");
597 DIE("Undefined subroutine in sort");
598 }
599 DIE("Not a CODE reference in sort");
600 }
601 sortcop = CvSTART(cv);
602 SAVESPTR(CvROOT(cv)->op_ppaddr);
603 CvROOT(cv)->op_ppaddr = ppaddr[OP_NULL];
604
605 SAVESPTR(curpad);
606 curpad = AvARRAY((AV*)AvARRAY(CvPADLIST(cv))[1]);
607 }
608 }
609 else {
610 sortcop = Nullop;
611 stash = curcop->cop_stash;
612 }
613
614 up = myorigmark + 1;
615 while (MARK < SP) { /* This may or may not shift down one here. */
616 /*SUPPRESS 560*/
617 if (*up = *++MARK) { /* Weed out nulls. */
9f8d30d5
CS
618 SvTEMP_off(*up);
619 if (!sortcop && !SvPOK(*up))
a0d0e21e 620 (void)sv_2pv(*up, &na);
a0d0e21e
LW
621 up++;
622 }
623 }
624 max = --up - myorigmark;
625 if (sortcop) {
626 if (max > 1) {
627 AV *oldstack;
628 CONTEXT *cx;
629 SV** newsp;
54310121 630 bool oldcatch = CATCH_GET;
a0d0e21e
LW
631
632 SAVETMPS;
633 SAVESPTR(op);
634
1ce6579f 635 oldstack = curstack;
a0d0e21e
LW
636 if (!sortstack) {
637 sortstack = newAV();
638 AvREAL_off(sortstack);
639 av_extend(sortstack, 32);
640 }
54310121 641 CATCH_SET(TRUE);
1ce6579f 642 SWITCHSTACK(curstack, sortstack);
a0d0e21e
LW
643 if (sortstash != stash) {
644 firstgv = gv_fetchpv("a", TRUE, SVt_PV);
645 secondgv = gv_fetchpv("b", TRUE, SVt_PV);
646 sortstash = stash;
647 }
648
649 SAVESPTR(GvSV(firstgv));
650 SAVESPTR(GvSV(secondgv));
0a753a76 651 PUSHBLOCK(cx, CXt_NULL, stack_base);
a0d0e21e
LW
652 sortcxix = cxstack_ix;
653
654 qsort((char*)(myorigmark+1), max, sizeof(SV*), sortcv);
655
656 POPBLOCK(cx,curpm);
657 SWITCHSTACK(sortstack, oldstack);
54310121 658 CATCH_SET(oldcatch);
a0d0e21e
LW
659 }
660 LEAVE;
661 }
662 else {
663 if (max > 1) {
664 MEXTEND(SP, 20); /* Can't afford stack realloc on signal. */
bbce6d69
PP
665 qsort((char*)(ORIGMARK+1), max, sizeof(SV*),
666 (op->op_private & OPpLOCALE) ? sortcmp_locale : sortcmp);
a0d0e21e
LW
667 }
668 }
669 stack_sp = ORIGMARK + max;
670 return nextop;
671}
672
673/* Range stuff. */
674
675PP(pp_range)
676{
677 if (GIMME == G_ARRAY)
678 return cCONDOP->op_true;
679 return SvTRUEx(PAD_SV(op->op_targ)) ? cCONDOP->op_false : cCONDOP->op_true;
680}
681
682PP(pp_flip)
683{
684 dSP;
685
686 if (GIMME == G_ARRAY) {
687 RETURNOP(((CONDOP*)cUNOP->op_first)->op_false);
688 }
689 else {
690 dTOPss;
691 SV *targ = PAD_SV(op->op_targ);
692
693 if ((op->op_private & OPpFLIP_LINENUM)
694 ? last_in_gv && SvIV(sv) == IoLINES(GvIOp(last_in_gv))
695 : SvTRUE(sv) ) {
696 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
697 if (op->op_flags & OPf_SPECIAL) {
698 sv_setiv(targ, 1);
699 RETURN;
700 }
701 else {
702 sv_setiv(targ, 0);
703 sp--;
704 RETURNOP(((CONDOP*)cUNOP->op_first)->op_false);
705 }
706 }
707 sv_setpv(TARG, "");
708 SETs(targ);
709 RETURN;
710 }
711}
712
713PP(pp_flop)
714{
715 dSP;
716
717 if (GIMME == G_ARRAY) {
718 dPOPPOPssrl;
719 register I32 i;
720 register SV *sv;
721 I32 max;
722
4633a7c4 723 if (SvNIOKp(left) || !SvPOKp(left) ||
bbce6d69
PP
724 (looks_like_number(left) && *SvPVX(left) != '0') )
725 {
a0d0e21e
LW
726 i = SvIV(left);
727 max = SvIV(right);
bbce6d69
PP
728 if (max >= i) {
729 EXTEND_MORTAL(max - i + 1);
a0d0e21e 730 EXTEND(SP, max - i + 1);
bbce6d69 731 }
a0d0e21e 732 while (i <= max) {
bbce6d69 733 sv = sv_2mortal(newSViv(i++));
a0d0e21e
LW
734 PUSHs(sv);
735 }
736 }
737 else {
738 SV *final = sv_mortalcopy(right);
739 STRLEN len;
740 char *tmps = SvPV(final, len);
741
742 sv = sv_mortalcopy(left);
4633a7c4 743 while (!SvNIOKp(sv) && SvCUR(sv) <= len &&
a0d0e21e
LW
744 strNE(SvPVX(sv),tmps) ) {
745 XPUSHs(sv);
746 sv = sv_2mortal(newSVsv(sv));
747 sv_inc(sv);
748 }
749 if (strEQ(SvPVX(sv),tmps))
750 XPUSHs(sv);
751 }
752 }
753 else {
754 dTOPss;
755 SV *targ = PAD_SV(cUNOP->op_first->op_targ);
756 sv_inc(targ);
757 if ((op->op_private & OPpFLIP_LINENUM)
758 ? last_in_gv && SvIV(sv) == IoLINES(GvIOp(last_in_gv))
759 : SvTRUE(sv) ) {
760 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
761 sv_catpv(targ, "E0");
762 }
763 SETs(targ);
764 }
765
766 RETURN;
767}
768
769/* Control. */
770
771static I32
772dopoptolabel(label)
773char *label;
774{
775 register I32 i;
776 register CONTEXT *cx;
777
778 for (i = cxstack_ix; i >= 0; i--) {
779 cx = &cxstack[i];
780 switch (cx->cx_type) {
781 case CXt_SUBST:
782 if (dowarn)
783 warn("Exiting substitution via %s", op_name[op->op_type]);
784 break;
785 case CXt_SUB:
786 if (dowarn)
787 warn("Exiting subroutine via %s", op_name[op->op_type]);
788 break;
789 case CXt_EVAL:
790 if (dowarn)
791 warn("Exiting eval via %s", op_name[op->op_type]);
792 break;
0a753a76
PP
793 case CXt_NULL:
794 if (dowarn)
795 warn("Exiting pseudo-block via %s", op_name[op->op_type]);
796 return -1;
a0d0e21e
LW
797 case CXt_LOOP:
798 if (!cx->blk_loop.label ||
799 strNE(label, cx->blk_loop.label) ) {
68dc0745
PP
800 DEBUG_l(deb("(Skipping label #%ld %s)\n",
801 (long)i, cx->blk_loop.label));
a0d0e21e
LW
802 continue;
803 }
68dc0745 804 DEBUG_l( deb("(Found label #%ld %s)\n", (long)i, label));
a0d0e21e
LW
805 return i;
806 }
807 }
808 return i;
809}
810
e50aee73
AD
811I32
812dowantarray()
813{
54310121
PP
814 I32 gimme = block_gimme();
815 return (gimme == G_VOID) ? G_SCALAR : gimme;
816}
817
818I32
819block_gimme()
820{
e50aee73
AD
821 I32 cxix;
822
823 cxix = dopoptosub(cxstack_ix);
824 if (cxix < 0)
46fc3d4c 825 return G_VOID;
e50aee73 826
54310121
PP
827 switch (cxstack[cxix].blk_gimme) {
828 case G_VOID:
829 return G_VOID;
830 case G_SCALAR:
e50aee73 831 return G_SCALAR;
54310121
PP
832 case G_ARRAY:
833 return G_ARRAY;
834 default:
835 croak("panic: bad gimme: %d\n", cxstack[cxix].blk_gimme);
836 }
e50aee73
AD
837}
838
a0d0e21e
LW
839static I32
840dopoptosub(startingblock)
841I32 startingblock;
842{
843 I32 i;
844 register CONTEXT *cx;
845 for (i = startingblock; i >= 0; i--) {
846 cx = &cxstack[i];
847 switch (cx->cx_type) {
848 default:
849 continue;
850 case CXt_EVAL:
851 case CXt_SUB:
68dc0745 852 DEBUG_l( deb("(Found sub #%ld)\n", (long)i));
a0d0e21e
LW
853 return i;
854 }
855 }
856 return i;
857}
858
859static I32
860dopoptoeval(startingblock)
861I32 startingblock;
862{
863 I32 i;
864 register CONTEXT *cx;
865 for (i = startingblock; i >= 0; i--) {
866 cx = &cxstack[i];
867 switch (cx->cx_type) {
868 default:
869 continue;
870 case CXt_EVAL:
68dc0745 871 DEBUG_l( deb("(Found eval #%ld)\n", (long)i));
a0d0e21e
LW
872 return i;
873 }
874 }
875 return i;
876}
877
878static I32
879dopoptoloop(startingblock)
880I32 startingblock;
881{
882 I32 i;
883 register CONTEXT *cx;
884 for (i = startingblock; i >= 0; i--) {
885 cx = &cxstack[i];
886 switch (cx->cx_type) {
887 case CXt_SUBST:
888 if (dowarn)
5f05dabc 889 warn("Exiting substitution via %s", op_name[op->op_type]);
a0d0e21e
LW
890 break;
891 case CXt_SUB:
892 if (dowarn)
893 warn("Exiting subroutine via %s", op_name[op->op_type]);
894 break;
895 case CXt_EVAL:
896 if (dowarn)
897 warn("Exiting eval via %s", op_name[op->op_type]);
898 break;
0a753a76
PP
899 case CXt_NULL:
900 if (dowarn)
901 warn("Exiting pseudo-block via %s", op_name[op->op_type]);
902 return -1;
a0d0e21e 903 case CXt_LOOP:
68dc0745 904 DEBUG_l( deb("(Found loop #%ld)\n", (long)i));
a0d0e21e
LW
905 return i;
906 }
907 }
908 return i;
909}
910
911void
912dounwind(cxix)
913I32 cxix;
914{
915 register CONTEXT *cx;
916 SV **newsp;
917 I32 optype;
918
919 while (cxstack_ix > cxix) {
920 cx = &cxstack[cxstack_ix--];
760ac839 921 DEBUG_l(PerlIO_printf(Perl_debug_log, "Unwinding block %ld, type %s\n", (long) cxstack_ix+1,
a0d0e21e
LW
922 block_type[cx->cx_type]));
923 /* Note: we don't need to restore the base context info till the end. */
924 switch (cx->cx_type) {
925 case CXt_SUB:
926 POPSUB(cx);
927 break;
928 case CXt_EVAL:
929 POPEVAL(cx);
930 break;
931 case CXt_LOOP:
932 POPLOOP(cx);
933 break;
0a753a76 934 case CXt_NULL:
a0d0e21e
LW
935 case CXt_SUBST:
936 break;
937 }
938 }
939}
940
a0d0e21e
LW
941OP *
942die_where(message)
943char *message;
944{
945 if (in_eval) {
946 I32 cxix;
947 register CONTEXT *cx;
948 I32 gimme;
949 SV **newsp;
950
4633a7c4
LW
951 if (in_eval & 4) {
952 SV **svp;
953 STRLEN klen = strlen(message);
954
955 svp = hv_fetch(GvHV(errgv), message, klen, TRUE);
956 if (svp) {
957 if (!SvIOK(*svp)) {
958 static char prefix[] = "\t(in cleanup) ";
959 sv_upgrade(*svp, SVt_IV);
960 (void)SvIOK_only(*svp);
961 SvGROW(GvSV(errgv), SvCUR(GvSV(errgv))+sizeof(prefix)+klen);
962 sv_catpvn(GvSV(errgv), prefix, sizeof(prefix)-1);
963 sv_catpvn(GvSV(errgv), message, klen);
964 }
965 sv_inc(*svp);
966 }
967 }
968 else
c07a80fd 969 sv_setpv(GvSV(errgv), message);
4633a7c4 970
a0d0e21e
LW
971 cxix = dopoptoeval(cxstack_ix);
972 if (cxix >= 0) {
973 I32 optype;
974
975 if (cxix < cxstack_ix)
976 dounwind(cxix);
977
978 POPBLOCK(cx,curpm);
979 if (cx->cx_type != CXt_EVAL) {
760ac839 980 PerlIO_printf(PerlIO_stderr(), "panic: die %s", message);
a0d0e21e
LW
981 my_exit(1);
982 }
983 POPEVAL(cx);
984
985 if (gimme == G_SCALAR)
986 *++newsp = &sv_undef;
987 stack_sp = newsp;
988
989 LEAVE;
748a9306 990
7a2e2cd6
PP
991 if (optype == OP_REQUIRE) {
992 char* msg = SvPVx(GvSV(errgv), na);
993 DIE("%s", *msg ? msg : "Compilation failed in require");
994 }
a0d0e21e
LW
995 return pop_return();
996 }
997 }
760ac839
LW
998 PerlIO_printf(PerlIO_stderr(), "%s",message);
999 PerlIO_flush(PerlIO_stderr());
f86702cc
PP
1000 my_failure_exit();
1001 /* NOTREACHED */
a0d0e21e
LW
1002 return 0;
1003}
1004
1005PP(pp_xor)
1006{
1007 dSP; dPOPTOPssrl;
1008 if (SvTRUE(left) != SvTRUE(right))
1009 RETSETYES;
1010 else
1011 RETSETNO;
1012}
1013
1014PP(pp_andassign)
1015{
1016 dSP;
1017 if (!SvTRUE(TOPs))
1018 RETURN;
1019 else
1020 RETURNOP(cLOGOP->op_other);
1021}
1022
1023PP(pp_orassign)
1024{
1025 dSP;
1026 if (SvTRUE(TOPs))
1027 RETURN;
1028 else
1029 RETURNOP(cLOGOP->op_other);
1030}
1031
1032#ifdef DEPRECATED
1033PP(pp_entersubr)
1034{
1035 dSP;
1036 SV** mark = (stack_base + *markstack_ptr + 1);
1037 SV* cv = *mark;
1038 while (mark < sp) { /* emulate old interface */
1039 *mark = mark[1];
1040 mark++;
1041 }
1042 *sp = cv;
1043 return pp_entersub();
1044}
1045#endif
1046
1047PP(pp_caller)
1048{
1049 dSP;
1050 register I32 cxix = dopoptosub(cxstack_ix);
1051 register CONTEXT *cx;
1052 I32 dbcxix;
54310121 1053 I32 gimme;
a0d0e21e
LW
1054 SV *sv;
1055 I32 count = 0;
1056
1057 if (MAXARG)
1058 count = POPi;
1059 EXTEND(SP, 6);
1060 for (;;) {
1061 if (cxix < 0) {
1062 if (GIMME != G_ARRAY)
1063 RETPUSHUNDEF;
1064 RETURN;
1065 }
1066 if (DBsub && cxix >= 0 &&
1067 cxstack[cxix].blk_sub.cv == GvCV(DBsub))
1068 count++;
1069 if (!count--)
1070 break;
1071 cxix = dopoptosub(cxix - 1);
1072 }
1073 cx = &cxstack[cxix];
06a5b730
PP
1074 if (cxstack[cxix].cx_type == CXt_SUB) {
1075 dbcxix = dopoptosub(cxix - 1);
1076 /* We expect that cxstack[dbcxix] is CXt_SUB, anyway, the
1077 field below is defined for any cx. */
1078 if (DBsub && dbcxix >= 0 && cxstack[dbcxix].blk_sub.cv == GvCV(DBsub))
1079 cx = &cxstack[dbcxix];
1080 }
1081
a0d0e21e
LW
1082 if (GIMME != G_ARRAY) {
1083 dTARGET;
1084
1085 sv_setpv(TARG, HvNAME(cx->blk_oldcop->cop_stash));
1086 PUSHs(TARG);
1087 RETURN;
1088 }
a0d0e21e
LW
1089
1090 PUSHs(sv_2mortal(newSVpv(HvNAME(cx->blk_oldcop->cop_stash), 0)));
1091 PUSHs(sv_2mortal(newSVpv(SvPVX(GvSV(cx->blk_oldcop->cop_filegv)), 0)));
1092 PUSHs(sv_2mortal(newSViv((I32)cx->blk_oldcop->cop_line)));
1093 if (!MAXARG)
1094 RETURN;
06a5b730 1095 if (cx->cx_type == CXt_SUB) { /* So is cxstack[dbcxix]. */
a0d0e21e 1096 sv = NEWSV(49, 0);
e5cf08de 1097 gv_efullname3(sv, CvGV(cxstack[cxix].blk_sub.cv), Nullch);
a0d0e21e
LW
1098 PUSHs(sv_2mortal(sv));
1099 PUSHs(sv_2mortal(newSViv((I32)cx->blk_sub.hasargs)));
1100 }
1101 else {
1102 PUSHs(sv_2mortal(newSVpv("(eval)",0)));
1103 PUSHs(sv_2mortal(newSViv(0)));
1104 }
54310121
PP
1105 gimme = (I32)cx->blk_gimme;
1106 if (gimme == G_VOID)
1107 PUSHs(&sv_undef);
1108 else
1109 PUSHs(sv_2mortal(newSViv(gimme & G_ARRAY)));
4633a7c4 1110 if (cx->cx_type == CXt_EVAL) {
06a5b730 1111 if (cx->blk_eval.old_op_type == OP_ENTEREVAL) {
4633a7c4 1112 PUSHs(cx->blk_eval.cur_text);
06a5b730
PP
1113 PUSHs(&sv_no);
1114 }
1115 else if (cx->blk_eval.old_name) { /* Try blocks have old_name == 0. */
1116 /* Require, put the name. */
1117 PUSHs(sv_2mortal(newSVpv(cx->blk_eval.old_name, 0)));
1118 PUSHs(&sv_yes);
1119 }
4633a7c4
LW
1120 }
1121 else if (cx->cx_type == CXt_SUB &&
1122 cx->blk_sub.hasargs &&
1123 curcop->cop_stash == debstash)
1124 {
a0d0e21e
LW
1125 AV *ary = cx->blk_sub.argarray;
1126 int off = AvARRAY(ary) - AvALLOC(ary);
1127
1128 if (!dbargs) {
1129 GV* tmpgv;
1130 dbargs = GvAV(gv_AVadd(tmpgv = gv_fetchpv("DB::args", TRUE,
1131 SVt_PVAV)));
a5f75d66 1132 GvMULTI_on(tmpgv);
a0d0e21e
LW
1133 AvREAL_off(dbargs); /* XXX Should be REIFY */
1134 }
1135
1136 if (AvMAX(dbargs) < AvFILL(ary) + off)
1137 av_extend(dbargs, AvFILL(ary) + off);
1138 Copy(AvALLOC(ary), AvARRAY(dbargs), AvFILL(ary) + 1 + off, SV*);
1139 AvFILL(dbargs) = AvFILL(ary) + off;
1140 }
1141 RETURN;
1142}
1143
1144static int
1145sortcv(a, b)
1146const void *a;
1147const void *b;
1148{
9607fc9c
PP
1149 SV * const *str1 = (SV * const *)a;
1150 SV * const *str2 = (SV * const *)b;
748a9306 1151 I32 oldsaveix = savestack_ix;
a0d0e21e
LW
1152 I32 oldscopeix = scopestack_ix;
1153 I32 result;
1154 GvSV(firstgv) = *str1;
1155 GvSV(secondgv) = *str2;
1156 stack_sp = stack_base;
1157 op = sortcop;
a6c477ed 1158 runops();
a0d0e21e
LW
1159 if (stack_sp != stack_base + 1)
1160 croak("Sort subroutine didn't return single value");
748a9306 1161 if (!SvNIOKp(*stack_sp))
a0d0e21e
LW
1162 croak("Sort subroutine didn't return a numeric value");
1163 result = SvIV(*stack_sp);
1164 while (scopestack_ix > oldscopeix) {
1165 LEAVE;
1166 }
748a9306 1167 leave_scope(oldsaveix);
a0d0e21e
LW
1168 return result;
1169}
1170
1171static int
1172sortcmp(a, b)
1173const void *a;
1174const void *b;
1175{
9607fc9c 1176 return sv_cmp(*(SV * const *)a, *(SV * const *)b);
bbce6d69 1177}
e5cf08de 1178
bbce6d69
PP
1179static int
1180sortcmp_locale(a, b)
1181const void *a;
1182const void *b;
1183{
9607fc9c 1184 return sv_cmp_locale(*(SV * const *)a, *(SV * const *)b);
a0d0e21e
LW
1185}
1186
1187PP(pp_reset)
1188{
1189 dSP;
1190 char *tmps;
1191
1192 if (MAXARG < 1)
1193 tmps = "";
1194 else
1195 tmps = POPp;
1196 sv_reset(tmps, curcop->cop_stash);
1197 PUSHs(&sv_yes);
1198 RETURN;
1199}
1200
1201PP(pp_lineseq)
1202{
1203 return NORMAL;
1204}
1205
1206PP(pp_dbstate)
1207{
1208 curcop = (COP*)op;
1209 TAINT_NOT; /* Each statement is presumed innocent */
1210 stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
1211 FREETMPS;
1212
1213 if (op->op_private || SvIV(DBsingle) || SvIV(DBsignal) || SvIV(DBtrace))
1214 {
1215 SV **sp;
1216 register CV *cv;
1217 register CONTEXT *cx;
748a9306 1218 I32 gimme = G_ARRAY;
a0d0e21e
LW
1219 I32 hasargs;
1220 GV *gv;
1221
a0d0e21e
LW
1222 gv = DBgv;
1223 cv = GvCV(gv);
a0d0e21e
LW
1224 if (!cv)
1225 DIE("No DB::DB routine defined");
1226
06a5b730 1227 if (CvDEPTH(cv) >= 1 && !(debug & (1<<30))) /* don't do recursive DB::DB call */
a0d0e21e 1228 return NORMAL;
748a9306 1229
4633a7c4
LW
1230 ENTER;
1231 SAVETMPS;
1232
748a9306 1233 SAVEI32(debug);
55497cff 1234 SAVESTACK_POS();
748a9306
LW
1235 debug = 0;
1236 hasargs = 0;
1237 sp = stack_sp;
1238
a0d0e21e 1239 push_return(op->op_next);
748a9306 1240 PUSHBLOCK(cx, CXt_SUB, sp);
a0d0e21e
LW
1241 PUSHSUB(cx);
1242 CvDEPTH(cv)++;
1243 (void)SvREFCNT_inc(cv);
1244 SAVESPTR(curpad);
1245 curpad = AvARRAY((AV*)*av_fetch(CvPADLIST(cv),1,FALSE));
1246 RETURNOP(CvSTART(cv));
1247 }
1248 else
1249 return NORMAL;
1250}
1251
1252PP(pp_scope)
1253{
1254 return NORMAL;
1255}
1256
1257PP(pp_enteriter)
1258{
1259 dSP; dMARK;
1260 register CONTEXT *cx;
54310121 1261 I32 gimme = GIMME_V;
a0d0e21e
LW
1262 SV **svp;
1263
4633a7c4
LW
1264 ENTER;
1265 SAVETMPS;
1266
a0d0e21e
LW
1267 if (op->op_targ)
1268 svp = &curpad[op->op_targ]; /* "my" variable */
1269 else
1270 svp = &GvSV((GV*)POPs); /* symbol table variable */
1271
4633a7c4
LW
1272 SAVESPTR(*svp);
1273
a0d0e21e
LW
1274 ENTER;
1275
1276 PUSHBLOCK(cx, CXt_LOOP, SP);
1277 PUSHLOOP(cx, svp, MARK);
44a8e56a
PP
1278 if (op->op_flags & OPf_STACKED)
1279 cx->blk_loop.iterary = (AV*)SvREFCNT_inc(POPs);
4633a7c4 1280 else {
1ce6579f
PP
1281 cx->blk_loop.iterary = curstack;
1282 AvFILL(curstack) = sp - stack_base;
4633a7c4
LW
1283 cx->blk_loop.iterix = MARK - stack_base;
1284 }
a0d0e21e
LW
1285
1286 RETURN;
1287}
1288
1289PP(pp_enterloop)
1290{
1291 dSP;
1292 register CONTEXT *cx;
54310121 1293 I32 gimme = GIMME_V;
a0d0e21e
LW
1294
1295 ENTER;
1296 SAVETMPS;
1297 ENTER;
1298
1299 PUSHBLOCK(cx, CXt_LOOP, SP);
1300 PUSHLOOP(cx, 0, SP);
1301
1302 RETURN;
1303}
1304
1305PP(pp_leaveloop)
1306{
1307 dSP;
1308 register CONTEXT *cx;
f86702cc 1309 struct block_loop cxloop;
a0d0e21e
LW
1310 I32 gimme;
1311 SV **newsp;
1312 PMOP *newpm;
1313 SV **mark;
1314
1315 POPBLOCK(cx,newpm);
4fdae800 1316 mark = newsp;
f86702cc
PP
1317 POPLOOP1(cx); /* Delay POPLOOP2 until stack values are safe */
1318
a1f49e72 1319 TAINT_NOT;
54310121
PP
1320 if (gimme == G_VOID)
1321 ; /* do nothing */
1322 else if (gimme == G_SCALAR) {
1323 if (mark < SP)
1324 *++newsp = sv_mortalcopy(*SP);
1325 else
1326 *++newsp = &sv_undef;
a0d0e21e
LW
1327 }
1328 else {
a1f49e72 1329 while (mark < SP) {
a0d0e21e 1330 *++newsp = sv_mortalcopy(*++mark);
a1f49e72
CS
1331 TAINT_NOT; /* Each item is independent */
1332 }
a0d0e21e 1333 }
f86702cc
PP
1334 SP = newsp;
1335 PUTBACK;
1336
1337 POPLOOP2(); /* Stack values are safe: release loop vars ... */
1338 curpm = newpm; /* ... and pop $1 et al */
1339
a0d0e21e
LW
1340 LEAVE;
1341 LEAVE;
1342
f86702cc 1343 return NORMAL;
a0d0e21e
LW
1344}
1345
1346PP(pp_return)
1347{
1348 dSP; dMARK;
1349 I32 cxix;
1350 register CONTEXT *cx;
f86702cc
PP
1351 struct block_sub cxsub;
1352 bool popsub2 = FALSE;
a0d0e21e
LW
1353 I32 gimme;
1354 SV **newsp;
1355 PMOP *newpm;
1356 I32 optype = 0;
1357
1ce6579f 1358 if (curstack == sortstack) {
a0d0e21e 1359 if (cxstack_ix == sortcxix || dopoptosub(cxstack_ix) < sortcxix) {
16d20bd9
AD
1360 if (cxstack_ix > sortcxix)
1361 dounwind(sortcxix);
1ce6579f 1362 AvARRAY(curstack)[1] = *SP;
a0d0e21e
LW
1363 stack_sp = stack_base + 1;
1364 return 0;
1365 }
1366 }
1367
1368 cxix = dopoptosub(cxstack_ix);
1369 if (cxix < 0)
1370 DIE("Can't return outside a subroutine");
1371 if (cxix < cxstack_ix)
1372 dounwind(cxix);
1373
1374 POPBLOCK(cx,newpm);
1375 switch (cx->cx_type) {
1376 case CXt_SUB:
f86702cc
PP
1377 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1378 popsub2 = TRUE;
a0d0e21e
LW
1379 break;
1380 case CXt_EVAL:
1381 POPEVAL(cx);
748a9306
LW
1382 if (optype == OP_REQUIRE &&
1383 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
1384 {
54310121 1385 /* Unassume the success we assumed earlier. */
748a9306
LW
1386 char *name = cx->blk_eval.old_name;
1387 (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
1388 DIE("%s did not return a true value", name);
1389 }
a0d0e21e
LW
1390 break;
1391 default:
1392 DIE("panic: return");
a0d0e21e
LW
1393 }
1394
a1f49e72 1395 TAINT_NOT;
a0d0e21e
LW
1396 if (gimme == G_SCALAR) {
1397 if (MARK < SP)
f86702cc
PP
1398 *++newsp = (popsub2 && SvTEMP(*SP))
1399 ? *SP : sv_mortalcopy(*SP);
a0d0e21e
LW
1400 else
1401 *++newsp = &sv_undef;
a0d0e21e 1402 }
54310121 1403 else if (gimme == G_ARRAY) {
a1f49e72 1404 while (++MARK <= SP) {
f86702cc
PP
1405 *++newsp = (popsub2 && SvTEMP(*MARK))
1406 ? *MARK : sv_mortalcopy(*MARK);
a1f49e72
CS
1407 TAINT_NOT; /* Each item is independent */
1408 }
a0d0e21e 1409 }
a0d0e21e
LW
1410 stack_sp = newsp;
1411
f86702cc
PP
1412 /* Stack values are safe: */
1413 if (popsub2) {
1414 POPSUB2(); /* release CV and @_ ... */
1415 }
1416 curpm = newpm; /* ... and pop $1 et al */
1417
a0d0e21e
LW
1418 LEAVE;
1419 return pop_return();
1420}
1421
1422PP(pp_last)
1423{
1424 dSP;
1425 I32 cxix;
1426 register CONTEXT *cx;
f86702cc
PP
1427 struct block_loop cxloop;
1428 struct block_sub cxsub;
1429 I32 pop2 = 0;
a0d0e21e
LW
1430 I32 gimme;
1431 I32 optype;
1432 OP *nextop;
1433 SV **newsp;
1434 PMOP *newpm;
1435 SV **mark = stack_base + cxstack[cxstack_ix].blk_oldsp;
a0d0e21e
LW
1436
1437 if (op->op_flags & OPf_SPECIAL) {
1438 cxix = dopoptoloop(cxstack_ix);
1439 if (cxix < 0)
1440 DIE("Can't \"last\" outside a block");
1441 }
1442 else {
1443 cxix = dopoptolabel(cPVOP->op_pv);
1444 if (cxix < 0)
1445 DIE("Label not found for \"last %s\"", cPVOP->op_pv);
1446 }
1447 if (cxix < cxstack_ix)
1448 dounwind(cxix);
1449
1450 POPBLOCK(cx,newpm);
1451 switch (cx->cx_type) {
1452 case CXt_LOOP:
f86702cc
PP
1453 POPLOOP1(cx); /* Delay POPLOOP2 until stack values are safe */
1454 pop2 = CXt_LOOP;
4fdae800 1455 nextop = cxloop.last_op->op_next;
a0d0e21e 1456 break;
f86702cc
PP
1457 case CXt_SUB:
1458 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1459 pop2 = CXt_SUB;
a0d0e21e
LW
1460 nextop = pop_return();
1461 break;
f86702cc
PP
1462 case CXt_EVAL:
1463 POPEVAL(cx);
a0d0e21e
LW
1464 nextop = pop_return();
1465 break;
1466 default:
1467 DIE("panic: last");
a0d0e21e
LW
1468 }
1469
a1f49e72 1470 TAINT_NOT;
a0d0e21e 1471 if (gimme == G_SCALAR) {
f86702cc
PP
1472 if (MARK < SP)
1473 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
1474 ? *SP : sv_mortalcopy(*SP);
a0d0e21e
LW
1475 else
1476 *++newsp = &sv_undef;
1477 }
54310121 1478 else if (gimme == G_ARRAY) {
a1f49e72 1479 while (++MARK <= SP) {
f86702cc
PP
1480 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
1481 ? *MARK : sv_mortalcopy(*MARK);
a1f49e72
CS
1482 TAINT_NOT; /* Each item is independent */
1483 }
f86702cc
PP
1484 }
1485 SP = newsp;
1486 PUTBACK;
1487
1488 /* Stack values are safe: */
1489 switch (pop2) {
1490 case CXt_LOOP:
1491 POPLOOP2(); /* release loop vars ... */
4fdae800 1492 LEAVE;
f86702cc
PP
1493 break;
1494 case CXt_SUB:
1495 POPSUB2(); /* release CV and @_ ... */
1496 break;
a0d0e21e 1497 }
f86702cc 1498 curpm = newpm; /* ... and pop $1 et al */
a0d0e21e
LW
1499
1500 LEAVE;
f86702cc 1501 return nextop;
a0d0e21e
LW
1502}
1503
1504PP(pp_next)
1505{
1506 I32 cxix;
1507 register CONTEXT *cx;
1508 I32 oldsave;
1509
1510 if (op->op_flags & OPf_SPECIAL) {
1511 cxix = dopoptoloop(cxstack_ix);
1512 if (cxix < 0)
1513 DIE("Can't \"next\" outside a block");
1514 }
1515 else {
1516 cxix = dopoptolabel(cPVOP->op_pv);
1517 if (cxix < 0)
1518 DIE("Label not found for \"next %s\"", cPVOP->op_pv);
1519 }
1520 if (cxix < cxstack_ix)
1521 dounwind(cxix);
1522
1523 TOPBLOCK(cx);
1524 oldsave = scopestack[scopestack_ix - 1];
1525 LEAVE_SCOPE(oldsave);
1526 return cx->blk_loop.next_op;
1527}
1528
1529PP(pp_redo)
1530{
1531 I32 cxix;
1532 register CONTEXT *cx;
1533 I32 oldsave;
1534
1535 if (op->op_flags & OPf_SPECIAL) {
1536 cxix = dopoptoloop(cxstack_ix);
1537 if (cxix < 0)
1538 DIE("Can't \"redo\" outside a block");
1539 }
1540 else {
1541 cxix = dopoptolabel(cPVOP->op_pv);
1542 if (cxix < 0)
1543 DIE("Label not found for \"redo %s\"", cPVOP->op_pv);
1544 }
1545 if (cxix < cxstack_ix)
1546 dounwind(cxix);
1547
1548 TOPBLOCK(cx);
1549 oldsave = scopestack[scopestack_ix - 1];
1550 LEAVE_SCOPE(oldsave);
1551 return cx->blk_loop.redo_op;
1552}
1553
1554static OP* lastgotoprobe;
1555
1556static OP *
fc36a67e 1557dofindlabel(op,label,opstack,oplimit)
a0d0e21e
LW
1558OP *op;
1559char *label;
1560OP **opstack;
fc36a67e 1561OP **oplimit;
a0d0e21e
LW
1562{
1563 OP *kid;
1564 OP **ops = opstack;
fc36a67e 1565 static char too_deep[] = "Target of goto is too deeply nested";
a0d0e21e 1566
fc36a67e
PP
1567 if (ops >= oplimit)
1568 croak(too_deep);
a0d0e21e
LW
1569 if (op->op_type == OP_LEAVE ||
1570 op->op_type == OP_SCOPE ||
1571 op->op_type == OP_LEAVELOOP ||
1572 op->op_type == OP_LEAVETRY)
fc36a67e
PP
1573 {
1574 *ops++ = cUNOP->op_first;
1575 if (ops >= oplimit)
1576 croak(too_deep);
1577 }
a0d0e21e
LW
1578 *ops = 0;
1579 if (op->op_flags & OPf_KIDS) {
1580 /* First try all the kids at this level, since that's likeliest. */
1581 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling) {
1582 if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
1583 kCOP->cop_label && strEQ(kCOP->cop_label, label))
1584 return kid;
1585 }
1586 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling) {
1587 if (kid == lastgotoprobe)
1588 continue;
fc36a67e
PP
1589 if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
1590 (ops == opstack ||
1591 (ops[-1]->op_type != OP_NEXTSTATE &&
1592 ops[-1]->op_type != OP_DBSTATE)))
1593 *ops++ = kid;
1594 if (op = dofindlabel(kid, label, ops, oplimit))
a0d0e21e
LW
1595 return op;
1596 }
1597 }
1598 *ops = 0;
1599 return 0;
1600}
1601
1602PP(pp_dump)
1603{
1604 return pp_goto(ARGS);
1605 /*NOTREACHED*/
1606}
1607
1608PP(pp_goto)
1609{
1610 dSP;
1611 OP *retop = 0;
1612 I32 ix;
1613 register CONTEXT *cx;
fc36a67e
PP
1614#define GOTO_DEPTH 64
1615 OP *enterops[GOTO_DEPTH];
a0d0e21e
LW
1616 char *label;
1617 int do_dump = (op->op_type == OP_DUMP);
1618
1619 label = 0;
1620 if (op->op_flags & OPf_STACKED) {
1621 SV *sv = POPs;
1622
1623 /* This egregious kludge implements goto &subroutine */
1624 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
1625 I32 cxix;
1626 register CONTEXT *cx;
1627 CV* cv = (CV*)SvRV(sv);
1628 SV** mark;
1629 I32 items = 0;
1630 I32 oldsave;
1631
4aa0a1f7
AD
1632 if (!CvROOT(cv) && !CvXSUB(cv)) {
1633 if (CvGV(cv)) {
1634 SV *tmpstr = sv_newmortal();
e5cf08de 1635 gv_efullname3(tmpstr, CvGV(cv), Nullch);
4aa0a1f7
AD
1636 DIE("Goto undefined subroutine &%s",SvPVX(tmpstr));
1637 }
1638 DIE("Goto undefined subroutine");
1639 }
1640
a0d0e21e
LW
1641 /* First do some returnish stuff. */
1642 cxix = dopoptosub(cxstack_ix);
1643 if (cxix < 0)
1644 DIE("Can't goto subroutine outside a subroutine");
1645 if (cxix < cxstack_ix)
1646 dounwind(cxix);
1647 TOPBLOCK(cx);
1648 mark = stack_sp;
1649 if (cx->blk_sub.hasargs) { /* put @_ back onto stack */
1650 AV* av = cx->blk_sub.argarray;
1651
1652 items = AvFILL(av) + 1;
1ce6579f
PP
1653 stack_sp++;
1654 EXTEND(stack_sp, items); /* @_ could have been extended. */
1655 Copy(AvARRAY(av), stack_sp, items, SV*);
a0d0e21e 1656 stack_sp += items;
2c05e328 1657 SvREFCNT_dec(GvAV(defgv));
a0d0e21e 1658 GvAV(defgv) = cx->blk_sub.savearray;
a0d0e21e 1659 AvREAL_off(av);
4633a7c4 1660 av_clear(av);
a0d0e21e
LW
1661 }
1662 if (!(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
1663 SvREFCNT_dec(cx->blk_sub.cv);
1664 oldsave = scopestack[scopestack_ix - 1];
1665 LEAVE_SCOPE(oldsave);
1666
1667 /* Now do some callish stuff. */
1668 SAVETMPS;
1669 if (CvXSUB(cv)) {
1670 if (CvOLDSTYLE(cv)) {
ecfc5424 1671 I32 (*fp3)_((int,int,int));
a0d0e21e
LW
1672 while (sp > mark) {
1673 sp[1] = sp[0];
1674 sp--;
1675 }
ecfc5424
AD
1676 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
1677 items = (*fp3)(CvXSUBANY(cv).any_i32,
1678 mark - stack_base + 1,
1679 items);
a0d0e21e
LW
1680 sp = stack_base + items;
1681 }
1682 else {
1ce6579f 1683 stack_sp--; /* There is no cv arg. */
a0d0e21e
LW
1684 (void)(*CvXSUB(cv))(cv);
1685 }
1686 LEAVE;
1687 return pop_return();
1688 }
1689 else {
1690 AV* padlist = CvPADLIST(cv);
1691 SV** svp = AvARRAY(padlist);
1692 cx->blk_sub.cv = cv;
1693 cx->blk_sub.olddepth = CvDEPTH(cv);
1694 CvDEPTH(cv)++;
1695 if (CvDEPTH(cv) < 2)
1696 (void)SvREFCNT_inc(cv);
1697 else { /* save temporaries on recursion? */
1698 if (CvDEPTH(cv) == 100 && dowarn)
44a8e56a 1699 sub_crush_depth(cv);
a0d0e21e
LW
1700 if (CvDEPTH(cv) > AvFILL(padlist)) {
1701 AV *newpad = newAV();
4aa0a1f7 1702 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
a0d0e21e
LW
1703 I32 ix = AvFILL((AV*)svp[1]);
1704 svp = AvARRAY(svp[0]);
748a9306 1705 for ( ;ix > 0; ix--) {
a0d0e21e 1706 if (svp[ix] != &sv_undef) {
748a9306 1707 char *name = SvPVX(svp[ix]);
5f05dabc
PP
1708 if ((SvFLAGS(svp[ix]) & SVf_FAKE)
1709 || *name == '&')
1710 {
1711 /* outer lexical or anon code */
748a9306 1712 av_store(newpad, ix,
4aa0a1f7 1713 SvREFCNT_inc(oldpad[ix]) );
748a9306
LW
1714 }
1715 else { /* our own lexical */
1716 if (*name == '@')
1717 av_store(newpad, ix, sv = (SV*)newAV());
1718 else if (*name == '%')
1719 av_store(newpad, ix, sv = (SV*)newHV());
1720 else
1721 av_store(newpad, ix, sv = NEWSV(0,0));
1722 SvPADMY_on(sv);
1723 }
a0d0e21e
LW
1724 }
1725 else {
748a9306 1726 av_store(newpad, ix, sv = NEWSV(0,0));
a0d0e21e
LW
1727 SvPADTMP_on(sv);
1728 }
1729 }
1730 if (cx->blk_sub.hasargs) {
1731 AV* av = newAV();
1732 av_extend(av, 0);
1733 av_store(newpad, 0, (SV*)av);
1734 AvFLAGS(av) = AVf_REIFY;
1735 }
1736 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
1737 AvFILL(padlist) = CvDEPTH(cv);
1738 svp = AvARRAY(padlist);
1739 }
1740 }
1741 SAVESPTR(curpad);
1742 curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
1743 if (cx->blk_sub.hasargs) {
1744 AV* av = (AV*)curpad[0];
1745 SV** ary;
1746
1747 cx->blk_sub.savearray = GvAV(defgv);
1748 cx->blk_sub.argarray = av;
2c05e328 1749 GvAV(defgv) = (AV*)SvREFCNT_inc(av);
a0d0e21e
LW
1750 ++mark;
1751
1752 if (items >= AvMAX(av) + 1) {
1753 ary = AvALLOC(av);
1754 if (AvARRAY(av) != ary) {
1755 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
1756 SvPVX(av) = (char*)ary;
1757 }
1758 if (items >= AvMAX(av) + 1) {
1759 AvMAX(av) = items - 1;
1760 Renew(ary,items+1,SV*);
1761 AvALLOC(av) = ary;
1762 SvPVX(av) = (char*)ary;
1763 }
1764 }
1765 Copy(mark,AvARRAY(av),items,SV*);
1766 AvFILL(av) = items - 1;
1767
1768 while (items--) {
1769 if (*mark)
1770 SvTEMP_off(*mark);
1771 mark++;
1772 }
1773 }
f967eb5f 1774 if (perldb && curstash != debstash) {
44a8e56a
PP
1775 /*
1776 * We do not care about using sv to call CV;
1777 * it's for informational purposes only.
1778 */
1ce6579f
PP
1779 SV *sv = GvSV(DBsub);
1780 save_item(sv);
e5cf08de 1781 gv_efullname3(sv, CvGV(cv), Nullch);
1ce6579f 1782 }
a0d0e21e
LW
1783 RETURNOP(CvSTART(cv));
1784 }
1785 }
1786 else
1787 label = SvPV(sv,na);
1788 }
1789 else if (op->op_flags & OPf_SPECIAL) {
1790 if (! do_dump)
1791 DIE("goto must have label");
1792 }
1793 else
1794 label = cPVOP->op_pv;
1795
1796 if (label && *label) {
1797 OP *gotoprobe = 0;
1798
1799 /* find label */
1800
1801 lastgotoprobe = 0;
1802 *enterops = 0;
1803 for (ix = cxstack_ix; ix >= 0; ix--) {
1804 cx = &cxstack[ix];
1805 switch (cx->cx_type) {
1806 case CXt_SUB:
1807 gotoprobe = CvROOT(cx->blk_sub.cv);
1808 break;
1809 case CXt_EVAL:
1810 gotoprobe = eval_root; /* XXX not good for nested eval */
1811 break;
1812 case CXt_LOOP:
1813 gotoprobe = cx->blk_oldcop->op_sibling;
1814 break;
1815 case CXt_SUBST:
1816 continue;
1817 case CXt_BLOCK:
1818 if (ix)
1819 gotoprobe = cx->blk_oldcop->op_sibling;
1820 else
1821 gotoprobe = main_root;
1822 break;
0a753a76
PP
1823 case CXt_NULL:
1824 DIE("Can't \"goto\" outside a block");
a0d0e21e
LW
1825 default:
1826 if (ix)
1827 DIE("panic: goto");
68dc0745 1828 gotoprobe = main_root;
a0d0e21e
LW
1829 break;
1830 }
fc36a67e
PP
1831 retop = dofindlabel(gotoprobe, label,
1832 enterops, enterops + GOTO_DEPTH);
a0d0e21e
LW
1833 if (retop)
1834 break;
1835 lastgotoprobe = gotoprobe;
1836 }
1837 if (!retop)
1838 DIE("Can't find label %s", label);
1839
1840 /* pop unwanted frames */
1841
1842 if (ix < cxstack_ix) {
1843 I32 oldsave;
1844
1845 if (ix < 0)
1846 ix = 0;
1847 dounwind(ix);
1848 TOPBLOCK(cx);
1849 oldsave = scopestack[scopestack_ix];
1850 LEAVE_SCOPE(oldsave);
1851 }
1852
1853 /* push wanted frames */
1854
748a9306 1855 if (*enterops && enterops[1]) {
a0d0e21e 1856 OP *oldop = op;
748a9306 1857 for (ix = 1; enterops[ix]; ix++) {
a0d0e21e
LW
1858 op = enterops[ix];
1859 (*op->op_ppaddr)();
1860 }
1861 op = oldop;
1862 }
1863 }
1864
1865 if (do_dump) {
a5f75d66
AD
1866#ifdef VMS
1867 if (!retop) retop = main_start;
1868#endif
a0d0e21e
LW
1869 restartop = retop;
1870 do_undump = TRUE;
1871
1872 my_unexec();
1873
1874 restartop = 0; /* hmm, must be GNU unexec().. */
1875 do_undump = FALSE;
1876 }
1877
1ce6579f 1878 if (curstack == signalstack) {
748a9306 1879 restartop = retop;
54310121 1880 JMPENV_JUMP(3);
748a9306
LW
1881 }
1882
a0d0e21e
LW
1883 RETURNOP(retop);
1884}
1885
1886PP(pp_exit)
1887{
1888 dSP;
1889 I32 anum;
1890
1891 if (MAXARG < 1)
1892 anum = 0;
ff0cee69 1893 else {
a0d0e21e 1894 anum = SvIVx(POPs);
ff0cee69
PP
1895#ifdef VMSISH_EXIT
1896 if (anum == 1 && VMSISH_EXIT)
1897 anum = 0;
1898#endif
1899 }
a0d0e21e
LW
1900 my_exit(anum);
1901 PUSHs(&sv_undef);
1902 RETURN;
1903}
1904
1905#ifdef NOTYET
1906PP(pp_nswitch)
1907{
1908 dSP;
1909 double value = SvNVx(GvSV(cCOP->cop_gv));
1910 register I32 match = I_32(value);
1911
1912 if (value < 0.0) {
1913 if (((double)match) > value)
1914 --match; /* was fractional--truncate other way */
1915 }
1916 match -= cCOP->uop.scop.scop_offset;
1917 if (match < 0)
1918 match = 0;
1919 else if (match > cCOP->uop.scop.scop_max)
1920 match = cCOP->uop.scop.scop_max;
1921 op = cCOP->uop.scop.scop_next[match];
1922 RETURNOP(op);
1923}
1924
1925PP(pp_cswitch)
1926{
1927 dSP;
1928 register I32 match;
1929
1930 if (multiline)
1931 op = op->op_next; /* can't assume anything */
1932 else {
1933 match = *(SvPVx(GvSV(cCOP->cop_gv), na)) & 255;
1934 match -= cCOP->uop.scop.scop_offset;
1935 if (match < 0)
1936 match = 0;
1937 else if (match > cCOP->uop.scop.scop_max)
1938 match = cCOP->uop.scop.scop_max;
1939 op = cCOP->uop.scop.scop_next[match];
1940 }
1941 RETURNOP(op);
1942}
1943#endif
1944
1945/* Eval. */
1946
1947static void
1948save_lines(array, sv)
1949AV *array;
1950SV *sv;
1951{
1952 register char *s = SvPVX(sv);
1953 register char *send = SvPVX(sv) + SvCUR(sv);
1954 register char *t;
1955 register I32 line = 1;
1956
1957 while (s && s < send) {
1958 SV *tmpstr = NEWSV(85,0);
1959
1960 sv_upgrade(tmpstr, SVt_PVMG);
1961 t = strchr(s, '\n');
1962 if (t)
1963 t++;
1964 else
1965 t = send;
1966
1967 sv_setpvn(tmpstr, s, t - s);
1968 av_store(array, line++, tmpstr);
1969 s = t;
1970 }
1971}
1972
1973static OP *
1e422769
PP
1974docatch(o)
1975OP *o;
1976{
1977 int ret;
035902c7 1978 I32 oldrunlevel = runlevel;
1e422769 1979 OP *oldop = op;
54310121 1980 dJMPENV;
1e422769
PP
1981
1982 op = o;
1e422769 1983#ifdef DEBUGGING
54310121 1984 assert(CATCH_GET == TRUE);
035902c7 1985 DEBUG_l(deb("(Setting up local jumplevel, runlevel = %ld)\n", (long)runlevel+1));
1e422769 1986#endif
22921e25
CS
1987 JMPENV_PUSH(ret);
1988 switch (ret) {
1e422769 1989 default: /* topmost level handles it */
54310121 1990 JMPENV_POP;
1e422769 1991 runlevel = oldrunlevel;
1e422769 1992 op = oldop;
54310121 1993 JMPENV_JUMP(ret);
1e422769
PP
1994 /* NOTREACHED */
1995 case 3:
1996 if (!restartop) {
1997 PerlIO_printf(PerlIO_stderr(), "panic: restartop\n");
1998 break;
1999 }
1e422769
PP
2000 op = restartop;
2001 restartop = 0;
2002 /* FALL THROUGH */
2003 case 0:
2004 runops();
2005 break;
2006 }
54310121 2007 JMPENV_POP;
1e422769 2008 runlevel = oldrunlevel;
1e422769
PP
2009 op = oldop;
2010 return Nullop;
2011}
2012
2013static OP *
a0d0e21e
LW
2014doeval(gimme)
2015int gimme;
2016{
2017 dSP;
2018 OP *saveop = op;
2019 HV *newstash;
ff3ff8d1 2020 CV *caller;
748a9306 2021 AV* comppadlist;
a0d0e21e
LW
2022
2023 in_eval = 1;
2024
1ce6579f
PP
2025 PUSHMARK(SP);
2026
a0d0e21e
LW
2027 /* set up a scratch pad */
2028
55497cff 2029 SAVEI32(padix);
a0d0e21e
LW
2030 SAVESPTR(curpad);
2031 SAVESPTR(comppad);
2032 SAVESPTR(comppad_name);
55497cff
PP
2033 SAVEI32(comppad_name_fill);
2034 SAVEI32(min_intro_pending);
2035 SAVEI32(max_intro_pending);
748a9306 2036
ff3ff8d1 2037 caller = compcv;
748a9306
LW
2038 SAVESPTR(compcv);
2039 compcv = (CV*)NEWSV(1104,0);
2040 sv_upgrade((SV *)compcv, SVt_PVCV);
07055b4c 2041 CvUNIQUE_on(compcv);
748a9306 2042
a0d0e21e
LW
2043 comppad = newAV();
2044 comppad_name = newAV();
2045 comppad_name_fill = 0;
2046 min_intro_pending = 0;
2047 av_push(comppad, Nullsv);
2048 curpad = AvARRAY(comppad);
2049 padix = 0;
2050
748a9306
LW
2051 comppadlist = newAV();
2052 AvREAL_off(comppadlist);
8e07c86e
AD
2053 av_store(comppadlist, 0, (SV*)comppad_name);
2054 av_store(comppadlist, 1, (SV*)comppad);
748a9306 2055 CvPADLIST(compcv) = comppadlist;
2c05e328
CS
2056
2057 if (saveop->op_type != OP_REQUIRE)
2058 CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc(caller);
07055b4c 2059
8e07c86e 2060 SAVEFREESV(compcv);
748a9306 2061
a0d0e21e
LW
2062 /* make sure we compile in the right package */
2063
2064 newstash = curcop->cop_stash;
2065 if (curstash != newstash) {
2066 SAVESPTR(curstash);
2067 curstash = newstash;
2068 }
2069 SAVESPTR(beginav);
2070 beginav = newAV();
2071 SAVEFREESV(beginav);
2072
2073 /* try to compile it */
2074
2075 eval_root = Nullop;
2076 error_count = 0;
2077 curcop = &compiling;
2078 curcop->cop_arybase = 0;
c07a80fd
PP
2079 SvREFCNT_dec(rs);
2080 rs = newSVpv("\n", 1);
1ce6579f
PP
2081 if (saveop->op_flags & OPf_SPECIAL)
2082 in_eval |= 4;
2083 else
2084 sv_setpv(GvSV(errgv),"");
a0d0e21e
LW
2085 if (yyparse() || error_count || !eval_root) {
2086 SV **newsp;
2087 I32 gimme;
2088 CONTEXT *cx;
2089 I32 optype;
2090
2091 op = saveop;
2092 if (eval_root) {
2093 op_free(eval_root);
2094 eval_root = Nullop;
2095 }
1ce6579f 2096 SP = stack_base + POPMARK; /* pop original mark */
a0d0e21e
LW
2097 POPBLOCK(cx,curpm);
2098 POPEVAL(cx);
2099 pop_return();
2100 lex_end();
2101 LEAVE;
7a2e2cd6
PP
2102 if (optype == OP_REQUIRE) {
2103 char* msg = SvPVx(GvSV(errgv), na);
2104 DIE("%s", *msg ? msg : "Compilation failed in require");
2105 }
c07a80fd
PP
2106 SvREFCNT_dec(rs);
2107 rs = SvREFCNT_inc(nrs);
a0d0e21e
LW
2108 RETPUSHUNDEF;
2109 }
c07a80fd
PP
2110 SvREFCNT_dec(rs);
2111 rs = SvREFCNT_inc(nrs);
a0d0e21e 2112 compiling.cop_line = 0;
a0d0e21e 2113 SAVEFREEOP(eval_root);
54310121
PP
2114 if (gimme & G_VOID)
2115 scalarvoid(eval_root);
2116 else if (gimme & G_ARRAY)
a0d0e21e
LW
2117 list(eval_root);
2118 else
2119 scalar(eval_root);
2120
2121 DEBUG_x(dump_eval());
2122
55497cff 2123 /* Register with debugger: */
55497cff
PP
2124 if (perldb && saveop->op_type == OP_REQUIRE) {
2125 CV *cv = perl_get_cv("DB::postponed", FALSE);
55497cff
PP
2126 if (cv) {
2127 dSP;
2128 PUSHMARK(sp);
2129 XPUSHs((SV*)compiling.cop_filegv);
2130 PUTBACK;
2131 perl_call_sv((SV*)cv, G_DISCARD);
2132 }
2133 }
2134
a0d0e21e
LW
2135 /* compiled okay, so do it */
2136
4fdae800
PP
2137 CvDEPTH(compcv) = 1;
2138
1ce6579f 2139 SP = stack_base + POPMARK; /* pop original mark */
a0d0e21e
LW
2140 RETURNOP(eval_start);
2141}
2142
2143PP(pp_require)
2144{
2145 dSP;
2146 register CONTEXT *cx;
2147 SV *sv;
2148 char *name;
46fc3d4c
PP
2149 char *tryname;
2150 SV *namesv = Nullsv;
a0d0e21e
LW
2151 SV** svp;
2152 I32 gimme = G_SCALAR;
760ac839 2153 PerlIO *tryrsfp = 0;
a0d0e21e
LW
2154
2155 sv = POPs;
4633a7c4 2156 if (SvNIOKp(sv) && !SvPOKp(sv)) {
36477c24 2157 SET_NUMERIC_STANDARD();
a5f75d66
AD
2158 if (atof(patchlevel) + 0.00000999 < SvNV(sv))
2159 DIE("Perl %s required--this is only version %s, stopped",
2160 SvPV(sv,na),patchlevel);
a0d0e21e
LW
2161 RETPUSHYES;
2162 }
2163 name = SvPV(sv, na);
2164 if (!*name)
2165 DIE("Null filename used");
4633a7c4 2166 TAINT_PROPER("require");
a0d0e21e
LW
2167 if (op->op_type == OP_REQUIRE &&
2168 (svp = hv_fetch(GvHVn(incgv), name, SvCUR(sv), 0)) &&
2169 *svp != &sv_undef)
2170 RETPUSHYES;
2171
2172 /* prepare to compile file */
2173
46fc3d4c
PP
2174 if (*name == '/' ||
2175 (*name == '.' &&
2176 (name[1] == '/' ||
2177 (name[1] == '.' && name[2] == '/')))
4633a7c4 2178#ifdef DOSISH
46fc3d4c 2179 || (name[0] && name[1] == ':')
4633a7c4 2180#endif
748a9306 2181#ifdef VMS
46fc3d4c
PP
2182 || (strchr(name,':') || ((*name == '[' || *name == '<') &&
2183 (isALNUM(name[1]) || strchr("$-_]>",name[1]))))
748a9306
LW
2184#endif
2185 )
a0d0e21e 2186 {
46fc3d4c
PP
2187 tryname = name;
2188 tryrsfp = PerlIO_open(name,"r");
a0d0e21e
LW
2189 }
2190 else {
2191 AV *ar = GvAVn(incgv);
2192 I32 i;
748a9306 2193#ifdef VMS
46fc3d4c
PP
2194 char *unixname;
2195 if ((unixname = tounixspec(name, Nullch)) != Nullch)
2196#endif
2197 {
2198 namesv = NEWSV(806, 0);
2199 for (i = 0; i <= AvFILL(ar); i++) {
2200 char *dir = SvPVx(*av_fetch(ar, i, TRUE), na);
2201#ifdef VMS
2202 char *unixdir;
2203 if ((unixdir = tounixpath(dir, Nullch)) == Nullch)
2204 continue;
2205 sv_setpv(namesv, unixdir);
2206 sv_catpv(namesv, unixname);
748a9306 2207#else
46fc3d4c 2208 sv_setpvf(namesv, "%s/%s", dir, name);
748a9306 2209#endif
46fc3d4c
PP
2210 tryname = SvPVX(namesv);
2211 tryrsfp = PerlIO_open(tryname, "r");
2212 if (tryrsfp) {
2213 if (tryname[0] == '.' && tryname[1] == '/')
2214 tryname += 2;
2215 break;
2216 }
a0d0e21e
LW
2217 }
2218 }
2219 }
2220 SAVESPTR(compiling.cop_filegv);
46fc3d4c
PP
2221 compiling.cop_filegv = gv_fetchfile(tryrsfp ? tryname : name);
2222 SvREFCNT_dec(namesv);
a0d0e21e
LW
2223 if (!tryrsfp) {
2224 if (op->op_type == OP_REQUIRE) {
46fc3d4c
PP
2225 SV *msg = sv_2mortal(newSVpvf("Can't locate %s in @INC", name));
2226 if (instr(SvPVX(msg), ".h "))
2227 sv_catpv(msg, " (change .h to .ph maybe?)");
2228 if (instr(SvPVX(msg), ".ph "))
2229 sv_catpv(msg, " (did you run h2ph?)");
fc36a67e 2230 DIE("%_", msg);
a0d0e21e
LW
2231 }
2232
2233 RETPUSHUNDEF;
2234 }
2235
2236 /* Assume success here to prevent recursive requirement. */
2237 (void)hv_store(GvHVn(incgv), name, strlen(name),
2238 newSVsv(GvSV(compiling.cop_filegv)), 0 );
2239
2240 ENTER;
2241 SAVETMPS;
2242 lex_start(sv_2mortal(newSVpv("",0)));
e50aee73
AD
2243 if (rsfp_filters){
2244 save_aptr(&rsfp_filters);
2245 rsfp_filters = NULL;
2246 }
2247
a0d0e21e
LW
2248 rsfp = tryrsfp;
2249 name = savepv(name);
2250 SAVEFREEPV(name);
2251 SAVEI32(hints);
2252 hints = 0;
2253
2254 /* switch to eval mode */
2255
2256 push_return(op->op_next);
2257 PUSHBLOCK(cx, CXt_EVAL, SP);
2258 PUSHEVAL(cx, name, compiling.cop_filegv);
2259
2260 compiling.cop_line = 0;
2261
2262 PUTBACK;
1e422769 2263 return DOCATCH(doeval(G_SCALAR));
a0d0e21e
LW
2264}
2265
2266PP(pp_dofile)
2267{
2268 return pp_require(ARGS);
2269}
2270
2271PP(pp_entereval)
2272{
2273 dSP;
2274 register CONTEXT *cx;
2275 dPOPss;
54310121 2276 I32 gimme = GIMME_V, was = sub_generation;
fc36a67e
PP
2277 char tmpbuf[TYPE_DIGITS(long) + 12];
2278 char *safestr;
a0d0e21e 2279 STRLEN len;
55497cff 2280 OP *ret;
a0d0e21e
LW
2281
2282 if (!SvPV(sv,len) || !len)
2283 RETPUSHUNDEF;
748a9306 2284 TAINT_PROPER("eval");
a0d0e21e
LW
2285
2286 ENTER;
a0d0e21e 2287 lex_start(sv);
748a9306 2288 SAVETMPS;
a0d0e21e
LW
2289
2290 /* switch to eval mode */
2291
748a9306 2292 SAVESPTR(compiling.cop_filegv);
ff0cee69 2293 sprintf(tmpbuf, "_<(eval %lu)", (unsigned long)++evalseq);
a0d0e21e
LW
2294 compiling.cop_filegv = gv_fetchfile(tmpbuf+2);
2295 compiling.cop_line = 1;
55497cff
PP
2296 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
2297 deleting the eval's FILEGV from the stash before gv_check() runs
2298 (i.e. before run-time proper). To work around the coredump that
2299 ensues, we always turn GvMULTI_on for any globals that were
2300 introduced within evals. See force_ident(). GSAR 96-10-12 */
2301 safestr = savepv(tmpbuf);
2302 SAVEDELETE(defstash, safestr, strlen(safestr));
a0d0e21e
LW
2303 SAVEI32(hints);
2304 hints = op->op_targ;
2305
2306 push_return(op->op_next);
2307 PUSHBLOCK(cx, CXt_EVAL, SP);
2308 PUSHEVAL(cx, 0, compiling.cop_filegv);
2309
2310 /* prepare to compile string */
2311
2312 if (perldb && curstash != debstash)
2313 save_lines(GvAV(compiling.cop_filegv), linestr);
2314 PUTBACK;
55497cff
PP
2315 ret = doeval(gimme);
2316 if (perldb && was != sub_generation) { /* Some subs defined here. */
2317 strcpy(safestr, "_<(eval )"); /* Anything fake and short. */
2318 }
1e422769 2319 return DOCATCH(ret);
a0d0e21e
LW
2320}
2321
2322PP(pp_leaveeval)
2323{
2324 dSP;
2325 register SV **mark;
2326 SV **newsp;
2327 PMOP *newpm;
2328 I32 gimme;
2329 register CONTEXT *cx;
2330 OP *retop;
760ac839 2331 U8 save_flags = op -> op_flags;
a0d0e21e
LW
2332 I32 optype;
2333
2334 POPBLOCK(cx,newpm);
2335 POPEVAL(cx);
2336 retop = pop_return();
2337
a1f49e72 2338 TAINT_NOT;
54310121
PP
2339 if (gimme == G_VOID)
2340 MARK = newsp;
2341 else if (gimme == G_SCALAR) {
2342 MARK = newsp + 1;
2343 if (MARK <= SP) {
2344 if (SvFLAGS(TOPs) & SVs_TEMP)
2345 *MARK = TOPs;
2346 else
2347 *MARK = sv_mortalcopy(TOPs);
2348 }
a0d0e21e 2349 else {
54310121
PP
2350 MEXTEND(mark,0);
2351 *MARK = &sv_undef;
a0d0e21e 2352 }
a0d0e21e
LW
2353 }
2354 else {
a1f49e72
CS
2355 /* in case LEAVE wipes old return values */
2356 for (mark = newsp + 1; mark <= SP; mark++) {
2357 if (!(SvFLAGS(*mark) & SVs_TEMP)) {
a0d0e21e 2358 *mark = sv_mortalcopy(*mark);
a1f49e72
CS
2359 TAINT_NOT; /* Each item is independent */
2360 }
2361 }
a0d0e21e
LW
2362 }
2363 curpm = newpm; /* Don't pop $1 et al till now */
2364
4fdae800
PP
2365#ifdef DEBUGGING
2366 assert(CvDEPTH(compcv) == 1);
2367#endif
2368 CvDEPTH(compcv) = 0;
2369
1ce6579f 2370 if (optype == OP_REQUIRE &&
54310121
PP
2371 !(gimme == G_SCALAR ? SvTRUE(*sp) : sp > newsp))
2372 {
1ce6579f 2373 /* Unassume the success we assumed earlier. */
54310121 2374 char *name = cx->blk_eval.old_name;
1ce6579f
PP
2375 (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
2376 retop = die("%s did not return a true value", name);
a0d0e21e
LW
2377 }
2378
2379 lex_end();
2380 LEAVE;
4fdae800 2381
760ac839 2382 if (!(save_flags & OPf_SPECIAL))
1ce6579f 2383 sv_setpv(GvSV(errgv),"");
a0d0e21e
LW
2384
2385 RETURNOP(retop);
2386}
2387
a0d0e21e
LW
2388PP(pp_entertry)
2389{
2390 dSP;
2391 register CONTEXT *cx;
54310121 2392 I32 gimme = GIMME_V;
a0d0e21e
LW
2393
2394 ENTER;
2395 SAVETMPS;
2396
2397 push_return(cLOGOP->op_other->op_next);
2398 PUSHBLOCK(cx, CXt_EVAL, SP);
2399 PUSHEVAL(cx, 0, 0);
2400 eval_root = op; /* Only needed so that goto works right. */
2401
2402 in_eval = 1;
4633a7c4 2403 sv_setpv(GvSV(errgv),"");
1e422769
PP
2404 PUTBACK;
2405 return DOCATCH(op->op_next);
a0d0e21e
LW
2406}
2407
2408PP(pp_leavetry)
2409{
2410 dSP;
2411 register SV **mark;
2412 SV **newsp;
2413 PMOP *newpm;
2414 I32 gimme;
2415 register CONTEXT *cx;
2416 I32 optype;
2417
2418 POPBLOCK(cx,newpm);
2419 POPEVAL(cx);
2420 pop_return();
2421
a1f49e72 2422 TAINT_NOT;
54310121
PP
2423 if (gimme == G_VOID)
2424 SP = newsp;
2425 else if (gimme == G_SCALAR) {
2426 MARK = newsp + 1;
2427 if (MARK <= SP) {
2428 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
2429 *MARK = TOPs;
2430 else
2431 *MARK = sv_mortalcopy(TOPs);
2432 }
a0d0e21e 2433 else {
54310121
PP
2434 MEXTEND(mark,0);
2435 *MARK = &sv_undef;
a0d0e21e
LW
2436 }
2437 SP = MARK;
2438 }
2439 else {
a1f49e72
CS
2440 /* in case LEAVE wipes old return values */
2441 for (mark = newsp + 1; mark <= SP; mark++) {
2442 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
a0d0e21e 2443 *mark = sv_mortalcopy(*mark);
a1f49e72
CS
2444 TAINT_NOT; /* Each item is independent */
2445 }
2446 }
a0d0e21e
LW
2447 }
2448 curpm = newpm; /* Don't pop $1 et al till now */
2449
2450 LEAVE;
4633a7c4 2451 sv_setpv(GvSV(errgv),"");
a0d0e21e
LW
2452 RETURN;
2453}
2454
2455static void
2456doparseform(sv)
2457SV *sv;
2458{
2459 STRLEN len;
2460 register char *s = SvPV_force(sv, len);
2461 register char *send = s + len;
2462 register char *base;
2463 register I32 skipspaces = 0;
2464 bool noblank;
2465 bool repeat;
2466 bool postspace = FALSE;
2467 U16 *fops;
2468 register U16 *fpc;
2469 U16 *linepc;
2470 register I32 arg;
2471 bool ischop;
2472
55497cff 2473 if (len == 0)
bbce6d69 2474 croak("Null picture in formline");
55497cff
PP
2475
2476 New(804, fops, (send - s)*3+10, U16); /* Almost certainly too long... */
a0d0e21e
LW
2477 fpc = fops;
2478
2479 if (s < send) {
2480 linepc = fpc;
2481 *fpc++ = FF_LINEMARK;
2482 noblank = repeat = FALSE;
2483 base = s;
2484 }
2485
2486 while (s <= send) {
2487 switch (*s++) {
2488 default:
2489 skipspaces = 0;
2490 continue;
2491
2492 case '~':
2493 if (*s == '~') {
2494 repeat = TRUE;
2495 *s = ' ';
2496 }
2497 noblank = TRUE;
2498 s[-1] = ' ';
2499 /* FALL THROUGH */
2500 case ' ': case '\t':
2501 skipspaces++;
2502 continue;
2503
2504 case '\n': case 0:
2505 arg = s - base;
2506 skipspaces++;
2507 arg -= skipspaces;
2508 if (arg) {
5f05dabc 2509 if (postspace)
a0d0e21e 2510 *fpc++ = FF_SPACE;
a0d0e21e
LW
2511 *fpc++ = FF_LITERAL;
2512 *fpc++ = arg;
2513 }
5f05dabc 2514 postspace = FALSE;
a0d0e21e
LW
2515 if (s <= send)
2516 skipspaces--;
2517 if (skipspaces) {
2518 *fpc++ = FF_SKIP;
2519 *fpc++ = skipspaces;
2520 }
2521 skipspaces = 0;
2522 if (s <= send)
2523 *fpc++ = FF_NEWLINE;
2524 if (noblank) {
2525 *fpc++ = FF_BLANK;
2526 if (repeat)
2527 arg = fpc - linepc + 1;
2528 else
2529 arg = 0;
2530 *fpc++ = arg;
2531 }
2532 if (s < send) {
2533 linepc = fpc;
2534 *fpc++ = FF_LINEMARK;
2535 noblank = repeat = FALSE;
2536 base = s;
2537 }
2538 else
2539 s++;
2540 continue;
2541
2542 case '@':
2543 case '^':
2544 ischop = s[-1] == '^';
2545
2546 if (postspace) {
2547 *fpc++ = FF_SPACE;
2548 postspace = FALSE;
2549 }
2550 arg = (s - base) - 1;
2551 if (arg) {
2552 *fpc++ = FF_LITERAL;
2553 *fpc++ = arg;
2554 }
2555
2556 base = s - 1;
2557 *fpc++ = FF_FETCH;
2558 if (*s == '*') {
2559 s++;
2560 *fpc++ = 0;
2561 *fpc++ = FF_LINEGLOB;
2562 }
2563 else if (*s == '#' || (*s == '.' && s[1] == '#')) {
2564 arg = ischop ? 512 : 0;
2565 base = s - 1;
2566 while (*s == '#')
2567 s++;
2568 if (*s == '.') {
2569 char *f;
2570 s++;
2571 f = s;
2572 while (*s == '#')
2573 s++;
2574 arg |= 256 + (s - f);
2575 }
2576 *fpc++ = s - base; /* fieldsize for FETCH */
2577 *fpc++ = FF_DECIMAL;
2578 *fpc++ = arg;
2579 }
2580 else {
2581 I32 prespace = 0;
2582 bool ismore = FALSE;
2583
2584 if (*s == '>') {
2585 while (*++s == '>') ;
2586 prespace = FF_SPACE;
2587 }
2588 else if (*s == '|') {
2589 while (*++s == '|') ;
2590 prespace = FF_HALFSPACE;
2591 postspace = TRUE;
2592 }
2593 else {
2594 if (*s == '<')
2595 while (*++s == '<') ;
2596 postspace = TRUE;
2597 }
2598 if (*s == '.' && s[1] == '.' && s[2] == '.') {
2599 s += 3;
2600 ismore = TRUE;
2601 }
2602 *fpc++ = s - base; /* fieldsize for FETCH */
2603
2604 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
2605
2606 if (prespace)
2607 *fpc++ = prespace;
2608 *fpc++ = FF_ITEM;
2609 if (ismore)
2610 *fpc++ = FF_MORE;
2611 if (ischop)
2612 *fpc++ = FF_CHOP;
2613 }
2614 base = s;
2615 skipspaces = 0;
2616 continue;
2617 }
2618 }
2619 *fpc++ = FF_END;
2620
2621 arg = fpc - fops;
2622 { /* need to jump to the next word */
2623 int z;
2624 z = WORD_ALIGN - SvCUR(sv) % WORD_ALIGN;
2625 SvGROW(sv, SvCUR(sv) + z + arg * sizeof(U16) + 4);
2626 s = SvPVX(sv) + SvCUR(sv) + z;
2627 }
2628 Copy(fops, s, arg, U16);
2629 Safefree(fops);
55497cff 2630 sv_magic(sv, Nullsv, 'f', Nullch, 0);
a0d0e21e
LW
2631 SvCOMPILED_on(sv);
2632}