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