This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[shell changes from patch from perl5.003_27 to perl5.003_28]
[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
PP
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
PP
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
PP
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. */
9f8d30d5
CS
615 SvTEMP_off(*up);
616 if (!sortcop && !SvPOK(*up))
a0d0e21e 617 (void)sv_2pv(*up, &na);
a0d0e21e
LW
618 up++;
619 }
620 }
621 max = --up - myorigmark;
622 if (sortcop) {
623 if (max > 1) {
624 AV *oldstack;
625 CONTEXT *cx;
626 SV** newsp;
627
628 SAVETMPS;
629 SAVESPTR(op);
630
1ce6579f 631 oldstack = curstack;
a0d0e21e
LW
632 if (!sortstack) {
633 sortstack = newAV();
634 AvREAL_off(sortstack);
635 av_extend(sortstack, 32);
636 }
1ce6579f 637 SWITCHSTACK(curstack, sortstack);
a0d0e21e
LW
638 if (sortstash != stash) {
639 firstgv = gv_fetchpv("a", TRUE, SVt_PV);
640 secondgv = gv_fetchpv("b", TRUE, SVt_PV);
641 sortstash = stash;
642 }
643
644 SAVESPTR(GvSV(firstgv));
645 SAVESPTR(GvSV(secondgv));
0a753a76 646 PUSHBLOCK(cx, CXt_NULL, stack_base);
a0d0e21e
LW
647 sortcxix = cxstack_ix;
648
649 qsort((char*)(myorigmark+1), max, sizeof(SV*), sortcv);
650
651 POPBLOCK(cx,curpm);
652 SWITCHSTACK(sortstack, oldstack);
653 }
654 LEAVE;
655 }
656 else {
657 if (max > 1) {
658 MEXTEND(SP, 20); /* Can't afford stack realloc on signal. */
bbce6d69
PP
659 qsort((char*)(ORIGMARK+1), max, sizeof(SV*),
660 (op->op_private & OPpLOCALE) ? sortcmp_locale : sortcmp);
a0d0e21e
LW
661 }
662 }
663 stack_sp = ORIGMARK + max;
664 return nextop;
665}
666
667/* Range stuff. */
668
669PP(pp_range)
670{
671 if (GIMME == G_ARRAY)
672 return cCONDOP->op_true;
673 return SvTRUEx(PAD_SV(op->op_targ)) ? cCONDOP->op_false : cCONDOP->op_true;
674}
675
676PP(pp_flip)
677{
678 dSP;
679
680 if (GIMME == G_ARRAY) {
681 RETURNOP(((CONDOP*)cUNOP->op_first)->op_false);
682 }
683 else {
684 dTOPss;
685 SV *targ = PAD_SV(op->op_targ);
686
687 if ((op->op_private & OPpFLIP_LINENUM)
688 ? last_in_gv && SvIV(sv) == IoLINES(GvIOp(last_in_gv))
689 : SvTRUE(sv) ) {
690 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
691 if (op->op_flags & OPf_SPECIAL) {
692 sv_setiv(targ, 1);
693 RETURN;
694 }
695 else {
696 sv_setiv(targ, 0);
697 sp--;
698 RETURNOP(((CONDOP*)cUNOP->op_first)->op_false);
699 }
700 }
701 sv_setpv(TARG, "");
702 SETs(targ);
703 RETURN;
704 }
705}
706
707PP(pp_flop)
708{
709 dSP;
710
711 if (GIMME == G_ARRAY) {
712 dPOPPOPssrl;
713 register I32 i;
714 register SV *sv;
715 I32 max;
716
4633a7c4 717 if (SvNIOKp(left) || !SvPOKp(left) ||
bbce6d69
PP
718 (looks_like_number(left) && *SvPVX(left) != '0') )
719 {
a0d0e21e
LW
720 i = SvIV(left);
721 max = SvIV(right);
bbce6d69
PP
722 if (max >= i) {
723 EXTEND_MORTAL(max - i + 1);
a0d0e21e 724 EXTEND(SP, max - i + 1);
bbce6d69 725 }
a0d0e21e 726 while (i <= max) {
bbce6d69 727 sv = sv_2mortal(newSViv(i++));
a0d0e21e
LW
728 PUSHs(sv);
729 }
730 }
731 else {
732 SV *final = sv_mortalcopy(right);
733 STRLEN len;
734 char *tmps = SvPV(final, len);
735
736 sv = sv_mortalcopy(left);
4633a7c4 737 while (!SvNIOKp(sv) && SvCUR(sv) <= len &&
a0d0e21e
LW
738 strNE(SvPVX(sv),tmps) ) {
739 XPUSHs(sv);
740 sv = sv_2mortal(newSVsv(sv));
741 sv_inc(sv);
742 }
743 if (strEQ(SvPVX(sv),tmps))
744 XPUSHs(sv);
745 }
746 }
747 else {
748 dTOPss;
749 SV *targ = PAD_SV(cUNOP->op_first->op_targ);
750 sv_inc(targ);
751 if ((op->op_private & OPpFLIP_LINENUM)
752 ? last_in_gv && SvIV(sv) == IoLINES(GvIOp(last_in_gv))
753 : SvTRUE(sv) ) {
754 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
755 sv_catpv(targ, "E0");
756 }
757 SETs(targ);
758 }
759
760 RETURN;
761}
762
763/* Control. */
764
765static I32
766dopoptolabel(label)
767char *label;
768{
769 register I32 i;
770 register CONTEXT *cx;
771
772 for (i = cxstack_ix; i >= 0; i--) {
773 cx = &cxstack[i];
774 switch (cx->cx_type) {
775 case CXt_SUBST:
776 if (dowarn)
777 warn("Exiting substitution via %s", op_name[op->op_type]);
778 break;
779 case CXt_SUB:
780 if (dowarn)
781 warn("Exiting subroutine via %s", op_name[op->op_type]);
782 break;
783 case CXt_EVAL:
784 if (dowarn)
785 warn("Exiting eval via %s", op_name[op->op_type]);
786 break;
0a753a76
PP
787 case CXt_NULL:
788 if (dowarn)
789 warn("Exiting pseudo-block via %s", op_name[op->op_type]);
790 return -1;
a0d0e21e
LW
791 case CXt_LOOP:
792 if (!cx->blk_loop.label ||
793 strNE(label, cx->blk_loop.label) ) {
794 DEBUG_l(deb("(Skipping label #%d %s)\n",
795 i, cx->blk_loop.label));
796 continue;
797 }
798 DEBUG_l( deb("(Found label #%d %s)\n", i, label));
799 return i;
800 }
801 }
802 return i;
803}
804
e50aee73
AD
805I32
806dowantarray()
807{
808 I32 cxix;
809
810 cxix = dopoptosub(cxstack_ix);
811 if (cxix < 0)
812 return G_SCALAR;
813
814 if (cxstack[cxix].blk_gimme == G_ARRAY)
815 return G_ARRAY;
816 else
817 return G_SCALAR;
818}
819
a0d0e21e
LW
820static I32
821dopoptosub(startingblock)
822I32 startingblock;
823{
824 I32 i;
825 register CONTEXT *cx;
826 for (i = startingblock; i >= 0; i--) {
827 cx = &cxstack[i];
828 switch (cx->cx_type) {
829 default:
830 continue;
831 case CXt_EVAL:
832 case CXt_SUB:
833 DEBUG_l( deb("(Found sub #%d)\n", i));
834 return i;
835 }
836 }
837 return i;
838}
839
840static I32
841dopoptoeval(startingblock)
842I32 startingblock;
843{
844 I32 i;
845 register CONTEXT *cx;
846 for (i = startingblock; i >= 0; i--) {
847 cx = &cxstack[i];
848 switch (cx->cx_type) {
849 default:
850 continue;
851 case CXt_EVAL:
852 DEBUG_l( deb("(Found eval #%d)\n", i));
853 return i;
854 }
855 }
856 return i;
857}
858
859static I32
860dopoptoloop(startingblock)
861I32 startingblock;
862{
863 I32 i;
864 register CONTEXT *cx;
865 for (i = startingblock; i >= 0; i--) {
866 cx = &cxstack[i];
867 switch (cx->cx_type) {
868 case CXt_SUBST:
869 if (dowarn)
5f05dabc 870 warn("Exiting substitution via %s", op_name[op->op_type]);
a0d0e21e
LW
871 break;
872 case CXt_SUB:
873 if (dowarn)
874 warn("Exiting subroutine via %s", op_name[op->op_type]);
875 break;
876 case CXt_EVAL:
877 if (dowarn)
878 warn("Exiting eval via %s", op_name[op->op_type]);
879 break;
0a753a76
PP
880 case CXt_NULL:
881 if (dowarn)
882 warn("Exiting pseudo-block via %s", op_name[op->op_type]);
883 return -1;
a0d0e21e
LW
884 case CXt_LOOP:
885 DEBUG_l( deb("(Found loop #%d)\n", i));
886 return i;
887 }
888 }
889 return i;
890}
891
892void
893dounwind(cxix)
894I32 cxix;
895{
896 register CONTEXT *cx;
897 SV **newsp;
898 I32 optype;
899
900 while (cxstack_ix > cxix) {
901 cx = &cxstack[cxstack_ix--];
760ac839 902 DEBUG_l(PerlIO_printf(Perl_debug_log, "Unwinding block %ld, type %s\n", (long) cxstack_ix+1,
a0d0e21e
LW
903 block_type[cx->cx_type]));
904 /* Note: we don't need to restore the base context info till the end. */
905 switch (cx->cx_type) {
906 case CXt_SUB:
907 POPSUB(cx);
908 break;
909 case CXt_EVAL:
910 POPEVAL(cx);
911 break;
912 case CXt_LOOP:
913 POPLOOP(cx);
914 break;
0a753a76 915 case CXt_NULL:
a0d0e21e
LW
916 case CXt_SUBST:
917 break;
918 }
919 }
920}
921
a0d0e21e
LW
922OP *
923die_where(message)
924char *message;
925{
926 if (in_eval) {
927 I32 cxix;
928 register CONTEXT *cx;
929 I32 gimme;
930 SV **newsp;
931
4633a7c4
LW
932 if (in_eval & 4) {
933 SV **svp;
934 STRLEN klen = strlen(message);
935
936 svp = hv_fetch(GvHV(errgv), message, klen, TRUE);
937 if (svp) {
938 if (!SvIOK(*svp)) {
939 static char prefix[] = "\t(in cleanup) ";
940 sv_upgrade(*svp, SVt_IV);
941 (void)SvIOK_only(*svp);
942 SvGROW(GvSV(errgv), SvCUR(GvSV(errgv))+sizeof(prefix)+klen);
943 sv_catpvn(GvSV(errgv), prefix, sizeof(prefix)-1);
944 sv_catpvn(GvSV(errgv), message, klen);
945 }
946 sv_inc(*svp);
947 }
948 }
949 else
c07a80fd 950 sv_setpv(GvSV(errgv), message);
4633a7c4 951
a0d0e21e
LW
952 cxix = dopoptoeval(cxstack_ix);
953 if (cxix >= 0) {
954 I32 optype;
955
956 if (cxix < cxstack_ix)
957 dounwind(cxix);
958
959 POPBLOCK(cx,curpm);
960 if (cx->cx_type != CXt_EVAL) {
760ac839 961 PerlIO_printf(PerlIO_stderr(), "panic: die %s", message);
a0d0e21e
LW
962 my_exit(1);
963 }
964 POPEVAL(cx);
965
966 if (gimme == G_SCALAR)
967 *++newsp = &sv_undef;
968 stack_sp = newsp;
969
970 LEAVE;
748a9306 971
a0d0e21e 972 if (optype == OP_REQUIRE)
4633a7c4 973 DIE("%s", SvPVx(GvSV(errgv), na));
a0d0e21e
LW
974 return pop_return();
975 }
976 }
760ac839
LW
977 PerlIO_printf(PerlIO_stderr(), "%s",message);
978 PerlIO_flush(PerlIO_stderr());
f86702cc
PP
979 my_failure_exit();
980 /* NOTREACHED */
a0d0e21e
LW
981 return 0;
982}
983
984PP(pp_xor)
985{
986 dSP; dPOPTOPssrl;
987 if (SvTRUE(left) != SvTRUE(right))
988 RETSETYES;
989 else
990 RETSETNO;
991}
992
993PP(pp_andassign)
994{
995 dSP;
996 if (!SvTRUE(TOPs))
997 RETURN;
998 else
999 RETURNOP(cLOGOP->op_other);
1000}
1001
1002PP(pp_orassign)
1003{
1004 dSP;
1005 if (SvTRUE(TOPs))
1006 RETURN;
1007 else
1008 RETURNOP(cLOGOP->op_other);
1009}
1010
1011#ifdef DEPRECATED
1012PP(pp_entersubr)
1013{
1014 dSP;
1015 SV** mark = (stack_base + *markstack_ptr + 1);
1016 SV* cv = *mark;
1017 while (mark < sp) { /* emulate old interface */
1018 *mark = mark[1];
1019 mark++;
1020 }
1021 *sp = cv;
1022 return pp_entersub();
1023}
1024#endif
1025
1026PP(pp_caller)
1027{
1028 dSP;
1029 register I32 cxix = dopoptosub(cxstack_ix);
1030 register CONTEXT *cx;
1031 I32 dbcxix;
1032 SV *sv;
1033 I32 count = 0;
1034
1035 if (MAXARG)
1036 count = POPi;
1037 EXTEND(SP, 6);
1038 for (;;) {
1039 if (cxix < 0) {
1040 if (GIMME != G_ARRAY)
1041 RETPUSHUNDEF;
1042 RETURN;
1043 }
1044 if (DBsub && cxix >= 0 &&
1045 cxstack[cxix].blk_sub.cv == GvCV(DBsub))
1046 count++;
1047 if (!count--)
1048 break;
1049 cxix = dopoptosub(cxix - 1);
1050 }
1051 cx = &cxstack[cxix];
06a5b730
PP
1052 if (cxstack[cxix].cx_type == CXt_SUB) {
1053 dbcxix = dopoptosub(cxix - 1);
1054 /* We expect that cxstack[dbcxix] is CXt_SUB, anyway, the
1055 field below is defined for any cx. */
1056 if (DBsub && dbcxix >= 0 && cxstack[dbcxix].blk_sub.cv == GvCV(DBsub))
1057 cx = &cxstack[dbcxix];
1058 }
1059
a0d0e21e
LW
1060 if (GIMME != G_ARRAY) {
1061 dTARGET;
1062
1063 sv_setpv(TARG, HvNAME(cx->blk_oldcop->cop_stash));
1064 PUSHs(TARG);
1065 RETURN;
1066 }
a0d0e21e
LW
1067
1068 PUSHs(sv_2mortal(newSVpv(HvNAME(cx->blk_oldcop->cop_stash), 0)));
1069 PUSHs(sv_2mortal(newSVpv(SvPVX(GvSV(cx->blk_oldcop->cop_filegv)), 0)));
1070 PUSHs(sv_2mortal(newSViv((I32)cx->blk_oldcop->cop_line)));
1071 if (!MAXARG)
1072 RETURN;
06a5b730 1073 if (cx->cx_type == CXt_SUB) { /* So is cxstack[dbcxix]. */
a0d0e21e 1074 sv = NEWSV(49, 0);
e5cf08de 1075 gv_efullname3(sv, CvGV(cxstack[cxix].blk_sub.cv), Nullch);
a0d0e21e
LW
1076 PUSHs(sv_2mortal(sv));
1077 PUSHs(sv_2mortal(newSViv((I32)cx->blk_sub.hasargs)));
1078 }
1079 else {
1080 PUSHs(sv_2mortal(newSVpv("(eval)",0)));
1081 PUSHs(sv_2mortal(newSViv(0)));
1082 }
1083 PUSHs(sv_2mortal(newSViv((I32)cx->blk_gimme)));
4633a7c4 1084 if (cx->cx_type == CXt_EVAL) {
06a5b730 1085 if (cx->blk_eval.old_op_type == OP_ENTEREVAL) {
4633a7c4 1086 PUSHs(cx->blk_eval.cur_text);
06a5b730
PP
1087 PUSHs(&sv_no);
1088 }
1089 else if (cx->blk_eval.old_name) { /* Try blocks have old_name == 0. */
1090 /* Require, put the name. */
1091 PUSHs(sv_2mortal(newSVpv(cx->blk_eval.old_name, 0)));
1092 PUSHs(&sv_yes);
1093 }
4633a7c4
LW
1094 }
1095 else if (cx->cx_type == CXt_SUB &&
1096 cx->blk_sub.hasargs &&
1097 curcop->cop_stash == debstash)
1098 {
a0d0e21e
LW
1099 AV *ary = cx->blk_sub.argarray;
1100 int off = AvARRAY(ary) - AvALLOC(ary);
1101
1102 if (!dbargs) {
1103 GV* tmpgv;
1104 dbargs = GvAV(gv_AVadd(tmpgv = gv_fetchpv("DB::args", TRUE,
1105 SVt_PVAV)));
a5f75d66 1106 GvMULTI_on(tmpgv);
a0d0e21e
LW
1107 AvREAL_off(dbargs); /* XXX Should be REIFY */
1108 }
1109
1110 if (AvMAX(dbargs) < AvFILL(ary) + off)
1111 av_extend(dbargs, AvFILL(ary) + off);
1112 Copy(AvALLOC(ary), AvARRAY(dbargs), AvFILL(ary) + 1 + off, SV*);
1113 AvFILL(dbargs) = AvFILL(ary) + off;
1114 }
1115 RETURN;
1116}
1117
1118static int
1119sortcv(a, b)
1120const void *a;
1121const void *b;
1122{
1123 SV **str1 = (SV **) a;
1124 SV **str2 = (SV **) b;
748a9306 1125 I32 oldsaveix = savestack_ix;
a0d0e21e
LW
1126 I32 oldscopeix = scopestack_ix;
1127 I32 result;
1128 GvSV(firstgv) = *str1;
1129 GvSV(secondgv) = *str2;
1130 stack_sp = stack_base;
1131 op = sortcop;
a6c477ed 1132 runops();
a0d0e21e
LW
1133 if (stack_sp != stack_base + 1)
1134 croak("Sort subroutine didn't return single value");
748a9306 1135 if (!SvNIOKp(*stack_sp))
a0d0e21e
LW
1136 croak("Sort subroutine didn't return a numeric value");
1137 result = SvIV(*stack_sp);
1138 while (scopestack_ix > oldscopeix) {
1139 LEAVE;
1140 }
748a9306 1141 leave_scope(oldsaveix);
a0d0e21e
LW
1142 return result;
1143}
1144
1145static int
1146sortcmp(a, b)
1147const void *a;
1148const void *b;
1149{
bbce6d69
PP
1150 return sv_cmp(*(SV **)a, *(SV **)b);
1151}
e5cf08de 1152
bbce6d69
PP
1153static int
1154sortcmp_locale(a, b)
1155const void *a;
1156const void *b;
1157{
1158 return sv_cmp_locale(*(SV **)a, *(SV **)b);
a0d0e21e
LW
1159}
1160
1161PP(pp_reset)
1162{
1163 dSP;
1164 char *tmps;
1165
1166 if (MAXARG < 1)
1167 tmps = "";
1168 else
1169 tmps = POPp;
1170 sv_reset(tmps, curcop->cop_stash);
1171 PUSHs(&sv_yes);
1172 RETURN;
1173}
1174
1175PP(pp_lineseq)
1176{
1177 return NORMAL;
1178}
1179
1180PP(pp_dbstate)
1181{
1182 curcop = (COP*)op;
1183 TAINT_NOT; /* Each statement is presumed innocent */
1184 stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
1185 FREETMPS;
1186
1187 if (op->op_private || SvIV(DBsingle) || SvIV(DBsignal) || SvIV(DBtrace))
1188 {
1189 SV **sp;
1190 register CV *cv;
1191 register CONTEXT *cx;
748a9306 1192 I32 gimme = G_ARRAY;
a0d0e21e
LW
1193 I32 hasargs;
1194 GV *gv;
1195
a0d0e21e
LW
1196 gv = DBgv;
1197 cv = GvCV(gv);
a0d0e21e
LW
1198 if (!cv)
1199 DIE("No DB::DB routine defined");
1200
06a5b730 1201 if (CvDEPTH(cv) >= 1 && !(debug & (1<<30))) /* don't do recursive DB::DB call */
a0d0e21e 1202 return NORMAL;
748a9306 1203
4633a7c4
LW
1204 ENTER;
1205 SAVETMPS;
1206
748a9306 1207 SAVEI32(debug);
55497cff 1208 SAVESTACK_POS();
748a9306
LW
1209 debug = 0;
1210 hasargs = 0;
1211 sp = stack_sp;
1212
a0d0e21e 1213 push_return(op->op_next);
748a9306 1214 PUSHBLOCK(cx, CXt_SUB, sp);
a0d0e21e
LW
1215 PUSHSUB(cx);
1216 CvDEPTH(cv)++;
1217 (void)SvREFCNT_inc(cv);
1218 SAVESPTR(curpad);
1219 curpad = AvARRAY((AV*)*av_fetch(CvPADLIST(cv),1,FALSE));
1220 RETURNOP(CvSTART(cv));
1221 }
1222 else
1223 return NORMAL;
1224}
1225
1226PP(pp_scope)
1227{
1228 return NORMAL;
1229}
1230
1231PP(pp_enteriter)
1232{
1233 dSP; dMARK;
1234 register CONTEXT *cx;
1235 I32 gimme = GIMME;
1236 SV **svp;
1237
4633a7c4
LW
1238 ENTER;
1239 SAVETMPS;
1240
a0d0e21e
LW
1241 if (op->op_targ)
1242 svp = &curpad[op->op_targ]; /* "my" variable */
1243 else
1244 svp = &GvSV((GV*)POPs); /* symbol table variable */
1245
4633a7c4
LW
1246 SAVESPTR(*svp);
1247
a0d0e21e
LW
1248 ENTER;
1249
1250 PUSHBLOCK(cx, CXt_LOOP, SP);
1251 PUSHLOOP(cx, svp, MARK);
44a8e56a
PP
1252 if (op->op_flags & OPf_STACKED)
1253 cx->blk_loop.iterary = (AV*)SvREFCNT_inc(POPs);
4633a7c4 1254 else {
1ce6579f
PP
1255 cx->blk_loop.iterary = curstack;
1256 AvFILL(curstack) = sp - stack_base;
4633a7c4
LW
1257 cx->blk_loop.iterix = MARK - stack_base;
1258 }
a0d0e21e
LW
1259
1260 RETURN;
1261}
1262
1263PP(pp_enterloop)
1264{
1265 dSP;
1266 register CONTEXT *cx;
1267 I32 gimme = GIMME;
1268
1269 ENTER;
1270 SAVETMPS;
1271 ENTER;
1272
1273 PUSHBLOCK(cx, CXt_LOOP, SP);
1274 PUSHLOOP(cx, 0, SP);
1275
1276 RETURN;
1277}
1278
1279PP(pp_leaveloop)
1280{
1281 dSP;
1282 register CONTEXT *cx;
f86702cc 1283 struct block_loop cxloop;
a0d0e21e
LW
1284 I32 gimme;
1285 SV **newsp;
1286 PMOP *newpm;
1287 SV **mark;
1288
1289 POPBLOCK(cx,newpm);
4fdae800 1290 mark = newsp;
f86702cc
PP
1291 POPLOOP1(cx); /* Delay POPLOOP2 until stack values are safe */
1292
a0d0e21e
LW
1293 if (gimme == G_SCALAR) {
1294 if (op->op_private & OPpLEAVE_VOID)
1295 ;
1296 else {
1297 if (mark < SP)
1298 *++newsp = sv_mortalcopy(*SP);
1299 else
1300 *++newsp = &sv_undef;
1301 }
1302 }
1303 else {
1304 while (mark < SP)
1305 *++newsp = sv_mortalcopy(*++mark);
1306 }
f86702cc
PP
1307 SP = newsp;
1308 PUTBACK;
1309
1310 POPLOOP2(); /* Stack values are safe: release loop vars ... */
1311 curpm = newpm; /* ... and pop $1 et al */
1312
a0d0e21e
LW
1313 LEAVE;
1314 LEAVE;
1315
f86702cc 1316 return NORMAL;
a0d0e21e
LW
1317}
1318
1319PP(pp_return)
1320{
1321 dSP; dMARK;
1322 I32 cxix;
1323 register CONTEXT *cx;
f86702cc
PP
1324 struct block_sub cxsub;
1325 bool popsub2 = FALSE;
a0d0e21e
LW
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:
f86702cc
PP
1350 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1351 popsub2 = TRUE;
a0d0e21e
LW
1352 break;
1353 case CXt_EVAL:
1354 POPEVAL(cx);
748a9306
LW
1355 if (optype == OP_REQUIRE &&
1356 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
1357 {
1358 char *name = cx->blk_eval.old_name;
1359 (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
1360 DIE("%s did not return a true value", name);
1361 }
a0d0e21e
LW
1362 break;
1363 default:
1364 DIE("panic: return");
1365 break;
1366 }
1367
1368 if (gimme == G_SCALAR) {
1369 if (MARK < SP)
f86702cc
PP
1370 *++newsp = (popsub2 && SvTEMP(*SP))
1371 ? *SP : sv_mortalcopy(*SP);
a0d0e21e
LW
1372 else
1373 *++newsp = &sv_undef;
a0d0e21e
LW
1374 }
1375 else {
f86702cc
PP
1376 while (++MARK <= SP)
1377 *++newsp = (popsub2 && SvTEMP(*MARK))
1378 ? *MARK : sv_mortalcopy(*MARK);
a0d0e21e 1379 }
a0d0e21e
LW
1380 stack_sp = newsp;
1381
f86702cc
PP
1382 /* Stack values are safe: */
1383 if (popsub2) {
1384 POPSUB2(); /* release CV and @_ ... */
1385 }
1386 curpm = newpm; /* ... and pop $1 et al */
1387
a0d0e21e
LW
1388 LEAVE;
1389 return pop_return();
1390}
1391
1392PP(pp_last)
1393{
1394 dSP;
1395 I32 cxix;
1396 register CONTEXT *cx;
f86702cc
PP
1397 struct block_loop cxloop;
1398 struct block_sub cxsub;
1399 I32 pop2 = 0;
a0d0e21e
LW
1400 I32 gimme;
1401 I32 optype;
1402 OP *nextop;
1403 SV **newsp;
1404 PMOP *newpm;
1405 SV **mark = stack_base + cxstack[cxstack_ix].blk_oldsp;
a0d0e21e
LW
1406
1407 if (op->op_flags & OPf_SPECIAL) {
1408 cxix = dopoptoloop(cxstack_ix);
1409 if (cxix < 0)
1410 DIE("Can't \"last\" outside a block");
1411 }
1412 else {
1413 cxix = dopoptolabel(cPVOP->op_pv);
1414 if (cxix < 0)
1415 DIE("Label not found for \"last %s\"", cPVOP->op_pv);
1416 }
1417 if (cxix < cxstack_ix)
1418 dounwind(cxix);
1419
1420 POPBLOCK(cx,newpm);
1421 switch (cx->cx_type) {
1422 case CXt_LOOP:
f86702cc
PP
1423 POPLOOP1(cx); /* Delay POPLOOP2 until stack values are safe */
1424 pop2 = CXt_LOOP;
4fdae800 1425 nextop = cxloop.last_op->op_next;
a0d0e21e 1426 break;
f86702cc
PP
1427 case CXt_SUB:
1428 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1429 pop2 = CXt_SUB;
a0d0e21e
LW
1430 nextop = pop_return();
1431 break;
f86702cc
PP
1432 case CXt_EVAL:
1433 POPEVAL(cx);
a0d0e21e
LW
1434 nextop = pop_return();
1435 break;
1436 default:
1437 DIE("panic: last");
1438 break;
1439 }
1440
1441 if (gimme == G_SCALAR) {
f86702cc
PP
1442 if (MARK < SP)
1443 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
1444 ? *SP : sv_mortalcopy(*SP);
a0d0e21e
LW
1445 else
1446 *++newsp = &sv_undef;
1447 }
1448 else {
f86702cc
PP
1449 while (++MARK <= SP)
1450 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
1451 ? *MARK : sv_mortalcopy(*MARK);
1452 }
1453 SP = newsp;
1454 PUTBACK;
1455
1456 /* Stack values are safe: */
1457 switch (pop2) {
1458 case CXt_LOOP:
1459 POPLOOP2(); /* release loop vars ... */
4fdae800 1460 LEAVE;
f86702cc
PP
1461 break;
1462 case CXt_SUB:
1463 POPSUB2(); /* release CV and @_ ... */
1464 break;
a0d0e21e 1465 }
f86702cc 1466 curpm = newpm; /* ... and pop $1 et al */
a0d0e21e
LW
1467
1468 LEAVE;
f86702cc 1469 return nextop;
a0d0e21e
LW
1470}
1471
1472PP(pp_next)
1473{
1474 I32 cxix;
1475 register CONTEXT *cx;
1476 I32 oldsave;
1477
1478 if (op->op_flags & OPf_SPECIAL) {
1479 cxix = dopoptoloop(cxstack_ix);
1480 if (cxix < 0)
1481 DIE("Can't \"next\" outside a block");
1482 }
1483 else {
1484 cxix = dopoptolabel(cPVOP->op_pv);
1485 if (cxix < 0)
1486 DIE("Label not found for \"next %s\"", cPVOP->op_pv);
1487 }
1488 if (cxix < cxstack_ix)
1489 dounwind(cxix);
1490
1491 TOPBLOCK(cx);
1492 oldsave = scopestack[scopestack_ix - 1];
1493 LEAVE_SCOPE(oldsave);
1494 return cx->blk_loop.next_op;
1495}
1496
1497PP(pp_redo)
1498{
1499 I32 cxix;
1500 register CONTEXT *cx;
1501 I32 oldsave;
1502
1503 if (op->op_flags & OPf_SPECIAL) {
1504 cxix = dopoptoloop(cxstack_ix);
1505 if (cxix < 0)
1506 DIE("Can't \"redo\" outside a block");
1507 }
1508 else {
1509 cxix = dopoptolabel(cPVOP->op_pv);
1510 if (cxix < 0)
1511 DIE("Label not found for \"redo %s\"", cPVOP->op_pv);
1512 }
1513 if (cxix < cxstack_ix)
1514 dounwind(cxix);
1515
1516 TOPBLOCK(cx);
1517 oldsave = scopestack[scopestack_ix - 1];
1518 LEAVE_SCOPE(oldsave);
1519 return cx->blk_loop.redo_op;
1520}
1521
1522static OP* lastgotoprobe;
1523
1524static OP *
1525dofindlabel(op,label,opstack)
1526OP *op;
1527char *label;
1528OP **opstack;
1529{
1530 OP *kid;
1531 OP **ops = opstack;
1532
1533 if (op->op_type == OP_LEAVE ||
1534 op->op_type == OP_SCOPE ||
1535 op->op_type == OP_LEAVELOOP ||
1536 op->op_type == OP_LEAVETRY)
1537 *ops++ = cUNOP->op_first;
1538 *ops = 0;
1539 if (op->op_flags & OPf_KIDS) {
1540 /* First try all the kids at this level, since that's likeliest. */
1541 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling) {
1542 if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
1543 kCOP->cop_label && strEQ(kCOP->cop_label, label))
1544 return kid;
1545 }
1546 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling) {
1547 if (kid == lastgotoprobe)
1548 continue;
1549 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
1550 if (ops > opstack &&
1551 (ops[-1]->op_type == OP_NEXTSTATE ||
1552 ops[-1]->op_type == OP_DBSTATE))
1553 *ops = kid;
1554 else
1555 *ops++ = kid;
1556 }
1557 if (op = dofindlabel(kid,label,ops))
1558 return op;
1559 }
1560 }
1561 *ops = 0;
1562 return 0;
1563}
1564
1565PP(pp_dump)
1566{
1567 return pp_goto(ARGS);
1568 /*NOTREACHED*/
1569}
1570
1571PP(pp_goto)
1572{
1573 dSP;
1574 OP *retop = 0;
1575 I32 ix;
1576 register CONTEXT *cx;
1577 OP *enterops[64];
1578 char *label;
1579 int do_dump = (op->op_type == OP_DUMP);
1580
1581 label = 0;
1582 if (op->op_flags & OPf_STACKED) {
1583 SV *sv = POPs;
1584
1585 /* This egregious kludge implements goto &subroutine */
1586 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
1587 I32 cxix;
1588 register CONTEXT *cx;
1589 CV* cv = (CV*)SvRV(sv);
1590 SV** mark;
1591 I32 items = 0;
1592 I32 oldsave;
1593
4aa0a1f7
AD
1594 if (!CvROOT(cv) && !CvXSUB(cv)) {
1595 if (CvGV(cv)) {
1596 SV *tmpstr = sv_newmortal();
e5cf08de 1597 gv_efullname3(tmpstr, CvGV(cv), Nullch);
4aa0a1f7
AD
1598 DIE("Goto undefined subroutine &%s",SvPVX(tmpstr));
1599 }
1600 DIE("Goto undefined subroutine");
1601 }
1602
a0d0e21e
LW
1603 /* First do some returnish stuff. */
1604 cxix = dopoptosub(cxstack_ix);
1605 if (cxix < 0)
1606 DIE("Can't goto subroutine outside a subroutine");
1607 if (cxix < cxstack_ix)
1608 dounwind(cxix);
1609 TOPBLOCK(cx);
1610 mark = stack_sp;
1611 if (cx->blk_sub.hasargs) { /* put @_ back onto stack */
1612 AV* av = cx->blk_sub.argarray;
1613
1614 items = AvFILL(av) + 1;
1ce6579f
PP
1615 stack_sp++;
1616 EXTEND(stack_sp, items); /* @_ could have been extended. */
1617 Copy(AvARRAY(av), stack_sp, items, SV*);
a0d0e21e 1618 stack_sp += items;
2c05e328 1619 SvREFCNT_dec(GvAV(defgv));
a0d0e21e 1620 GvAV(defgv) = cx->blk_sub.savearray;
a0d0e21e 1621 AvREAL_off(av);
4633a7c4 1622 av_clear(av);
a0d0e21e
LW
1623 }
1624 if (!(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
1625 SvREFCNT_dec(cx->blk_sub.cv);
1626 oldsave = scopestack[scopestack_ix - 1];
1627 LEAVE_SCOPE(oldsave);
1628
1629 /* Now do some callish stuff. */
1630 SAVETMPS;
1631 if (CvXSUB(cv)) {
1632 if (CvOLDSTYLE(cv)) {
ecfc5424 1633 I32 (*fp3)_((int,int,int));
a0d0e21e
LW
1634 while (sp > mark) {
1635 sp[1] = sp[0];
1636 sp--;
1637 }
ecfc5424
AD
1638 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
1639 items = (*fp3)(CvXSUBANY(cv).any_i32,
1640 mark - stack_base + 1,
1641 items);
a0d0e21e
LW
1642 sp = stack_base + items;
1643 }
1644 else {
1ce6579f 1645 stack_sp--; /* There is no cv arg. */
a0d0e21e
LW
1646 (void)(*CvXSUB(cv))(cv);
1647 }
1648 LEAVE;
1649 return pop_return();
1650 }
1651 else {
1652 AV* padlist = CvPADLIST(cv);
1653 SV** svp = AvARRAY(padlist);
1654 cx->blk_sub.cv = cv;
1655 cx->blk_sub.olddepth = CvDEPTH(cv);
1656 CvDEPTH(cv)++;
1657 if (CvDEPTH(cv) < 2)
1658 (void)SvREFCNT_inc(cv);
1659 else { /* save temporaries on recursion? */
1660 if (CvDEPTH(cv) == 100 && dowarn)
44a8e56a 1661 sub_crush_depth(cv);
a0d0e21e
LW
1662 if (CvDEPTH(cv) > AvFILL(padlist)) {
1663 AV *newpad = newAV();
4aa0a1f7 1664 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
a0d0e21e
LW
1665 I32 ix = AvFILL((AV*)svp[1]);
1666 svp = AvARRAY(svp[0]);
748a9306 1667 for ( ;ix > 0; ix--) {
a0d0e21e 1668 if (svp[ix] != &sv_undef) {
748a9306 1669 char *name = SvPVX(svp[ix]);
5f05dabc
PP
1670 if ((SvFLAGS(svp[ix]) & SVf_FAKE)
1671 || *name == '&')
1672 {
1673 /* outer lexical or anon code */
748a9306 1674 av_store(newpad, ix,
4aa0a1f7 1675 SvREFCNT_inc(oldpad[ix]) );
748a9306
LW
1676 }
1677 else { /* our own lexical */
1678 if (*name == '@')
1679 av_store(newpad, ix, sv = (SV*)newAV());
1680 else if (*name == '%')
1681 av_store(newpad, ix, sv = (SV*)newHV());
1682 else
1683 av_store(newpad, ix, sv = NEWSV(0,0));
1684 SvPADMY_on(sv);
1685 }
a0d0e21e
LW
1686 }
1687 else {
748a9306 1688 av_store(newpad, ix, sv = NEWSV(0,0));
a0d0e21e
LW
1689 SvPADTMP_on(sv);
1690 }
1691 }
1692 if (cx->blk_sub.hasargs) {
1693 AV* av = newAV();
1694 av_extend(av, 0);
1695 av_store(newpad, 0, (SV*)av);
1696 AvFLAGS(av) = AVf_REIFY;
1697 }
1698 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
1699 AvFILL(padlist) = CvDEPTH(cv);
1700 svp = AvARRAY(padlist);
1701 }
1702 }
1703 SAVESPTR(curpad);
1704 curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
1705 if (cx->blk_sub.hasargs) {
1706 AV* av = (AV*)curpad[0];
1707 SV** ary;
1708
1709 cx->blk_sub.savearray = GvAV(defgv);
1710 cx->blk_sub.argarray = av;
2c05e328 1711 GvAV(defgv) = (AV*)SvREFCNT_inc(av);
a0d0e21e
LW
1712 ++mark;
1713
1714 if (items >= AvMAX(av) + 1) {
1715 ary = AvALLOC(av);
1716 if (AvARRAY(av) != ary) {
1717 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
1718 SvPVX(av) = (char*)ary;
1719 }
1720 if (items >= AvMAX(av) + 1) {
1721 AvMAX(av) = items - 1;
1722 Renew(ary,items+1,SV*);
1723 AvALLOC(av) = ary;
1724 SvPVX(av) = (char*)ary;
1725 }
1726 }
1727 Copy(mark,AvARRAY(av),items,SV*);
1728 AvFILL(av) = items - 1;
1729
1730 while (items--) {
1731 if (*mark)
1732 SvTEMP_off(*mark);
1733 mark++;
1734 }
1735 }
f967eb5f 1736 if (perldb && curstash != debstash) {
44a8e56a
PP
1737 /*
1738 * We do not care about using sv to call CV;
1739 * it's for informational purposes only.
1740 */
1ce6579f
PP
1741 SV *sv = GvSV(DBsub);
1742 save_item(sv);
e5cf08de 1743 gv_efullname3(sv, CvGV(cv), Nullch);
1ce6579f 1744 }
a0d0e21e
LW
1745 RETURNOP(CvSTART(cv));
1746 }
1747 }
1748 else
1749 label = SvPV(sv,na);
1750 }
1751 else if (op->op_flags & OPf_SPECIAL) {
1752 if (! do_dump)
1753 DIE("goto must have label");
1754 }
1755 else
1756 label = cPVOP->op_pv;
1757
1758 if (label && *label) {
1759 OP *gotoprobe = 0;
1760
1761 /* find label */
1762
1763 lastgotoprobe = 0;
1764 *enterops = 0;
1765 for (ix = cxstack_ix; ix >= 0; ix--) {
1766 cx = &cxstack[ix];
1767 switch (cx->cx_type) {
1768 case CXt_SUB:
1769 gotoprobe = CvROOT(cx->blk_sub.cv);
1770 break;
1771 case CXt_EVAL:
1772 gotoprobe = eval_root; /* XXX not good for nested eval */
1773 break;
1774 case CXt_LOOP:
1775 gotoprobe = cx->blk_oldcop->op_sibling;
1776 break;
1777 case CXt_SUBST:
1778 continue;
1779 case CXt_BLOCK:
1780 if (ix)
1781 gotoprobe = cx->blk_oldcop->op_sibling;
1782 else
1783 gotoprobe = main_root;
1784 break;
0a753a76
PP
1785 case CXt_NULL:
1786 DIE("Can't \"goto\" outside a block");
1787 break;
a0d0e21e
LW
1788 default:
1789 if (ix)
1790 DIE("panic: goto");
1791 else
1792 gotoprobe = main_root;
1793 break;
1794 }
1795 retop = dofindlabel(gotoprobe, label, enterops);
1796 if (retop)
1797 break;
1798 lastgotoprobe = gotoprobe;
1799 }
1800 if (!retop)
1801 DIE("Can't find label %s", label);
1802
1803 /* pop unwanted frames */
1804
1805 if (ix < cxstack_ix) {
1806 I32 oldsave;
1807
1808 if (ix < 0)
1809 ix = 0;
1810 dounwind(ix);
1811 TOPBLOCK(cx);
1812 oldsave = scopestack[scopestack_ix];
1813 LEAVE_SCOPE(oldsave);
1814 }
1815
1816 /* push wanted frames */
1817
748a9306 1818 if (*enterops && enterops[1]) {
a0d0e21e 1819 OP *oldop = op;
748a9306 1820 for (ix = 1; enterops[ix]; ix++) {
a0d0e21e
LW
1821 op = enterops[ix];
1822 (*op->op_ppaddr)();
1823 }
1824 op = oldop;
1825 }
1826 }
1827
1828 if (do_dump) {
a5f75d66
AD
1829#ifdef VMS
1830 if (!retop) retop = main_start;
1831#endif
a0d0e21e
LW
1832 restartop = retop;
1833 do_undump = TRUE;
1834
1835 my_unexec();
1836
1837 restartop = 0; /* hmm, must be GNU unexec().. */
1838 do_undump = FALSE;
1839 }
1840
1ce6579f 1841 if (curstack == signalstack) {
748a9306 1842 restartop = retop;
a5f75d66 1843 Siglongjmp(top_env, 3);
748a9306
LW
1844 }
1845
a0d0e21e
LW
1846 RETURNOP(retop);
1847}
1848
1849PP(pp_exit)
1850{
1851 dSP;
1852 I32 anum;
1853
1854 if (MAXARG < 1)
1855 anum = 0;
ff0cee69 1856 else {
a0d0e21e 1857 anum = SvIVx(POPs);
ff0cee69
PP
1858#ifdef VMSISH_EXIT
1859 if (anum == 1 && VMSISH_EXIT)
1860 anum = 0;
1861#endif
1862 }
a0d0e21e
LW
1863 my_exit(anum);
1864 PUSHs(&sv_undef);
1865 RETURN;
1866}
1867
1868#ifdef NOTYET
1869PP(pp_nswitch)
1870{
1871 dSP;
1872 double value = SvNVx(GvSV(cCOP->cop_gv));
1873 register I32 match = I_32(value);
1874
1875 if (value < 0.0) {
1876 if (((double)match) > value)
1877 --match; /* was fractional--truncate other way */
1878 }
1879 match -= cCOP->uop.scop.scop_offset;
1880 if (match < 0)
1881 match = 0;
1882 else if (match > cCOP->uop.scop.scop_max)
1883 match = cCOP->uop.scop.scop_max;
1884 op = cCOP->uop.scop.scop_next[match];
1885 RETURNOP(op);
1886}
1887
1888PP(pp_cswitch)
1889{
1890 dSP;
1891 register I32 match;
1892
1893 if (multiline)
1894 op = op->op_next; /* can't assume anything */
1895 else {
1896 match = *(SvPVx(GvSV(cCOP->cop_gv), na)) & 255;
1897 match -= cCOP->uop.scop.scop_offset;
1898 if (match < 0)
1899 match = 0;
1900 else if (match > cCOP->uop.scop.scop_max)
1901 match = cCOP->uop.scop.scop_max;
1902 op = cCOP->uop.scop.scop_next[match];
1903 }
1904 RETURNOP(op);
1905}
1906#endif
1907
1908/* Eval. */
1909
1910static void
1911save_lines(array, sv)
1912AV *array;
1913SV *sv;
1914{
1915 register char *s = SvPVX(sv);
1916 register char *send = SvPVX(sv) + SvCUR(sv);
1917 register char *t;
1918 register I32 line = 1;
1919
1920 while (s && s < send) {
1921 SV *tmpstr = NEWSV(85,0);
1922
1923 sv_upgrade(tmpstr, SVt_PVMG);
1924 t = strchr(s, '\n');
1925 if (t)
1926 t++;
1927 else
1928 t = send;
1929
1930 sv_setpvn(tmpstr, s, t - s);
1931 av_store(array, line++, tmpstr);
1932 s = t;
1933 }
1934}
1935
1936static OP *
1937doeval(gimme)
1938int gimme;
1939{
1940 dSP;
1941 OP *saveop = op;
1942 HV *newstash;
ff3ff8d1 1943 CV *caller;
748a9306 1944 AV* comppadlist;
a0d0e21e
LW
1945
1946 in_eval = 1;
1947
1ce6579f
PP
1948 PUSHMARK(SP);
1949
a0d0e21e
LW
1950 /* set up a scratch pad */
1951
55497cff 1952 SAVEI32(padix);
a0d0e21e
LW
1953 SAVESPTR(curpad);
1954 SAVESPTR(comppad);
1955 SAVESPTR(comppad_name);
55497cff
PP
1956 SAVEI32(comppad_name_fill);
1957 SAVEI32(min_intro_pending);
1958 SAVEI32(max_intro_pending);
748a9306 1959
ff3ff8d1 1960 caller = compcv;
748a9306
LW
1961 SAVESPTR(compcv);
1962 compcv = (CV*)NEWSV(1104,0);
1963 sv_upgrade((SV *)compcv, SVt_PVCV);
07055b4c 1964 CvUNIQUE_on(compcv);
748a9306 1965
a0d0e21e
LW
1966 comppad = newAV();
1967 comppad_name = newAV();
1968 comppad_name_fill = 0;
1969 min_intro_pending = 0;
1970 av_push(comppad, Nullsv);
1971 curpad = AvARRAY(comppad);
1972 padix = 0;
1973
748a9306
LW
1974 comppadlist = newAV();
1975 AvREAL_off(comppadlist);
8e07c86e
AD
1976 av_store(comppadlist, 0, (SV*)comppad_name);
1977 av_store(comppadlist, 1, (SV*)comppad);
748a9306 1978 CvPADLIST(compcv) = comppadlist;
2c05e328
CS
1979
1980 if (saveop->op_type != OP_REQUIRE)
1981 CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc(caller);
07055b4c 1982
8e07c86e 1983 SAVEFREESV(compcv);
748a9306 1984
a0d0e21e
LW
1985 /* make sure we compile in the right package */
1986
1987 newstash = curcop->cop_stash;
1988 if (curstash != newstash) {
1989 SAVESPTR(curstash);
1990 curstash = newstash;
1991 }
1992 SAVESPTR(beginav);
1993 beginav = newAV();
1994 SAVEFREESV(beginav);
1995
1996 /* try to compile it */
1997
1998 eval_root = Nullop;
1999 error_count = 0;
2000 curcop = &compiling;
2001 curcop->cop_arybase = 0;
c07a80fd
PP
2002 SvREFCNT_dec(rs);
2003 rs = newSVpv("\n", 1);
1ce6579f
PP
2004 if (saveop->op_flags & OPf_SPECIAL)
2005 in_eval |= 4;
2006 else
2007 sv_setpv(GvSV(errgv),"");
a0d0e21e
LW
2008 if (yyparse() || error_count || !eval_root) {
2009 SV **newsp;
2010 I32 gimme;
2011 CONTEXT *cx;
2012 I32 optype;
2013
2014 op = saveop;
2015 if (eval_root) {
2016 op_free(eval_root);
2017 eval_root = Nullop;
2018 }
1ce6579f 2019 SP = stack_base + POPMARK; /* pop original mark */
a0d0e21e
LW
2020 POPBLOCK(cx,curpm);
2021 POPEVAL(cx);
2022 pop_return();
2023 lex_end();
2024 LEAVE;
2025 if (optype == OP_REQUIRE)
4633a7c4 2026 DIE("%s", SvPVx(GvSV(errgv), na));
c07a80fd
PP
2027 SvREFCNT_dec(rs);
2028 rs = SvREFCNT_inc(nrs);
a0d0e21e
LW
2029 RETPUSHUNDEF;
2030 }
c07a80fd
PP
2031 SvREFCNT_dec(rs);
2032 rs = SvREFCNT_inc(nrs);
a0d0e21e 2033 compiling.cop_line = 0;
a0d0e21e
LW
2034 SAVEFREEOP(eval_root);
2035 if (gimme & G_ARRAY)
2036 list(eval_root);
2037 else
2038 scalar(eval_root);
2039
2040 DEBUG_x(dump_eval());
2041
55497cff 2042 /* Register with debugger: */
55497cff
PP
2043 if (perldb && saveop->op_type == OP_REQUIRE) {
2044 CV *cv = perl_get_cv("DB::postponed", FALSE);
55497cff
PP
2045 if (cv) {
2046 dSP;
2047 PUSHMARK(sp);
2048 XPUSHs((SV*)compiling.cop_filegv);
2049 PUTBACK;
2050 perl_call_sv((SV*)cv, G_DISCARD);
2051 }
2052 }
2053
a0d0e21e
LW
2054 /* compiled okay, so do it */
2055
4fdae800
PP
2056 CvDEPTH(compcv) = 1;
2057
1ce6579f 2058 SP = stack_base + POPMARK; /* pop original mark */
a0d0e21e
LW
2059 RETURNOP(eval_start);
2060}
2061
2062PP(pp_require)
2063{
2064 dSP;
2065 register CONTEXT *cx;
2066 SV *sv;
2067 char *name;
2068 char *tmpname;
2069 SV** svp;
2070 I32 gimme = G_SCALAR;
760ac839 2071 PerlIO *tryrsfp = 0;
a0d0e21e
LW
2072
2073 sv = POPs;
4633a7c4 2074 if (SvNIOKp(sv) && !SvPOKp(sv)) {
36477c24 2075 SET_NUMERIC_STANDARD();
a5f75d66
AD
2076 if (atof(patchlevel) + 0.00000999 < SvNV(sv))
2077 DIE("Perl %s required--this is only version %s, stopped",
2078 SvPV(sv,na),patchlevel);
a0d0e21e
LW
2079 RETPUSHYES;
2080 }
2081 name = SvPV(sv, na);
2082 if (!*name)
2083 DIE("Null filename used");
4633a7c4 2084 TAINT_PROPER("require");
a0d0e21e
LW
2085 if (op->op_type == OP_REQUIRE &&
2086 (svp = hv_fetch(GvHVn(incgv), name, SvCUR(sv), 0)) &&
2087 *svp != &sv_undef)
2088 RETPUSHYES;
2089
2090 /* prepare to compile file */
2091
2092 tmpname = savepv(name);
2093 if (*tmpname == '/' ||
2094 (*tmpname == '.' &&
2095 (tmpname[1] == '/' ||
748a9306 2096 (tmpname[1] == '.' && tmpname[2] == '/')))
4633a7c4
LW
2097#ifdef DOSISH
2098 || (tmpname[0] && tmpname[1] == ':')
2099#endif
748a9306 2100#ifdef VMS
bbce6d69
PP
2101 || (strchr(tmpname,':') || ((*tmpname == '[' || *tmpname == '<') &&
2102 (isALNUM(tmpname[1]) || strchr("$-_]>",tmpname[1]))))
748a9306
LW
2103#endif
2104 )
a0d0e21e 2105 {
760ac839 2106 tryrsfp = PerlIO_open(tmpname,"r");
a0d0e21e
LW
2107 }
2108 else {
2109 AV *ar = GvAVn(incgv);
2110 I32 i;
748a9306 2111#ifdef VMS
bbce6d69
PP
2112 char unixified[256];
2113 if (tounixspec_ts(tmpname,unixified) != NULL)
2114 for (i = 0; i <= AvFILL(ar); i++) {
748a9306 2115 if (tounixpath_ts(SvPVx(*av_fetch(ar, i, TRUE), na),buf) == NULL)
4633a7c4 2116 continue;
bbce6d69 2117 strcat(buf,unixified);
748a9306 2118#else
bbce6d69 2119 for (i = 0; i <= AvFILL(ar); i++) {
a0d0e21e
LW
2120 (void)sprintf(buf, "%s/%s",
2121 SvPVx(*av_fetch(ar, i, TRUE), na), name);
748a9306 2122#endif
760ac839 2123 tryrsfp = PerlIO_open(buf, "r");
a0d0e21e
LW
2124 if (tryrsfp) {
2125 char *s = buf;
2126
2127 if (*s == '.' && s[1] == '/')
2128 s += 2;
2129 Safefree(tmpname);
2130 tmpname = savepv(s);
2131 break;
2132 }
2133 }
2134 }
2135 SAVESPTR(compiling.cop_filegv);
2136 compiling.cop_filegv = gv_fetchfile(tmpname);
2137 Safefree(tmpname);
2138 tmpname = Nullch;
2139 if (!tryrsfp) {
2140 if (op->op_type == OP_REQUIRE) {
2141 sprintf(tokenbuf,"Can't locate %s in @INC", name);
2142 if (instr(tokenbuf,".h "))
2143 strcat(tokenbuf," (change .h to .ph maybe?)");
2144 if (instr(tokenbuf,".ph "))
2145 strcat(tokenbuf," (did you run h2ph?)");
2146 DIE("%s",tokenbuf);
2147 }
2148
2149 RETPUSHUNDEF;
2150 }
2151
2152 /* Assume success here to prevent recursive requirement. */
2153 (void)hv_store(GvHVn(incgv), name, strlen(name),
2154 newSVsv(GvSV(compiling.cop_filegv)), 0 );
2155
2156 ENTER;
2157 SAVETMPS;
2158 lex_start(sv_2mortal(newSVpv("",0)));
e50aee73
AD
2159 if (rsfp_filters){
2160 save_aptr(&rsfp_filters);
2161 rsfp_filters = NULL;
2162 }
2163
a0d0e21e
LW
2164 rsfp = tryrsfp;
2165 name = savepv(name);
2166 SAVEFREEPV(name);
2167 SAVEI32(hints);
2168 hints = 0;
2169
2170 /* switch to eval mode */
2171
2172 push_return(op->op_next);
2173 PUSHBLOCK(cx, CXt_EVAL, SP);
2174 PUSHEVAL(cx, name, compiling.cop_filegv);
2175
2176 compiling.cop_line = 0;
2177
2178 PUTBACK;
2179 return doeval(G_SCALAR);
2180}
2181
2182PP(pp_dofile)
2183{
2184 return pp_require(ARGS);
2185}
2186
2187PP(pp_entereval)
2188{
2189 dSP;
2190 register CONTEXT *cx;
2191 dPOPss;
55497cff
PP
2192 I32 gimme = GIMME, was = sub_generation;
2193 char tmpbuf[32], *safestr;
a0d0e21e 2194 STRLEN len;
55497cff 2195 OP *ret;
a0d0e21e
LW
2196
2197 if (!SvPV(sv,len) || !len)
2198 RETPUSHUNDEF;
748a9306 2199 TAINT_PROPER("eval");
a0d0e21e
LW
2200
2201 ENTER;
a0d0e21e 2202 lex_start(sv);
748a9306 2203 SAVETMPS;
a0d0e21e
LW
2204
2205 /* switch to eval mode */
2206
748a9306 2207 SAVESPTR(compiling.cop_filegv);
ff0cee69 2208 sprintf(tmpbuf, "_<(eval %lu)", (unsigned long)++evalseq);
a0d0e21e
LW
2209 compiling.cop_filegv = gv_fetchfile(tmpbuf+2);
2210 compiling.cop_line = 1;
55497cff
PP
2211 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
2212 deleting the eval's FILEGV from the stash before gv_check() runs
2213 (i.e. before run-time proper). To work around the coredump that
2214 ensues, we always turn GvMULTI_on for any globals that were
2215 introduced within evals. See force_ident(). GSAR 96-10-12 */
2216 safestr = savepv(tmpbuf);
2217 SAVEDELETE(defstash, safestr, strlen(safestr));
a0d0e21e
LW
2218 SAVEI32(hints);
2219 hints = op->op_targ;
2220
2221 push_return(op->op_next);
2222 PUSHBLOCK(cx, CXt_EVAL, SP);
2223 PUSHEVAL(cx, 0, compiling.cop_filegv);
2224
2225 /* prepare to compile string */
2226
2227 if (perldb && curstash != debstash)
2228 save_lines(GvAV(compiling.cop_filegv), linestr);
2229 PUTBACK;
55497cff
PP
2230 ret = doeval(gimme);
2231 if (perldb && was != sub_generation) { /* Some subs defined here. */
2232 strcpy(safestr, "_<(eval )"); /* Anything fake and short. */
2233 }
2234 return ret;
a0d0e21e
LW
2235}
2236
2237PP(pp_leaveeval)
2238{
2239 dSP;
2240 register SV **mark;
2241 SV **newsp;
2242 PMOP *newpm;
2243 I32 gimme;
2244 register CONTEXT *cx;
2245 OP *retop;
760ac839 2246 U8 save_flags = op -> op_flags;
a0d0e21e
LW
2247 I32 optype;
2248
2249 POPBLOCK(cx,newpm);
2250 POPEVAL(cx);
2251 retop = pop_return();
2252
2253 if (gimme == G_SCALAR) {
2254 if (op->op_private & OPpLEAVE_VOID)
2255 MARK = newsp;
2256 else {
2257 MARK = newsp + 1;
2258 if (MARK <= SP) {
2259 if (SvFLAGS(TOPs) & SVs_TEMP)
2260 *MARK = TOPs;
2261 else
2262 *MARK = sv_mortalcopy(TOPs);
2263 }
2264 else {
2265 MEXTEND(mark,0);
2266 *MARK = &sv_undef;
2267 }
2268 }
2269 SP = MARK;
2270 }
2271 else {
2272 for (mark = newsp + 1; mark <= SP; mark++)
760ac839 2273 if (!(SvFLAGS(*mark) & SVs_TEMP))
a0d0e21e
LW
2274 *mark = sv_mortalcopy(*mark);
2275 /* in case LEAVE wipes old return values */
2276 }
2277 curpm = newpm; /* Don't pop $1 et al till now */
2278
4fdae800
PP
2279#ifdef DEBUGGING
2280 assert(CvDEPTH(compcv) == 1);
2281#endif
2282 CvDEPTH(compcv) = 0;
2283
1ce6579f
PP
2284 if (optype == OP_REQUIRE &&
2285 !(gimme == G_SCALAR ? SvTRUE(*sp) : sp > newsp)) {
a0d0e21e
LW
2286 char *name = cx->blk_eval.old_name;
2287
1ce6579f
PP
2288 /* Unassume the success we assumed earlier. */
2289 (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
2290 retop = die("%s did not return a true value", name);
a0d0e21e
LW
2291 }
2292
2293 lex_end();
2294 LEAVE;
4fdae800 2295
760ac839 2296 if (!(save_flags & OPf_SPECIAL))
1ce6579f 2297 sv_setpv(GvSV(errgv),"");
a0d0e21e
LW
2298
2299 RETURNOP(retop);
2300}
2301
a0d0e21e
LW
2302PP(pp_entertry)
2303{
2304 dSP;
2305 register CONTEXT *cx;
2306 I32 gimme = GIMME;
2307
2308 ENTER;
2309 SAVETMPS;
2310
2311 push_return(cLOGOP->op_other->op_next);
2312 PUSHBLOCK(cx, CXt_EVAL, SP);
2313 PUSHEVAL(cx, 0, 0);
2314 eval_root = op; /* Only needed so that goto works right. */
2315
2316 in_eval = 1;
4633a7c4 2317 sv_setpv(GvSV(errgv),"");
a0d0e21e
LW
2318 RETURN;
2319}
2320
2321PP(pp_leavetry)
2322{
2323 dSP;
2324 register SV **mark;
2325 SV **newsp;
2326 PMOP *newpm;
2327 I32 gimme;
2328 register CONTEXT *cx;
2329 I32 optype;
2330
2331 POPBLOCK(cx,newpm);
2332 POPEVAL(cx);
2333 pop_return();
2334
2335 if (gimme == G_SCALAR) {
2336 if (op->op_private & OPpLEAVE_VOID)
2337 MARK = newsp;
2338 else {
2339 MARK = newsp + 1;
2340 if (MARK <= SP) {
2341 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
2342 *MARK = TOPs;
2343 else
2344 *MARK = sv_mortalcopy(TOPs);
2345 }
2346 else {
2347 MEXTEND(mark,0);
2348 *MARK = &sv_undef;
2349 }
2350 }
2351 SP = MARK;
2352 }
2353 else {
2354 for (mark = newsp + 1; mark <= SP; mark++)
760ac839 2355 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP)))
a0d0e21e
LW
2356 *mark = sv_mortalcopy(*mark);
2357 /* in case LEAVE wipes old return values */
2358 }
2359 curpm = newpm; /* Don't pop $1 et al till now */
2360
2361 LEAVE;
4633a7c4 2362 sv_setpv(GvSV(errgv),"");
a0d0e21e
LW
2363 RETURN;
2364}
2365
2366static void
2367doparseform(sv)
2368SV *sv;
2369{
2370 STRLEN len;
2371 register char *s = SvPV_force(sv, len);
2372 register char *send = s + len;
2373 register char *base;
2374 register I32 skipspaces = 0;
2375 bool noblank;
2376 bool repeat;
2377 bool postspace = FALSE;
2378 U16 *fops;
2379 register U16 *fpc;
2380 U16 *linepc;
2381 register I32 arg;
2382 bool ischop;
2383
55497cff 2384 if (len == 0)
bbce6d69 2385 croak("Null picture in formline");
55497cff
PP
2386
2387 New(804, fops, (send - s)*3+10, U16); /* Almost certainly too long... */
a0d0e21e
LW
2388 fpc = fops;
2389
2390 if (s < send) {
2391 linepc = fpc;
2392 *fpc++ = FF_LINEMARK;
2393 noblank = repeat = FALSE;
2394 base = s;
2395 }
2396
2397 while (s <= send) {
2398 switch (*s++) {
2399 default:
2400 skipspaces = 0;
2401 continue;
2402
2403 case '~':
2404 if (*s == '~') {
2405 repeat = TRUE;
2406 *s = ' ';
2407 }
2408 noblank = TRUE;
2409 s[-1] = ' ';
2410 /* FALL THROUGH */
2411 case ' ': case '\t':
2412 skipspaces++;
2413 continue;
2414
2415 case '\n': case 0:
2416 arg = s - base;
2417 skipspaces++;
2418 arg -= skipspaces;
2419 if (arg) {
5f05dabc 2420 if (postspace)
a0d0e21e 2421 *fpc++ = FF_SPACE;
a0d0e21e
LW
2422 *fpc++ = FF_LITERAL;
2423 *fpc++ = arg;
2424 }
5f05dabc 2425 postspace = FALSE;
a0d0e21e
LW
2426 if (s <= send)
2427 skipspaces--;
2428 if (skipspaces) {
2429 *fpc++ = FF_SKIP;
2430 *fpc++ = skipspaces;
2431 }
2432 skipspaces = 0;
2433 if (s <= send)
2434 *fpc++ = FF_NEWLINE;
2435 if (noblank) {
2436 *fpc++ = FF_BLANK;
2437 if (repeat)
2438 arg = fpc - linepc + 1;
2439 else
2440 arg = 0;
2441 *fpc++ = arg;
2442 }
2443 if (s < send) {
2444 linepc = fpc;
2445 *fpc++ = FF_LINEMARK;
2446 noblank = repeat = FALSE;
2447 base = s;
2448 }
2449 else
2450 s++;
2451 continue;
2452
2453 case '@':
2454 case '^':
2455 ischop = s[-1] == '^';
2456
2457 if (postspace) {
2458 *fpc++ = FF_SPACE;
2459 postspace = FALSE;
2460 }
2461 arg = (s - base) - 1;
2462 if (arg) {
2463 *fpc++ = FF_LITERAL;
2464 *fpc++ = arg;
2465 }
2466
2467 base = s - 1;
2468 *fpc++ = FF_FETCH;
2469 if (*s == '*') {
2470 s++;
2471 *fpc++ = 0;
2472 *fpc++ = FF_LINEGLOB;
2473 }
2474 else if (*s == '#' || (*s == '.' && s[1] == '#')) {
2475 arg = ischop ? 512 : 0;
2476 base = s - 1;
2477 while (*s == '#')
2478 s++;
2479 if (*s == '.') {
2480 char *f;
2481 s++;
2482 f = s;
2483 while (*s == '#')
2484 s++;
2485 arg |= 256 + (s - f);
2486 }
2487 *fpc++ = s - base; /* fieldsize for FETCH */
2488 *fpc++ = FF_DECIMAL;
2489 *fpc++ = arg;
2490 }
2491 else {
2492 I32 prespace = 0;
2493 bool ismore = FALSE;
2494
2495 if (*s == '>') {
2496 while (*++s == '>') ;
2497 prespace = FF_SPACE;
2498 }
2499 else if (*s == '|') {
2500 while (*++s == '|') ;
2501 prespace = FF_HALFSPACE;
2502 postspace = TRUE;
2503 }
2504 else {
2505 if (*s == '<')
2506 while (*++s == '<') ;
2507 postspace = TRUE;
2508 }
2509 if (*s == '.' && s[1] == '.' && s[2] == '.') {
2510 s += 3;
2511 ismore = TRUE;
2512 }
2513 *fpc++ = s - base; /* fieldsize for FETCH */
2514
2515 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
2516
2517 if (prespace)
2518 *fpc++ = prespace;
2519 *fpc++ = FF_ITEM;
2520 if (ismore)
2521 *fpc++ = FF_MORE;
2522 if (ischop)
2523 *fpc++ = FF_CHOP;
2524 }
2525 base = s;
2526 skipspaces = 0;
2527 continue;
2528 }
2529 }
2530 *fpc++ = FF_END;
2531
2532 arg = fpc - fops;
2533 { /* need to jump to the next word */
2534 int z;
2535 z = WORD_ALIGN - SvCUR(sv) % WORD_ALIGN;
2536 SvGROW(sv, SvCUR(sv) + z + arg * sizeof(U16) + 4);
2537 s = SvPVX(sv) + SvCUR(sv) + z;
2538 }
2539 Copy(fops, s, arg, U16);
2540 Safefree(fops);
55497cff 2541 sv_magic(sv, Nullsv, 'f', Nullch, 0);
a0d0e21e
LW
2542 SvCOMPILED_on(sv);
2543}