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