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