Remove redundant functions UNIVERSAL::{class,is_instance}
[p5sagit/p5-mst-13.2.git] / pp_ctl.c
CommitLineData
a0d0e21e 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 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 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 80 pm->op_pmflags = pm->op_pmpermflags; /* reset case sensitivity */
81 pm->op_pmregexp = pregcomp(t, t + len, pm);
82 }
a0d0e21e 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 90 pm->op_pmflags &= ~PMf_RUNTIME; /* no point compiling again */
91 hoistmust(pm);
92 cLOGOP->op_first->op_next = op->op_next;
a0d0e21e 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 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 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 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 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 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 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 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 179 double value;
180 bool gotsome;
181 STRLEN len;
182
55497cff 183 if (!SvMAGICAL(form) || !SvCOMPILED(form)) {
a0d0e21e 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 224 } )
225 switch (*fpc++) {
226 case FF_LINEMARK:
227 linemark = t;
a0d0e21e 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 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 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 615 SvTEMP_off(*up);
616 if (!sortcop && !SvPOK(*up))
a0d0e21e 617 (void)sv_2pv(*up, &na);
a0d0e21e 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 632 if (!sortstack) {
633 sortstack = newAV();
634 AvREAL_off(sortstack);
635 av_extend(sortstack, 32);
636 }
1ce6579f 637 SWITCHSTACK(curstack, sortstack);
a0d0e21e 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 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 659 qsort((char*)(ORIGMARK+1), max, sizeof(SV*),
660 (op->op_private & OPpLOCALE) ? sortcmp_locale : sortcmp);
a0d0e21e 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 718 (looks_like_number(left) && *SvPVX(left) != '0') )
719 {
a0d0e21e 720 i = SvIV(left);
721 max = SvIV(right);
bbce6d69 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 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 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 787 case CXt_NULL:
788 if (dowarn)
789 warn("Exiting pseudo-block via %s", op_name[op->op_type]);
790 return -1;
a0d0e21e 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 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 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 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 880 case CXt_NULL:
881 if (dowarn)
882 warn("Exiting pseudo-block via %s", op_name[op->op_type]);
883 return -1;
a0d0e21e 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 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 916 case CXt_SUBST:
917 break;
918 }
919 }
920}
921
a0d0e21e 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 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 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 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 974 return pop_return();
975 }
976 }
760ac839 977 PerlIO_printf(PerlIO_stderr(), "%s",message);
978 PerlIO_flush(PerlIO_stderr());
f86702cc 979 my_failure_exit();
980 /* NOTREACHED */
a0d0e21e 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 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 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 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 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 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 1094 }
1095 else if (cx->cx_type == CXt_SUB &&
1096 cx->blk_sub.hasargs &&
1097 curcop->cop_stash == debstash)
1098 {
a0d0e21e 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 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 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 1133 if (stack_sp != stack_base + 1)
1134 croak("Sort subroutine didn't return single value");
748a9306 1135 if (!SvNIOKp(*stack_sp))
a0d0e21e 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 1142 return result;
1143}
1144
1145static int
1146sortcmp(a, b)
1147const void *a;
1148const void *b;
1149{
bbce6d69 1150 return sv_cmp(*(SV **)a, *(SV **)b);
1151}
e5cf08de 1152
bbce6d69 1153static int
1154sortcmp_locale(a, b)
1155const void *a;
1156const void *b;
1157{
1158 return sv_cmp_locale(*(SV **)a, *(SV **)b);
a0d0e21e 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 1193 I32 hasargs;
1194 GV *gv;
1195
a0d0e21e 1196 gv = DBgv;
1197 cv = GvCV(gv);
a0d0e21e 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 1204 ENTER;
1205 SAVETMPS;
1206
748a9306 1207 SAVEI32(debug);
55497cff 1208 SAVESTACK_POS();
748a9306 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 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 1238 ENTER;
1239 SAVETMPS;
1240
a0d0e21e 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 1246 SAVESPTR(*svp);
1247
a0d0e21e 1248 ENTER;
1249
1250 PUSHBLOCK(cx, CXt_LOOP, SP);
1251 PUSHLOOP(cx, svp, MARK);
44a8e56a 1252 if (op->op_flags & OPf_STACKED)
1253 cx->blk_loop.iterary = (AV*)SvREFCNT_inc(POPs);
4633a7c4 1254 else {
1ce6579f 1255 cx->blk_loop.iterary = curstack;
1256 AvFILL(curstack) = sp - stack_base;
4633a7c4 1257 cx->blk_loop.iterix = MARK - stack_base;
1258 }
a0d0e21e 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 1284 I32 gimme;
1285 SV **newsp;
1286 PMOP *newpm;
1287 SV **mark;
1288
1289 POPBLOCK(cx,newpm);
4fdae800 1290 mark = newsp;
f86702cc 1291 POPLOOP1(cx); /* Delay POPLOOP2 until stack values are safe */
1292
a0d0e21e 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 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 1313 LEAVE;
1314 LEAVE;
1315
f86702cc 1316 return NORMAL;
a0d0e21e 1317}
1318
1319PP(pp_return)
1320{
1321 dSP; dMARK;
1322 I32 cxix;
1323 register CONTEXT *cx;
f86702cc 1324 struct block_sub cxsub;
1325 bool popsub2 = FALSE;
a0d0e21e 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 1333 if (cxstack_ix > sortcxix)
1334 dounwind(sortcxix);
1ce6579f 1335 AvARRAY(curstack)[1] = *SP;
a0d0e21e 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 1350 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1351 popsub2 = TRUE;
a0d0e21e 1352 break;
1353 case CXt_EVAL:
1354 POPEVAL(cx);
748a9306 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 1362 break;
1363 default:
1364 DIE("panic: return");
1365 break;
1366 }
1367
1368 if (gimme == G_SCALAR) {
1369 if (MARK < SP)
f86702cc 1370 *++newsp = (popsub2 && SvTEMP(*SP))
1371 ? *SP : sv_mortalcopy(*SP);
a0d0e21e 1372 else
1373 *++newsp = &sv_undef;
a0d0e21e 1374 }
1375 else {
f86702cc 1376 while (++MARK <= SP)
1377 *++newsp = (popsub2 && SvTEMP(*MARK))
1378 ? *MARK : sv_mortalcopy(*MARK);
a0d0e21e 1379 }
a0d0e21e 1380 stack_sp = newsp;
1381
f86702cc 1382 /* Stack values are safe: */
1383 if (popsub2) {
1384 POPSUB2(); /* release CV and @_ ... */
1385 }
1386 curpm = newpm; /* ... and pop $1 et al */
1387
a0d0e21e 1388 LEAVE;
1389 return pop_return();
1390}
1391
1392PP(pp_last)
1393{
1394 dSP;
1395 I32 cxix;
1396 register CONTEXT *cx;
f86702cc 1397 struct block_loop cxloop;
1398 struct block_sub cxsub;
1399 I32 pop2 = 0;
a0d0e21e 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 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 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 1427 case CXt_SUB:
1428 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1429 pop2 = CXt_SUB;
a0d0e21e 1430 nextop = pop_return();
1431 break;
f86702cc 1432 case CXt_EVAL:
1433 POPEVAL(cx);
a0d0e21e 1434 nextop = pop_return();
1435 break;
1436 default:
1437 DIE("panic: last");
1438 break;
1439 }
1440
1441 if (gimme == G_SCALAR) {
f86702cc 1442 if (MARK < SP)
1443 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
1444 ? *SP : sv_mortalcopy(*SP);
a0d0e21e 1445 else
1446 *++newsp = &sv_undef;
1447 }
1448 else {
f86702cc 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 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 1467
1468 LEAVE;
f86702cc 1469 return nextop;
a0d0e21e 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 1594 if (!CvROOT(cv) && !CvXSUB(cv)) {
1595 if (CvGV(cv)) {
1596 SV *tmpstr = sv_newmortal();
e5cf08de 1597 gv_efullname3(tmpstr, CvGV(cv), Nullch);
4aa0a1f7 1598 DIE("Goto undefined subroutine &%s",SvPVX(tmpstr));
1599 }
1600 DIE("Goto undefined subroutine");
1601 }
1602
a0d0e21e 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 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 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 1634 while (sp > mark) {
1635 sp[1] = sp[0];
1636 sp--;
1637 }
ecfc5424 1638 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
1639 items = (*fp3)(CvXSUBANY(cv).any_i32,
1640 mark - stack_base + 1,
1641 items);
a0d0e21e 1642 sp = stack_base + items;
1643 }
1644 else {
1ce6579f 1645 stack_sp--; /* There is no cv arg. */
a0d0e21e 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 1662 if (CvDEPTH(cv) > AvFILL(padlist)) {
1663 AV *newpad = newAV();
4aa0a1f7 1664 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
a0d0e21e 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 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 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 1686 }
1687 else {
748a9306 1688 av_store(newpad, ix, sv = NEWSV(0,0));
a0d0e21e 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 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 1737 /*
1738 * We do not care about using sv to call CV;
1739 * it's for informational purposes only.
1740 */
1ce6579f 1741 SV *sv = GvSV(DBsub);
1742 save_item(sv);
e5cf08de 1743 gv_efullname3(sv, CvGV(cv), Nullch);
1ce6579f 1744 }
a0d0e21e 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 1785 case CXt_NULL:
1786 DIE("Can't \"goto\" outside a block");
1787 break;
a0d0e21e 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 1821 op = enterops[ix];
1822 (*op->op_ppaddr)();
1823 }
1824 op = oldop;
1825 }
1826 }
1827
1828 if (do_dump) {
a5f75d66 1829#ifdef VMS
1830 if (!retop) retop = main_start;
1831#endif
a0d0e21e 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 1844 }
1845
a0d0e21e 1846 RETURNOP(retop);
1847}
1848
1849PP(pp_exit)
1850{
1851 dSP;
1852 I32 anum;
1853
1854 if (MAXARG < 1)
1855 anum = 0;
1856 else
1857 anum = SvIVx(POPs);
1858 my_exit(anum);
1859 PUSHs(&sv_undef);
1860 RETURN;
1861}
1862
1863#ifdef NOTYET
1864PP(pp_nswitch)
1865{
1866 dSP;
1867 double value = SvNVx(GvSV(cCOP->cop_gv));
1868 register I32 match = I_32(value);
1869
1870 if (value < 0.0) {
1871 if (((double)match) > value)
1872 --match; /* was fractional--truncate other way */
1873 }
1874 match -= cCOP->uop.scop.scop_offset;
1875 if (match < 0)
1876 match = 0;
1877 else if (match > cCOP->uop.scop.scop_max)
1878 match = cCOP->uop.scop.scop_max;
1879 op = cCOP->uop.scop.scop_next[match];
1880 RETURNOP(op);
1881}
1882
1883PP(pp_cswitch)
1884{
1885 dSP;
1886 register I32 match;
1887
1888 if (multiline)
1889 op = op->op_next; /* can't assume anything */
1890 else {
1891 match = *(SvPVx(GvSV(cCOP->cop_gv), na)) & 255;
1892 match -= cCOP->uop.scop.scop_offset;
1893 if (match < 0)
1894 match = 0;
1895 else if (match > cCOP->uop.scop.scop_max)
1896 match = cCOP->uop.scop.scop_max;
1897 op = cCOP->uop.scop.scop_next[match];
1898 }
1899 RETURNOP(op);
1900}
1901#endif
1902
1903/* Eval. */
1904
1905static void
1906save_lines(array, sv)
1907AV *array;
1908SV *sv;
1909{
1910 register char *s = SvPVX(sv);
1911 register char *send = SvPVX(sv) + SvCUR(sv);
1912 register char *t;
1913 register I32 line = 1;
1914
1915 while (s && s < send) {
1916 SV *tmpstr = NEWSV(85,0);
1917
1918 sv_upgrade(tmpstr, SVt_PVMG);
1919 t = strchr(s, '\n');
1920 if (t)
1921 t++;
1922 else
1923 t = send;
1924
1925 sv_setpvn(tmpstr, s, t - s);
1926 av_store(array, line++, tmpstr);
1927 s = t;
1928 }
1929}
1930
1931static OP *
1932doeval(gimme)
1933int gimme;
1934{
1935 dSP;
1936 OP *saveop = op;
1937 HV *newstash;
ff3ff8d1 1938 CV *caller;
748a9306 1939 AV* comppadlist;
a0d0e21e 1940
1941 in_eval = 1;
1942
1ce6579f 1943 PUSHMARK(SP);
1944
a0d0e21e 1945 /* set up a scratch pad */
1946
55497cff 1947 SAVEI32(padix);
a0d0e21e 1948 SAVESPTR(curpad);
1949 SAVESPTR(comppad);
1950 SAVESPTR(comppad_name);
55497cff 1951 SAVEI32(comppad_name_fill);
1952 SAVEI32(min_intro_pending);
1953 SAVEI32(max_intro_pending);
748a9306 1954
ff3ff8d1 1955 caller = compcv;
748a9306 1956 SAVESPTR(compcv);
1957 compcv = (CV*)NEWSV(1104,0);
1958 sv_upgrade((SV *)compcv, SVt_PVCV);
07055b4c 1959 CvUNIQUE_on(compcv);
748a9306 1960
a0d0e21e 1961 comppad = newAV();
1962 comppad_name = newAV();
1963 comppad_name_fill = 0;
1964 min_intro_pending = 0;
1965 av_push(comppad, Nullsv);
1966 curpad = AvARRAY(comppad);
1967 padix = 0;
1968
748a9306 1969 comppadlist = newAV();
1970 AvREAL_off(comppadlist);
8e07c86e 1971 av_store(comppadlist, 0, (SV*)comppad_name);
1972 av_store(comppadlist, 1, (SV*)comppad);
748a9306 1973 CvPADLIST(compcv) = comppadlist;
2c05e328 1974
1975 if (saveop->op_type != OP_REQUIRE)
1976 CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc(caller);
07055b4c 1977
8e07c86e 1978 SAVEFREESV(compcv);
748a9306 1979
a0d0e21e 1980 /* make sure we compile in the right package */
1981
1982 newstash = curcop->cop_stash;
1983 if (curstash != newstash) {
1984 SAVESPTR(curstash);
1985 curstash = newstash;
1986 }
1987 SAVESPTR(beginav);
1988 beginav = newAV();
1989 SAVEFREESV(beginav);
1990
1991 /* try to compile it */
1992
1993 eval_root = Nullop;
1994 error_count = 0;
1995 curcop = &compiling;
1996 curcop->cop_arybase = 0;
c07a80fd 1997 SvREFCNT_dec(rs);
1998 rs = newSVpv("\n", 1);
1ce6579f 1999 if (saveop->op_flags & OPf_SPECIAL)
2000 in_eval |= 4;
2001 else
2002 sv_setpv(GvSV(errgv),"");
a0d0e21e 2003 if (yyparse() || error_count || !eval_root) {
2004 SV **newsp;
2005 I32 gimme;
2006 CONTEXT *cx;
2007 I32 optype;
2008
2009 op = saveop;
2010 if (eval_root) {
2011 op_free(eval_root);
2012 eval_root = Nullop;
2013 }
1ce6579f 2014 SP = stack_base + POPMARK; /* pop original mark */
a0d0e21e 2015 POPBLOCK(cx,curpm);
2016 POPEVAL(cx);
2017 pop_return();
2018 lex_end();
2019 LEAVE;
2020 if (optype == OP_REQUIRE)
4633a7c4 2021 DIE("%s", SvPVx(GvSV(errgv), na));
c07a80fd 2022 SvREFCNT_dec(rs);
2023 rs = SvREFCNT_inc(nrs);
a0d0e21e 2024 RETPUSHUNDEF;
2025 }
c07a80fd 2026 SvREFCNT_dec(rs);
2027 rs = SvREFCNT_inc(nrs);
a0d0e21e 2028 compiling.cop_line = 0;
a0d0e21e 2029 SAVEFREEOP(eval_root);
2030 if (gimme & G_ARRAY)
2031 list(eval_root);
2032 else
2033 scalar(eval_root);
2034
2035 DEBUG_x(dump_eval());
2036
55497cff 2037 /* Register with debugger: */
55497cff 2038 if (perldb && saveop->op_type == OP_REQUIRE) {
2039 CV *cv = perl_get_cv("DB::postponed", FALSE);
55497cff 2040 if (cv) {
2041 dSP;
2042 PUSHMARK(sp);
2043 XPUSHs((SV*)compiling.cop_filegv);
2044 PUTBACK;
2045 perl_call_sv((SV*)cv, G_DISCARD);
2046 }
2047 }
2048
a0d0e21e 2049 /* compiled okay, so do it */
2050
4fdae800 2051 CvDEPTH(compcv) = 1;
2052
1ce6579f 2053 SP = stack_base + POPMARK; /* pop original mark */
a0d0e21e 2054 RETURNOP(eval_start);
2055}
2056
2057PP(pp_require)
2058{
2059 dSP;
2060 register CONTEXT *cx;
2061 SV *sv;
2062 char *name;
2063 char *tmpname;
2064 SV** svp;
2065 I32 gimme = G_SCALAR;
760ac839 2066 PerlIO *tryrsfp = 0;
a0d0e21e 2067
2068 sv = POPs;
4633a7c4 2069 if (SvNIOKp(sv) && !SvPOKp(sv)) {
36477c24 2070 SET_NUMERIC_STANDARD();
a5f75d66 2071 if (atof(patchlevel) + 0.00000999 < SvNV(sv))
2072 DIE("Perl %s required--this is only version %s, stopped",
2073 SvPV(sv,na),patchlevel);
a0d0e21e 2074 RETPUSHYES;
2075 }
2076 name = SvPV(sv, na);
2077 if (!*name)
2078 DIE("Null filename used");
4633a7c4 2079 TAINT_PROPER("require");
a0d0e21e 2080 if (op->op_type == OP_REQUIRE &&
2081 (svp = hv_fetch(GvHVn(incgv), name, SvCUR(sv), 0)) &&
2082 *svp != &sv_undef)
2083 RETPUSHYES;
2084
2085 /* prepare to compile file */
2086
2087 tmpname = savepv(name);
2088 if (*tmpname == '/' ||
2089 (*tmpname == '.' &&
2090 (tmpname[1] == '/' ||
748a9306 2091 (tmpname[1] == '.' && tmpname[2] == '/')))
4633a7c4 2092#ifdef DOSISH
2093 || (tmpname[0] && tmpname[1] == ':')
2094#endif
748a9306 2095#ifdef VMS
bbce6d69 2096 || (strchr(tmpname,':') || ((*tmpname == '[' || *tmpname == '<') &&
2097 (isALNUM(tmpname[1]) || strchr("$-_]>",tmpname[1]))))
748a9306 2098#endif
2099 )
a0d0e21e 2100 {
760ac839 2101 tryrsfp = PerlIO_open(tmpname,"r");
a0d0e21e 2102 }
2103 else {
2104 AV *ar = GvAVn(incgv);
2105 I32 i;
748a9306 2106#ifdef VMS
bbce6d69 2107 char unixified[256];
2108 if (tounixspec_ts(tmpname,unixified) != NULL)
2109 for (i = 0; i <= AvFILL(ar); i++) {
748a9306 2110 if (tounixpath_ts(SvPVx(*av_fetch(ar, i, TRUE), na),buf) == NULL)
4633a7c4 2111 continue;
bbce6d69 2112 strcat(buf,unixified);
748a9306 2113#else
bbce6d69 2114 for (i = 0; i <= AvFILL(ar); i++) {
a0d0e21e 2115 (void)sprintf(buf, "%s/%s",
2116 SvPVx(*av_fetch(ar, i, TRUE), na), name);
748a9306 2117#endif
760ac839 2118 tryrsfp = PerlIO_open(buf, "r");
a0d0e21e 2119 if (tryrsfp) {
2120 char *s = buf;
2121
2122 if (*s == '.' && s[1] == '/')
2123 s += 2;
2124 Safefree(tmpname);
2125 tmpname = savepv(s);
2126 break;
2127 }
2128 }
2129 }
2130 SAVESPTR(compiling.cop_filegv);
2131 compiling.cop_filegv = gv_fetchfile(tmpname);
2132 Safefree(tmpname);
2133 tmpname = Nullch;
2134 if (!tryrsfp) {
2135 if (op->op_type == OP_REQUIRE) {
2136 sprintf(tokenbuf,"Can't locate %s in @INC", name);
2137 if (instr(tokenbuf,".h "))
2138 strcat(tokenbuf," (change .h to .ph maybe?)");
2139 if (instr(tokenbuf,".ph "))
2140 strcat(tokenbuf," (did you run h2ph?)");
2141 DIE("%s",tokenbuf);
2142 }
2143
2144 RETPUSHUNDEF;
2145 }
2146
2147 /* Assume success here to prevent recursive requirement. */
2148 (void)hv_store(GvHVn(incgv), name, strlen(name),
2149 newSVsv(GvSV(compiling.cop_filegv)), 0 );
2150
2151 ENTER;
2152 SAVETMPS;
2153 lex_start(sv_2mortal(newSVpv("",0)));
e50aee73 2154 if (rsfp_filters){
2155 save_aptr(&rsfp_filters);
2156 rsfp_filters = NULL;
2157 }
2158
a0d0e21e 2159 rsfp = tryrsfp;
2160 name = savepv(name);
2161 SAVEFREEPV(name);
2162 SAVEI32(hints);
2163 hints = 0;
2164
2165 /* switch to eval mode */
2166
2167 push_return(op->op_next);
2168 PUSHBLOCK(cx, CXt_EVAL, SP);
2169 PUSHEVAL(cx, name, compiling.cop_filegv);
2170
2171 compiling.cop_line = 0;
2172
2173 PUTBACK;
2174 return doeval(G_SCALAR);
2175}
2176
2177PP(pp_dofile)
2178{
2179 return pp_require(ARGS);
2180}
2181
2182PP(pp_entereval)
2183{
2184 dSP;
2185 register CONTEXT *cx;
2186 dPOPss;
55497cff 2187 I32 gimme = GIMME, was = sub_generation;
2188 char tmpbuf[32], *safestr;
a0d0e21e 2189 STRLEN len;
55497cff 2190 OP *ret;
a0d0e21e 2191
2192 if (!SvPV(sv,len) || !len)
2193 RETPUSHUNDEF;
748a9306 2194 TAINT_PROPER("eval");
a0d0e21e 2195
2196 ENTER;
a0d0e21e 2197 lex_start(sv);
748a9306 2198 SAVETMPS;
a0d0e21e 2199
2200 /* switch to eval mode */
2201
748a9306 2202 SAVESPTR(compiling.cop_filegv);
a0d0e21e 2203 sprintf(tmpbuf, "_<(eval %d)", ++evalseq);
2204 compiling.cop_filegv = gv_fetchfile(tmpbuf+2);
2205 compiling.cop_line = 1;
55497cff 2206 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
2207 deleting the eval's FILEGV from the stash before gv_check() runs
2208 (i.e. before run-time proper). To work around the coredump that
2209 ensues, we always turn GvMULTI_on for any globals that were
2210 introduced within evals. See force_ident(). GSAR 96-10-12 */
2211 safestr = savepv(tmpbuf);
2212 SAVEDELETE(defstash, safestr, strlen(safestr));
a0d0e21e 2213 SAVEI32(hints);
2214 hints = op->op_targ;
2215
2216 push_return(op->op_next);
2217 PUSHBLOCK(cx, CXt_EVAL, SP);
2218 PUSHEVAL(cx, 0, compiling.cop_filegv);
2219
2220 /* prepare to compile string */
2221
2222 if (perldb && curstash != debstash)
2223 save_lines(GvAV(compiling.cop_filegv), linestr);
2224 PUTBACK;
55497cff 2225 ret = doeval(gimme);
2226 if (perldb && was != sub_generation) { /* Some subs defined here. */
2227 strcpy(safestr, "_<(eval )"); /* Anything fake and short. */
2228 }
2229 return ret;
a0d0e21e 2230}
2231
2232PP(pp_leaveeval)
2233{
2234 dSP;
2235 register SV **mark;
2236 SV **newsp;
2237 PMOP *newpm;
2238 I32 gimme;
2239 register CONTEXT *cx;
2240 OP *retop;
760ac839 2241 U8 save_flags = op -> op_flags;
a0d0e21e 2242 I32 optype;
2243
2244 POPBLOCK(cx,newpm);
2245 POPEVAL(cx);
2246 retop = pop_return();
2247
2248 if (gimme == G_SCALAR) {
2249 if (op->op_private & OPpLEAVE_VOID)
2250 MARK = newsp;
2251 else {
2252 MARK = newsp + 1;
2253 if (MARK <= SP) {
2254 if (SvFLAGS(TOPs) & SVs_TEMP)
2255 *MARK = TOPs;
2256 else
2257 *MARK = sv_mortalcopy(TOPs);
2258 }
2259 else {
2260 MEXTEND(mark,0);
2261 *MARK = &sv_undef;
2262 }
2263 }
2264 SP = MARK;
2265 }
2266 else {
2267 for (mark = newsp + 1; mark <= SP; mark++)
760ac839 2268 if (!(SvFLAGS(*mark) & SVs_TEMP))
a0d0e21e 2269 *mark = sv_mortalcopy(*mark);
2270 /* in case LEAVE wipes old return values */
2271 }
2272 curpm = newpm; /* Don't pop $1 et al till now */
2273
4fdae800 2274#ifdef DEBUGGING
2275 assert(CvDEPTH(compcv) == 1);
2276#endif
2277 CvDEPTH(compcv) = 0;
2278
1ce6579f 2279 if (optype == OP_REQUIRE &&
2280 !(gimme == G_SCALAR ? SvTRUE(*sp) : sp > newsp)) {
a0d0e21e 2281 char *name = cx->blk_eval.old_name;
2282
1ce6579f 2283 /* Unassume the success we assumed earlier. */
2284 (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
2285 retop = die("%s did not return a true value", name);
a0d0e21e 2286 }
2287
2288 lex_end();
2289 LEAVE;
4fdae800 2290
760ac839 2291 if (!(save_flags & OPf_SPECIAL))
1ce6579f 2292 sv_setpv(GvSV(errgv),"");
a0d0e21e 2293
2294 RETURNOP(retop);
2295}
2296
a0d0e21e 2297PP(pp_entertry)
2298{
2299 dSP;
2300 register CONTEXT *cx;
2301 I32 gimme = GIMME;
2302
2303 ENTER;
2304 SAVETMPS;
2305
2306 push_return(cLOGOP->op_other->op_next);
2307 PUSHBLOCK(cx, CXt_EVAL, SP);
2308 PUSHEVAL(cx, 0, 0);
2309 eval_root = op; /* Only needed so that goto works right. */
2310
2311 in_eval = 1;
4633a7c4 2312 sv_setpv(GvSV(errgv),"");
a0d0e21e 2313 RETURN;
2314}
2315
2316PP(pp_leavetry)
2317{
2318 dSP;
2319 register SV **mark;
2320 SV **newsp;
2321 PMOP *newpm;
2322 I32 gimme;
2323 register CONTEXT *cx;
2324 I32 optype;
2325
2326 POPBLOCK(cx,newpm);
2327 POPEVAL(cx);
2328 pop_return();
2329
2330 if (gimme == G_SCALAR) {
2331 if (op->op_private & OPpLEAVE_VOID)
2332 MARK = newsp;
2333 else {
2334 MARK = newsp + 1;
2335 if (MARK <= SP) {
2336 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
2337 *MARK = TOPs;
2338 else
2339 *MARK = sv_mortalcopy(TOPs);
2340 }
2341 else {
2342 MEXTEND(mark,0);
2343 *MARK = &sv_undef;
2344 }
2345 }
2346 SP = MARK;
2347 }
2348 else {
2349 for (mark = newsp + 1; mark <= SP; mark++)
760ac839 2350 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP)))
a0d0e21e 2351 *mark = sv_mortalcopy(*mark);
2352 /* in case LEAVE wipes old return values */
2353 }
2354 curpm = newpm; /* Don't pop $1 et al till now */
2355
2356 LEAVE;
4633a7c4 2357 sv_setpv(GvSV(errgv),"");
a0d0e21e 2358 RETURN;
2359}
2360
2361static void
2362doparseform(sv)
2363SV *sv;
2364{
2365 STRLEN len;
2366 register char *s = SvPV_force(sv, len);
2367 register char *send = s + len;
2368 register char *base;
2369 register I32 skipspaces = 0;
2370 bool noblank;
2371 bool repeat;
2372 bool postspace = FALSE;
2373 U16 *fops;
2374 register U16 *fpc;
2375 U16 *linepc;
2376 register I32 arg;
2377 bool ischop;
2378
55497cff 2379 if (len == 0)
bbce6d69 2380 croak("Null picture in formline");
55497cff 2381
2382 New(804, fops, (send - s)*3+10, U16); /* Almost certainly too long... */
a0d0e21e 2383 fpc = fops;
2384
2385 if (s < send) {
2386 linepc = fpc;
2387 *fpc++ = FF_LINEMARK;
2388 noblank = repeat = FALSE;
2389 base = s;
2390 }
2391
2392 while (s <= send) {
2393 switch (*s++) {
2394 default:
2395 skipspaces = 0;
2396 continue;
2397
2398 case '~':
2399 if (*s == '~') {
2400 repeat = TRUE;
2401 *s = ' ';
2402 }
2403 noblank = TRUE;
2404 s[-1] = ' ';
2405 /* FALL THROUGH */
2406 case ' ': case '\t':
2407 skipspaces++;
2408 continue;
2409
2410 case '\n': case 0:
2411 arg = s - base;
2412 skipspaces++;
2413 arg -= skipspaces;
2414 if (arg) {
5f05dabc 2415 if (postspace)
a0d0e21e 2416 *fpc++ = FF_SPACE;
a0d0e21e 2417 *fpc++ = FF_LITERAL;
2418 *fpc++ = arg;
2419 }
5f05dabc 2420 postspace = FALSE;
a0d0e21e 2421 if (s <= send)
2422 skipspaces--;
2423 if (skipspaces) {
2424 *fpc++ = FF_SKIP;
2425 *fpc++ = skipspaces;
2426 }
2427 skipspaces = 0;
2428 if (s <= send)
2429 *fpc++ = FF_NEWLINE;
2430 if (noblank) {
2431 *fpc++ = FF_BLANK;
2432 if (repeat)
2433 arg = fpc - linepc + 1;
2434 else
2435 arg = 0;
2436 *fpc++ = arg;
2437 }
2438 if (s < send) {
2439 linepc = fpc;
2440 *fpc++ = FF_LINEMARK;
2441 noblank = repeat = FALSE;
2442 base = s;
2443 }
2444 else
2445 s++;
2446 continue;
2447
2448 case '@':
2449 case '^':
2450 ischop = s[-1] == '^';
2451
2452 if (postspace) {
2453 *fpc++ = FF_SPACE;
2454 postspace = FALSE;
2455 }
2456 arg = (s - base) - 1;
2457 if (arg) {
2458 *fpc++ = FF_LITERAL;
2459 *fpc++ = arg;
2460 }
2461
2462 base = s - 1;
2463 *fpc++ = FF_FETCH;
2464 if (*s == '*') {
2465 s++;
2466 *fpc++ = 0;
2467 *fpc++ = FF_LINEGLOB;
2468 }
2469 else if (*s == '#' || (*s == '.' && s[1] == '#')) {
2470 arg = ischop ? 512 : 0;
2471 base = s - 1;
2472 while (*s == '#')
2473 s++;
2474 if (*s == '.') {
2475 char *f;
2476 s++;
2477 f = s;
2478 while (*s == '#')
2479 s++;
2480 arg |= 256 + (s - f);
2481 }
2482 *fpc++ = s - base; /* fieldsize for FETCH */
2483 *fpc++ = FF_DECIMAL;
2484 *fpc++ = arg;
2485 }
2486 else {
2487 I32 prespace = 0;
2488 bool ismore = FALSE;
2489
2490 if (*s == '>') {
2491 while (*++s == '>') ;
2492 prespace = FF_SPACE;
2493 }
2494 else if (*s == '|') {
2495 while (*++s == '|') ;
2496 prespace = FF_HALFSPACE;
2497 postspace = TRUE;
2498 }
2499 else {
2500 if (*s == '<')
2501 while (*++s == '<') ;
2502 postspace = TRUE;
2503 }
2504 if (*s == '.' && s[1] == '.' && s[2] == '.') {
2505 s += 3;
2506 ismore = TRUE;
2507 }
2508 *fpc++ = s - base; /* fieldsize for FETCH */
2509
2510 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
2511
2512 if (prespace)
2513 *fpc++ = prespace;
2514 *fpc++ = FF_ITEM;
2515 if (ismore)
2516 *fpc++ = FF_MORE;
2517 if (ischop)
2518 *fpc++ = FF_CHOP;
2519 }
2520 base = s;
2521 skipspaces = 0;
2522 continue;
2523 }
2524 }
2525 *fpc++ = FF_END;
2526
2527 arg = fpc - fops;
2528 { /* need to jump to the next word */
2529 int z;
2530 z = WORD_ALIGN - SvCUR(sv) % WORD_ALIGN;
2531 SvGROW(sv, SvCUR(sv) + z + arg * sizeof(U16) + 4);
2532 s = SvPVX(sv) + SvCUR(sv) + z;
2533 }
2534 Copy(fops, s, arg, U16);
2535 Safefree(fops);
55497cff 2536 sv_magic(sv, Nullsv, 'f', Nullch, 0);
a0d0e21e 2537 SvCOMPILED_on(sv);
2538}