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