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