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