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