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