This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[dummy merge]
[perl5.git] / pp_ctl.c
CommitLineData
a0d0e21e
LW
1/* pp_ctl.c
2 *
9607fc9c 3 * Copyright (c) 1991-1997, Larry Wall
a0d0e21e
LW
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
1e422769 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 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 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;
1e422769 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 }
1e422769 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);
1e422769 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 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 725 (looks_like_number(left) && *SvPVX(left) != '0') )
726 {
a0d0e21e
LW
727 i = SvIV(left);
728 max = SvIV(right);
bbce6d69 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 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) ) {
68dc0745 801 DEBUG_l(deb("(Skipping label #%ld %s)\n",
802 (long)i, cx->blk_loop.label));
a0d0e21e
LW
803 continue;
804 }
68dc0745 805 DEBUG_l( deb("(Found label #%ld %s)\n", (long)i, label));
a0d0e21e
LW
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:
68dc0745 840 DEBUG_l( deb("(Found sub #%ld)\n", (long)i));
a0d0e21e
LW
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:
68dc0745 859 DEBUG_l( deb("(Found eval #%ld)\n", (long)i));
a0d0e21e
LW
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 887 case CXt_NULL:
888 if (dowarn)
889 warn("Exiting pseudo-block via %s", op_name[op->op_type]);
890 return -1;
a0d0e21e 891 case CXt_LOOP:
68dc0745 892 DEBUG_l( deb("(Found loop #%ld)\n", (long)i));
a0d0e21e
LW
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 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 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 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{
9607fc9c 1130 SV * const *str1 = (SV * const *)a;
1131 SV * const *str2 = (SV * const *)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{
9607fc9c 1157 return sv_cmp(*(SV * const *)a, *(SV * const *)b);
bbce6d69 1158}
e5cf08de 1159
bbce6d69 1160static int
1161sortcmp_locale(a, b)
1162const void *a;
1163const void *b;
1164{
9607fc9c 1165 return sv_cmp_locale(*(SV * const *)a, *(SV * const *)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 1259 if (op->op_flags & OPf_STACKED)
1260 cx->blk_loop.iterary = (AV*)SvREFCNT_inc(POPs);
4633a7c4 1261 else {
1ce6579f 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 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 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 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 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");
a0d0e21e
LW
1372 }
1373
1374 if (gimme == G_SCALAR) {
1375 if (MARK < SP)
f86702cc 1376 *++newsp = (popsub2 && SvTEMP(*SP))
1377 ? *SP : sv_mortalcopy(*SP);
a0d0e21e
LW
1378 else
1379 *++newsp = &sv_undef;
a0d0e21e
LW
1380 }
1381 else {
f86702cc 1382 while (++MARK <= SP)
1383 *++newsp = (popsub2 && SvTEMP(*MARK))
1384 ? *MARK : sv_mortalcopy(*MARK);
a0d0e21e 1385 }
a0d0e21e
LW
1386 stack_sp = newsp;
1387
f86702cc 1388 /* Stack values are safe: */
1389 if (popsub2) {
1390 POPSUB2(); /* release CV and @_ ... */
1391 }
1392 curpm = newpm; /* ... and pop $1 et al */
1393
a0d0e21e
LW
1394 LEAVE;
1395 return pop_return();
1396}
1397
1398PP(pp_last)
1399{
1400 dSP;
1401 I32 cxix;
1402 register CONTEXT *cx;
f86702cc 1403 struct block_loop cxloop;
1404 struct block_sub cxsub;
1405 I32 pop2 = 0;
a0d0e21e
LW
1406 I32 gimme;
1407 I32 optype;
1408 OP *nextop;
1409 SV **newsp;
1410 PMOP *newpm;
1411 SV **mark = stack_base + cxstack[cxstack_ix].blk_oldsp;
a0d0e21e
LW
1412
1413 if (op->op_flags & OPf_SPECIAL) {
1414 cxix = dopoptoloop(cxstack_ix);
1415 if (cxix < 0)
1416 DIE("Can't \"last\" outside a block");
1417 }
1418 else {
1419 cxix = dopoptolabel(cPVOP->op_pv);
1420 if (cxix < 0)
1421 DIE("Label not found for \"last %s\"", cPVOP->op_pv);
1422 }
1423 if (cxix < cxstack_ix)
1424 dounwind(cxix);
1425
1426 POPBLOCK(cx,newpm);
1427 switch (cx->cx_type) {
1428 case CXt_LOOP:
f86702cc 1429 POPLOOP1(cx); /* Delay POPLOOP2 until stack values are safe */
1430 pop2 = CXt_LOOP;
4fdae800 1431 nextop = cxloop.last_op->op_next;
a0d0e21e 1432 break;
f86702cc 1433 case CXt_SUB:
1434 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1435 pop2 = CXt_SUB;
a0d0e21e
LW
1436 nextop = pop_return();
1437 break;
f86702cc 1438 case CXt_EVAL:
1439 POPEVAL(cx);
a0d0e21e
LW
1440 nextop = pop_return();
1441 break;
1442 default:
1443 DIE("panic: last");
a0d0e21e
LW
1444 }
1445
1446 if (gimme == G_SCALAR) {
f86702cc 1447 if (MARK < SP)
1448 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
1449 ? *SP : sv_mortalcopy(*SP);
a0d0e21e
LW
1450 else
1451 *++newsp = &sv_undef;
1452 }
1453 else {
f86702cc 1454 while (++MARK <= SP)
1455 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
1456 ? *MARK : sv_mortalcopy(*MARK);
1457 }
1458 SP = newsp;
1459 PUTBACK;
1460
1461 /* Stack values are safe: */
1462 switch (pop2) {
1463 case CXt_LOOP:
1464 POPLOOP2(); /* release loop vars ... */
4fdae800 1465 LEAVE;
f86702cc 1466 break;
1467 case CXt_SUB:
1468 POPSUB2(); /* release CV and @_ ... */
1469 break;
a0d0e21e 1470 }
f86702cc 1471 curpm = newpm; /* ... and pop $1 et al */
a0d0e21e
LW
1472
1473 LEAVE;
f86702cc 1474 return nextop;
a0d0e21e
LW
1475}
1476
1477PP(pp_next)
1478{
1479 I32 cxix;
1480 register CONTEXT *cx;
1481 I32 oldsave;
1482
1483 if (op->op_flags & OPf_SPECIAL) {
1484 cxix = dopoptoloop(cxstack_ix);
1485 if (cxix < 0)
1486 DIE("Can't \"next\" outside a block");
1487 }
1488 else {
1489 cxix = dopoptolabel(cPVOP->op_pv);
1490 if (cxix < 0)
1491 DIE("Label not found for \"next %s\"", cPVOP->op_pv);
1492 }
1493 if (cxix < cxstack_ix)
1494 dounwind(cxix);
1495
1496 TOPBLOCK(cx);
1497 oldsave = scopestack[scopestack_ix - 1];
1498 LEAVE_SCOPE(oldsave);
1499 return cx->blk_loop.next_op;
1500}
1501
1502PP(pp_redo)
1503{
1504 I32 cxix;
1505 register CONTEXT *cx;
1506 I32 oldsave;
1507
1508 if (op->op_flags & OPf_SPECIAL) {
1509 cxix = dopoptoloop(cxstack_ix);
1510 if (cxix < 0)
1511 DIE("Can't \"redo\" outside a block");
1512 }
1513 else {
1514 cxix = dopoptolabel(cPVOP->op_pv);
1515 if (cxix < 0)
1516 DIE("Label not found for \"redo %s\"", cPVOP->op_pv);
1517 }
1518 if (cxix < cxstack_ix)
1519 dounwind(cxix);
1520
1521 TOPBLOCK(cx);
1522 oldsave = scopestack[scopestack_ix - 1];
1523 LEAVE_SCOPE(oldsave);
1524 return cx->blk_loop.redo_op;
1525}
1526
1527static OP* lastgotoprobe;
1528
1529static OP *
1530dofindlabel(op,label,opstack)
1531OP *op;
1532char *label;
1533OP **opstack;
1534{
1535 OP *kid;
1536 OP **ops = opstack;
1537
1538 if (op->op_type == OP_LEAVE ||
1539 op->op_type == OP_SCOPE ||
1540 op->op_type == OP_LEAVELOOP ||
1541 op->op_type == OP_LEAVETRY)
1542 *ops++ = cUNOP->op_first;
1543 *ops = 0;
1544 if (op->op_flags & OPf_KIDS) {
1545 /* First try all the kids at this level, since that's likeliest. */
1546 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling) {
1547 if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
1548 kCOP->cop_label && strEQ(kCOP->cop_label, label))
1549 return kid;
1550 }
1551 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling) {
1552 if (kid == lastgotoprobe)
1553 continue;
1554 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
1555 if (ops > opstack &&
1556 (ops[-1]->op_type == OP_NEXTSTATE ||
1557 ops[-1]->op_type == OP_DBSTATE))
1558 *ops = kid;
1559 else
1560 *ops++ = kid;
1561 }
1562 if (op = dofindlabel(kid,label,ops))
1563 return op;
1564 }
1565 }
1566 *ops = 0;
1567 return 0;
1568}
1569
1570PP(pp_dump)
1571{
1572 return pp_goto(ARGS);
1573 /*NOTREACHED*/
1574}
1575
1576PP(pp_goto)
1577{
1578 dSP;
1579 OP *retop = 0;
1580 I32 ix;
1581 register CONTEXT *cx;
1582 OP *enterops[64];
1583 char *label;
1584 int do_dump = (op->op_type == OP_DUMP);
1585
1586 label = 0;
1587 if (op->op_flags & OPf_STACKED) {
1588 SV *sv = POPs;
1589
1590 /* This egregious kludge implements goto &subroutine */
1591 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
1592 I32 cxix;
1593 register CONTEXT *cx;
1594 CV* cv = (CV*)SvRV(sv);
1595 SV** mark;
1596 I32 items = 0;
1597 I32 oldsave;
1598
4aa0a1f7
AD
1599 if (!CvROOT(cv) && !CvXSUB(cv)) {
1600 if (CvGV(cv)) {
1601 SV *tmpstr = sv_newmortal();
e5cf08de 1602 gv_efullname3(tmpstr, CvGV(cv), Nullch);
4aa0a1f7
AD
1603 DIE("Goto undefined subroutine &%s",SvPVX(tmpstr));
1604 }
1605 DIE("Goto undefined subroutine");
1606 }
1607
a0d0e21e
LW
1608 /* First do some returnish stuff. */
1609 cxix = dopoptosub(cxstack_ix);
1610 if (cxix < 0)
1611 DIE("Can't goto subroutine outside a subroutine");
1612 if (cxix < cxstack_ix)
1613 dounwind(cxix);
1614 TOPBLOCK(cx);
1615 mark = stack_sp;
1616 if (cx->blk_sub.hasargs) { /* put @_ back onto stack */
1617 AV* av = cx->blk_sub.argarray;
1618
1619 items = AvFILL(av) + 1;
1ce6579f 1620 stack_sp++;
1621 EXTEND(stack_sp, items); /* @_ could have been extended. */
1622 Copy(AvARRAY(av), stack_sp, items, SV*);
a0d0e21e 1623 stack_sp += items;
2c05e328 1624 SvREFCNT_dec(GvAV(defgv));
a0d0e21e 1625 GvAV(defgv) = cx->blk_sub.savearray;
a0d0e21e 1626 AvREAL_off(av);
4633a7c4 1627 av_clear(av);
a0d0e21e
LW
1628 }
1629 if (!(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
1630 SvREFCNT_dec(cx->blk_sub.cv);
1631 oldsave = scopestack[scopestack_ix - 1];
1632 LEAVE_SCOPE(oldsave);
1633
1634 /* Now do some callish stuff. */
1635 SAVETMPS;
1636 if (CvXSUB(cv)) {
1637 if (CvOLDSTYLE(cv)) {
ecfc5424 1638 I32 (*fp3)_((int,int,int));
a0d0e21e
LW
1639 while (sp > mark) {
1640 sp[1] = sp[0];
1641 sp--;
1642 }
ecfc5424
AD
1643 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
1644 items = (*fp3)(CvXSUBANY(cv).any_i32,
1645 mark - stack_base + 1,
1646 items);
a0d0e21e
LW
1647 sp = stack_base + items;
1648 }
1649 else {
1ce6579f 1650 stack_sp--; /* There is no cv arg. */
a0d0e21e
LW
1651 (void)(*CvXSUB(cv))(cv);
1652 }
1653 LEAVE;
1654 return pop_return();
1655 }
1656 else {
1657 AV* padlist = CvPADLIST(cv);
1658 SV** svp = AvARRAY(padlist);
1659 cx->blk_sub.cv = cv;
1660 cx->blk_sub.olddepth = CvDEPTH(cv);
1661 CvDEPTH(cv)++;
1662 if (CvDEPTH(cv) < 2)
1663 (void)SvREFCNT_inc(cv);
1664 else { /* save temporaries on recursion? */
1665 if (CvDEPTH(cv) == 100 && dowarn)
44a8e56a 1666 sub_crush_depth(cv);
a0d0e21e
LW
1667 if (CvDEPTH(cv) > AvFILL(padlist)) {
1668 AV *newpad = newAV();
4aa0a1f7 1669 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
a0d0e21e
LW
1670 I32 ix = AvFILL((AV*)svp[1]);
1671 svp = AvARRAY(svp[0]);
748a9306 1672 for ( ;ix > 0; ix--) {
a0d0e21e 1673 if (svp[ix] != &sv_undef) {
748a9306 1674 char *name = SvPVX(svp[ix]);
5f05dabc 1675 if ((SvFLAGS(svp[ix]) & SVf_FAKE)
1676 || *name == '&')
1677 {
1678 /* outer lexical or anon code */
748a9306 1679 av_store(newpad, ix,
4aa0a1f7 1680 SvREFCNT_inc(oldpad[ix]) );
748a9306
LW
1681 }
1682 else { /* our own lexical */
1683 if (*name == '@')
1684 av_store(newpad, ix, sv = (SV*)newAV());
1685 else if (*name == '%')
1686 av_store(newpad, ix, sv = (SV*)newHV());
1687 else
1688 av_store(newpad, ix, sv = NEWSV(0,0));
1689 SvPADMY_on(sv);
1690 }
a0d0e21e
LW
1691 }
1692 else {
748a9306 1693 av_store(newpad, ix, sv = NEWSV(0,0));
a0d0e21e
LW
1694 SvPADTMP_on(sv);
1695 }
1696 }
1697 if (cx->blk_sub.hasargs) {
1698 AV* av = newAV();
1699 av_extend(av, 0);
1700 av_store(newpad, 0, (SV*)av);
1701 AvFLAGS(av) = AVf_REIFY;
1702 }
1703 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
1704 AvFILL(padlist) = CvDEPTH(cv);
1705 svp = AvARRAY(padlist);
1706 }
1707 }
1708 SAVESPTR(curpad);
1709 curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
1710 if (cx->blk_sub.hasargs) {
1711 AV* av = (AV*)curpad[0];
1712 SV** ary;
1713
1714 cx->blk_sub.savearray = GvAV(defgv);
1715 cx->blk_sub.argarray = av;
2c05e328 1716 GvAV(defgv) = (AV*)SvREFCNT_inc(av);
a0d0e21e
LW
1717 ++mark;
1718
1719 if (items >= AvMAX(av) + 1) {
1720 ary = AvALLOC(av);
1721 if (AvARRAY(av) != ary) {
1722 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
1723 SvPVX(av) = (char*)ary;
1724 }
1725 if (items >= AvMAX(av) + 1) {
1726 AvMAX(av) = items - 1;
1727 Renew(ary,items+1,SV*);
1728 AvALLOC(av) = ary;
1729 SvPVX(av) = (char*)ary;
1730 }
1731 }
1732 Copy(mark,AvARRAY(av),items,SV*);
1733 AvFILL(av) = items - 1;
1734
1735 while (items--) {
1736 if (*mark)
1737 SvTEMP_off(*mark);
1738 mark++;
1739 }
1740 }
f967eb5f 1741 if (perldb && curstash != debstash) {
44a8e56a 1742 /*
1743 * We do not care about using sv to call CV;
1744 * it's for informational purposes only.
1745 */
1ce6579f 1746 SV *sv = GvSV(DBsub);
1747 save_item(sv);
e5cf08de 1748 gv_efullname3(sv, CvGV(cv), Nullch);
1ce6579f 1749 }
a0d0e21e
LW
1750 RETURNOP(CvSTART(cv));
1751 }
1752 }
1753 else
1754 label = SvPV(sv,na);
1755 }
1756 else if (op->op_flags & OPf_SPECIAL) {
1757 if (! do_dump)
1758 DIE("goto must have label");
1759 }
1760 else
1761 label = cPVOP->op_pv;
1762
1763 if (label && *label) {
1764 OP *gotoprobe = 0;
1765
1766 /* find label */
1767
1768 lastgotoprobe = 0;
1769 *enterops = 0;
1770 for (ix = cxstack_ix; ix >= 0; ix--) {
1771 cx = &cxstack[ix];
1772 switch (cx->cx_type) {
1773 case CXt_SUB:
1774 gotoprobe = CvROOT(cx->blk_sub.cv);
1775 break;
1776 case CXt_EVAL:
1777 gotoprobe = eval_root; /* XXX not good for nested eval */
1778 break;
1779 case CXt_LOOP:
1780 gotoprobe = cx->blk_oldcop->op_sibling;
1781 break;
1782 case CXt_SUBST:
1783 continue;
1784 case CXt_BLOCK:
1785 if (ix)
1786 gotoprobe = cx->blk_oldcop->op_sibling;
1787 else
1788 gotoprobe = main_root;
1789 break;
0a753a76 1790 case CXt_NULL:
1791 DIE("Can't \"goto\" outside a block");
a0d0e21e
LW
1792 default:
1793 if (ix)
1794 DIE("panic: goto");
68dc0745 1795 gotoprobe = main_root;
a0d0e21e
LW
1796 break;
1797 }
1798 retop = dofindlabel(gotoprobe, label, enterops);
1799 if (retop)
1800 break;
1801 lastgotoprobe = gotoprobe;
1802 }
1803 if (!retop)
1804 DIE("Can't find label %s", label);
1805
1806 /* pop unwanted frames */
1807
1808 if (ix < cxstack_ix) {
1809 I32 oldsave;
1810
1811 if (ix < 0)
1812 ix = 0;
1813 dounwind(ix);
1814 TOPBLOCK(cx);
1815 oldsave = scopestack[scopestack_ix];
1816 LEAVE_SCOPE(oldsave);
1817 }
1818
1819 /* push wanted frames */
1820
748a9306 1821 if (*enterops && enterops[1]) {
a0d0e21e 1822 OP *oldop = op;
748a9306 1823 for (ix = 1; enterops[ix]; ix++) {
a0d0e21e
LW
1824 op = enterops[ix];
1825 (*op->op_ppaddr)();
1826 }
1827 op = oldop;
1828 }
1829 }
1830
1831 if (do_dump) {
a5f75d66
AD
1832#ifdef VMS
1833 if (!retop) retop = main_start;
1834#endif
a0d0e21e
LW
1835 restartop = retop;
1836 do_undump = TRUE;
1837
1838 my_unexec();
1839
1840 restartop = 0; /* hmm, must be GNU unexec().. */
1841 do_undump = FALSE;
1842 }
1843
1ce6579f 1844 if (curstack == signalstack) {
748a9306 1845 restartop = retop;
a5f75d66 1846 Siglongjmp(top_env, 3);
748a9306
LW
1847 }
1848
a0d0e21e
LW
1849 RETURNOP(retop);
1850}
1851
1852PP(pp_exit)
1853{
1854 dSP;
1855 I32 anum;
1856
1857 if (MAXARG < 1)
1858 anum = 0;
ff0cee69 1859 else {
a0d0e21e 1860 anum = SvIVx(POPs);
ff0cee69 1861#ifdef VMSISH_EXIT
1862 if (anum == 1 && VMSISH_EXIT)
1863 anum = 0;
1864#endif
1865 }
a0d0e21e
LW
1866 my_exit(anum);
1867 PUSHs(&sv_undef);
1868 RETURN;
1869}
1870
1871#ifdef NOTYET
1872PP(pp_nswitch)
1873{
1874 dSP;
1875 double value = SvNVx(GvSV(cCOP->cop_gv));
1876 register I32 match = I_32(value);
1877
1878 if (value < 0.0) {
1879 if (((double)match) > value)
1880 --match; /* was fractional--truncate other way */
1881 }
1882 match -= cCOP->uop.scop.scop_offset;
1883 if (match < 0)
1884 match = 0;
1885 else if (match > cCOP->uop.scop.scop_max)
1886 match = cCOP->uop.scop.scop_max;
1887 op = cCOP->uop.scop.scop_next[match];
1888 RETURNOP(op);
1889}
1890
1891PP(pp_cswitch)
1892{
1893 dSP;
1894 register I32 match;
1895
1896 if (multiline)
1897 op = op->op_next; /* can't assume anything */
1898 else {
1899 match = *(SvPVx(GvSV(cCOP->cop_gv), na)) & 255;
1900 match -= cCOP->uop.scop.scop_offset;
1901 if (match < 0)
1902 match = 0;
1903 else if (match > cCOP->uop.scop.scop_max)
1904 match = cCOP->uop.scop.scop_max;
1905 op = cCOP->uop.scop.scop_next[match];
1906 }
1907 RETURNOP(op);
1908}
1909#endif
1910
1911/* Eval. */
1912
1913static void
1914save_lines(array, sv)
1915AV *array;
1916SV *sv;
1917{
1918 register char *s = SvPVX(sv);
1919 register char *send = SvPVX(sv) + SvCUR(sv);
1920 register char *t;
1921 register I32 line = 1;
1922
1923 while (s && s < send) {
1924 SV *tmpstr = NEWSV(85,0);
1925
1926 sv_upgrade(tmpstr, SVt_PVMG);
1927 t = strchr(s, '\n');
1928 if (t)
1929 t++;
1930 else
1931 t = send;
1932
1933 sv_setpvn(tmpstr, s, t - s);
1934 av_store(array, line++, tmpstr);
1935 s = t;
1936 }
1937}
1938
1939static OP *
1e422769 1940docatch(o)
1941OP *o;
1942{
1943 int ret;
1944 int oldrunlevel = runlevel;
1945 OP *oldop = op;
1946 Sigjmp_buf oldtop;
1947
1948 op = o;
1949 Copy(top_env, oldtop, 1, Sigjmp_buf);
1950#ifdef DEBUGGING
1951 assert(mustcatch == TRUE);
1952#endif
1953 mustcatch = FALSE;
1954 switch ((ret = Sigsetjmp(top_env,1))) {
1955 default: /* topmost level handles it */
1956 Copy(oldtop, top_env, 1, Sigjmp_buf);
1957 runlevel = oldrunlevel;
1958 mustcatch = TRUE;
1959 op = oldop;
1960 Siglongjmp(top_env, ret);
1961 /* NOTREACHED */
1962 case 3:
1963 if (!restartop) {
1964 PerlIO_printf(PerlIO_stderr(), "panic: restartop\n");
1965 break;
1966 }
1967 mustcatch = FALSE;
1968 op = restartop;
1969 restartop = 0;
1970 /* FALL THROUGH */
1971 case 0:
1972 runops();
1973 break;
1974 }
1975 Copy(oldtop, top_env, 1, Sigjmp_buf);
1976 runlevel = oldrunlevel;
1977 mustcatch = TRUE;
1978 op = oldop;
1979 return Nullop;
1980}
1981
1982static OP *
a0d0e21e
LW
1983doeval(gimme)
1984int gimme;
1985{
1986 dSP;
1987 OP *saveop = op;
1988 HV *newstash;
ff3ff8d1 1989 CV *caller;
748a9306 1990 AV* comppadlist;
a0d0e21e
LW
1991
1992 in_eval = 1;
1993
1ce6579f 1994 PUSHMARK(SP);
1995
a0d0e21e
LW
1996 /* set up a scratch pad */
1997
55497cff 1998 SAVEI32(padix);
a0d0e21e
LW
1999 SAVESPTR(curpad);
2000 SAVESPTR(comppad);
2001 SAVESPTR(comppad_name);
55497cff 2002 SAVEI32(comppad_name_fill);
2003 SAVEI32(min_intro_pending);
2004 SAVEI32(max_intro_pending);
748a9306 2005
ff3ff8d1 2006 caller = compcv;
748a9306
LW
2007 SAVESPTR(compcv);
2008 compcv = (CV*)NEWSV(1104,0);
2009 sv_upgrade((SV *)compcv, SVt_PVCV);
07055b4c 2010 CvUNIQUE_on(compcv);
748a9306 2011
a0d0e21e
LW
2012 comppad = newAV();
2013 comppad_name = newAV();
2014 comppad_name_fill = 0;
2015 min_intro_pending = 0;
2016 av_push(comppad, Nullsv);
2017 curpad = AvARRAY(comppad);
2018 padix = 0;
2019
748a9306
LW
2020 comppadlist = newAV();
2021 AvREAL_off(comppadlist);
8e07c86e
AD
2022 av_store(comppadlist, 0, (SV*)comppad_name);
2023 av_store(comppadlist, 1, (SV*)comppad);
748a9306 2024 CvPADLIST(compcv) = comppadlist;
2c05e328
CS
2025
2026 if (saveop->op_type != OP_REQUIRE)
2027 CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc(caller);
07055b4c 2028
8e07c86e 2029 SAVEFREESV(compcv);
748a9306 2030
a0d0e21e
LW
2031 /* make sure we compile in the right package */
2032
2033 newstash = curcop->cop_stash;
2034 if (curstash != newstash) {
2035 SAVESPTR(curstash);
2036 curstash = newstash;
2037 }
2038 SAVESPTR(beginav);
2039 beginav = newAV();
2040 SAVEFREESV(beginav);
2041
2042 /* try to compile it */
2043
2044 eval_root = Nullop;
2045 error_count = 0;
2046 curcop = &compiling;
2047 curcop->cop_arybase = 0;
c07a80fd 2048 SvREFCNT_dec(rs);
2049 rs = newSVpv("\n", 1);
1ce6579f 2050 if (saveop->op_flags & OPf_SPECIAL)
2051 in_eval |= 4;
2052 else
2053 sv_setpv(GvSV(errgv),"");
a0d0e21e
LW
2054 if (yyparse() || error_count || !eval_root) {
2055 SV **newsp;
2056 I32 gimme;
2057 CONTEXT *cx;
2058 I32 optype;
2059
2060 op = saveop;
2061 if (eval_root) {
2062 op_free(eval_root);
2063 eval_root = Nullop;
2064 }
1ce6579f 2065 SP = stack_base + POPMARK; /* pop original mark */
a0d0e21e
LW
2066 POPBLOCK(cx,curpm);
2067 POPEVAL(cx);
2068 pop_return();
2069 lex_end();
2070 LEAVE;
2071 if (optype == OP_REQUIRE)
4633a7c4 2072 DIE("%s", SvPVx(GvSV(errgv), na));
c07a80fd 2073 SvREFCNT_dec(rs);
2074 rs = SvREFCNT_inc(nrs);
a0d0e21e
LW
2075 RETPUSHUNDEF;
2076 }
c07a80fd 2077 SvREFCNT_dec(rs);
2078 rs = SvREFCNT_inc(nrs);
a0d0e21e 2079 compiling.cop_line = 0;
a0d0e21e
LW
2080 SAVEFREEOP(eval_root);
2081 if (gimme & G_ARRAY)
2082 list(eval_root);
2083 else
2084 scalar(eval_root);
2085
2086 DEBUG_x(dump_eval());
2087
55497cff 2088 /* Register with debugger: */
55497cff 2089 if (perldb && saveop->op_type == OP_REQUIRE) {
2090 CV *cv = perl_get_cv("DB::postponed", FALSE);
55497cff 2091 if (cv) {
2092 dSP;
2093 PUSHMARK(sp);
2094 XPUSHs((SV*)compiling.cop_filegv);
2095 PUTBACK;
2096 perl_call_sv((SV*)cv, G_DISCARD);
2097 }
2098 }
2099
a0d0e21e
LW
2100 /* compiled okay, so do it */
2101
4fdae800 2102 CvDEPTH(compcv) = 1;
2103
1ce6579f 2104 SP = stack_base + POPMARK; /* pop original mark */
a0d0e21e
LW
2105 RETURNOP(eval_start);
2106}
2107
2108PP(pp_require)
2109{
2110 dSP;
2111 register CONTEXT *cx;
2112 SV *sv;
2113 char *name;
2114 char *tmpname;
2115 SV** svp;
2116 I32 gimme = G_SCALAR;
760ac839 2117 PerlIO *tryrsfp = 0;
a0d0e21e
LW
2118
2119 sv = POPs;
4633a7c4 2120 if (SvNIOKp(sv) && !SvPOKp(sv)) {
36477c24 2121 SET_NUMERIC_STANDARD();
a5f75d66
AD
2122 if (atof(patchlevel) + 0.00000999 < SvNV(sv))
2123 DIE("Perl %s required--this is only version %s, stopped",
2124 SvPV(sv,na),patchlevel);
a0d0e21e
LW
2125 RETPUSHYES;
2126 }
2127 name = SvPV(sv, na);
2128 if (!*name)
2129 DIE("Null filename used");
4633a7c4 2130 TAINT_PROPER("require");
a0d0e21e
LW
2131 if (op->op_type == OP_REQUIRE &&
2132 (svp = hv_fetch(GvHVn(incgv), name, SvCUR(sv), 0)) &&
2133 *svp != &sv_undef)
2134 RETPUSHYES;
2135
2136 /* prepare to compile file */
2137
2138 tmpname = savepv(name);
2139 if (*tmpname == '/' ||
2140 (*tmpname == '.' &&
2141 (tmpname[1] == '/' ||
748a9306 2142 (tmpname[1] == '.' && tmpname[2] == '/')))
4633a7c4
LW
2143#ifdef DOSISH
2144 || (tmpname[0] && tmpname[1] == ':')
2145#endif
748a9306 2146#ifdef VMS
bbce6d69 2147 || (strchr(tmpname,':') || ((*tmpname == '[' || *tmpname == '<') &&
2148 (isALNUM(tmpname[1]) || strchr("$-_]>",tmpname[1]))))
748a9306
LW
2149#endif
2150 )
a0d0e21e 2151 {
760ac839 2152 tryrsfp = PerlIO_open(tmpname,"r");
a0d0e21e
LW
2153 }
2154 else {
2155 AV *ar = GvAVn(incgv);
2156 I32 i;
748a9306 2157#ifdef VMS
bbce6d69 2158 char unixified[256];
2159 if (tounixspec_ts(tmpname,unixified) != NULL)
2160 for (i = 0; i <= AvFILL(ar); i++) {
748a9306 2161 if (tounixpath_ts(SvPVx(*av_fetch(ar, i, TRUE), na),buf) == NULL)
4633a7c4 2162 continue;
bbce6d69 2163 strcat(buf,unixified);
748a9306 2164#else
bbce6d69 2165 for (i = 0; i <= AvFILL(ar); i++) {
a0d0e21e
LW
2166 (void)sprintf(buf, "%s/%s",
2167 SvPVx(*av_fetch(ar, i, TRUE), na), name);
748a9306 2168#endif
760ac839 2169 tryrsfp = PerlIO_open(buf, "r");
a0d0e21e
LW
2170 if (tryrsfp) {
2171 char *s = buf;
2172
2173 if (*s == '.' && s[1] == '/')
2174 s += 2;
2175 Safefree(tmpname);
2176 tmpname = savepv(s);
2177 break;
2178 }
2179 }
2180 }
2181 SAVESPTR(compiling.cop_filegv);
2182 compiling.cop_filegv = gv_fetchfile(tmpname);
2183 Safefree(tmpname);
2184 tmpname = Nullch;
2185 if (!tryrsfp) {
2186 if (op->op_type == OP_REQUIRE) {
2187 sprintf(tokenbuf,"Can't locate %s in @INC", name);
2188 if (instr(tokenbuf,".h "))
2189 strcat(tokenbuf," (change .h to .ph maybe?)");
2190 if (instr(tokenbuf,".ph "))
2191 strcat(tokenbuf," (did you run h2ph?)");
2192 DIE("%s",tokenbuf);
2193 }
2194
2195 RETPUSHUNDEF;
2196 }
2197
2198 /* Assume success here to prevent recursive requirement. */
2199 (void)hv_store(GvHVn(incgv), name, strlen(name),
2200 newSVsv(GvSV(compiling.cop_filegv)), 0 );
2201
2202 ENTER;
2203 SAVETMPS;
2204 lex_start(sv_2mortal(newSVpv("",0)));
e50aee73
AD
2205 if (rsfp_filters){
2206 save_aptr(&rsfp_filters);
2207 rsfp_filters = NULL;
2208 }
2209
a0d0e21e
LW
2210 rsfp = tryrsfp;
2211 name = savepv(name);
2212 SAVEFREEPV(name);
2213 SAVEI32(hints);
2214 hints = 0;
2215
2216 /* switch to eval mode */
2217
2218 push_return(op->op_next);
2219 PUSHBLOCK(cx, CXt_EVAL, SP);
2220 PUSHEVAL(cx, name, compiling.cop_filegv);
2221
2222 compiling.cop_line = 0;
2223
2224 PUTBACK;
1e422769 2225 return DOCATCH(doeval(G_SCALAR));
a0d0e21e
LW
2226}
2227
2228PP(pp_dofile)
2229{
2230 return pp_require(ARGS);
2231}
2232
2233PP(pp_entereval)
2234{
2235 dSP;
2236 register CONTEXT *cx;
2237 dPOPss;
55497cff 2238 I32 gimme = GIMME, was = sub_generation;
2239 char tmpbuf[32], *safestr;
a0d0e21e 2240 STRLEN len;
55497cff 2241 OP *ret;
a0d0e21e
LW
2242
2243 if (!SvPV(sv,len) || !len)
2244 RETPUSHUNDEF;
748a9306 2245 TAINT_PROPER("eval");
a0d0e21e
LW
2246
2247 ENTER;
a0d0e21e 2248 lex_start(sv);
748a9306 2249 SAVETMPS;
a0d0e21e
LW
2250
2251 /* switch to eval mode */
2252
748a9306 2253 SAVESPTR(compiling.cop_filegv);
ff0cee69 2254 sprintf(tmpbuf, "_<(eval %lu)", (unsigned long)++evalseq);
a0d0e21e
LW
2255 compiling.cop_filegv = gv_fetchfile(tmpbuf+2);
2256 compiling.cop_line = 1;
55497cff 2257 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
2258 deleting the eval's FILEGV from the stash before gv_check() runs
2259 (i.e. before run-time proper). To work around the coredump that
2260 ensues, we always turn GvMULTI_on for any globals that were
2261 introduced within evals. See force_ident(). GSAR 96-10-12 */
2262 safestr = savepv(tmpbuf);
2263 SAVEDELETE(defstash, safestr, strlen(safestr));
a0d0e21e
LW
2264 SAVEI32(hints);
2265 hints = op->op_targ;
2266
2267 push_return(op->op_next);
2268 PUSHBLOCK(cx, CXt_EVAL, SP);
2269 PUSHEVAL(cx, 0, compiling.cop_filegv);
2270
2271 /* prepare to compile string */
2272
2273 if (perldb && curstash != debstash)
2274 save_lines(GvAV(compiling.cop_filegv), linestr);
2275 PUTBACK;
55497cff 2276 ret = doeval(gimme);
2277 if (perldb && was != sub_generation) { /* Some subs defined here. */
2278 strcpy(safestr, "_<(eval )"); /* Anything fake and short. */
2279 }
1e422769 2280 return DOCATCH(ret);
a0d0e21e
LW
2281}
2282
2283PP(pp_leaveeval)
2284{
2285 dSP;
2286 register SV **mark;
2287 SV **newsp;
2288 PMOP *newpm;
2289 I32 gimme;
2290 register CONTEXT *cx;
2291 OP *retop;
760ac839 2292 U8 save_flags = op -> op_flags;
a0d0e21e
LW
2293 I32 optype;
2294
2295 POPBLOCK(cx,newpm);
2296 POPEVAL(cx);
2297 retop = pop_return();
2298
2299 if (gimme == G_SCALAR) {
2300 if (op->op_private & OPpLEAVE_VOID)
2301 MARK = newsp;
2302 else {
2303 MARK = newsp + 1;
2304 if (MARK <= SP) {
2305 if (SvFLAGS(TOPs) & SVs_TEMP)
2306 *MARK = TOPs;
2307 else
2308 *MARK = sv_mortalcopy(TOPs);
2309 }
2310 else {
2311 MEXTEND(mark,0);
2312 *MARK = &sv_undef;
2313 }
2314 }
2315 SP = MARK;
2316 }
2317 else {
2318 for (mark = newsp + 1; mark <= SP; mark++)
760ac839 2319 if (!(SvFLAGS(*mark) & SVs_TEMP))
a0d0e21e
LW
2320 *mark = sv_mortalcopy(*mark);
2321 /* in case LEAVE wipes old return values */
2322 }
2323 curpm = newpm; /* Don't pop $1 et al till now */
2324
4fdae800 2325#ifdef DEBUGGING
2326 assert(CvDEPTH(compcv) == 1);
2327#endif
2328 CvDEPTH(compcv) = 0;
2329
1ce6579f 2330 if (optype == OP_REQUIRE &&
2331 !(gimme == G_SCALAR ? SvTRUE(*sp) : sp > newsp)) {
a0d0e21e
LW
2332 char *name = cx->blk_eval.old_name;
2333
1ce6579f 2334 /* Unassume the success we assumed earlier. */
2335 (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
2336 retop = die("%s did not return a true value", name);
a0d0e21e
LW
2337 }
2338
2339 lex_end();
2340 LEAVE;
4fdae800 2341
760ac839 2342 if (!(save_flags & OPf_SPECIAL))
1ce6579f 2343 sv_setpv(GvSV(errgv),"");
a0d0e21e
LW
2344
2345 RETURNOP(retop);
2346}
2347
a0d0e21e
LW
2348PP(pp_entertry)
2349{
2350 dSP;
2351 register CONTEXT *cx;
2352 I32 gimme = GIMME;
2353
2354 ENTER;
2355 SAVETMPS;
2356
2357 push_return(cLOGOP->op_other->op_next);
2358 PUSHBLOCK(cx, CXt_EVAL, SP);
2359 PUSHEVAL(cx, 0, 0);
2360 eval_root = op; /* Only needed so that goto works right. */
2361
2362 in_eval = 1;
4633a7c4 2363 sv_setpv(GvSV(errgv),"");
1e422769 2364 PUTBACK;
2365 return DOCATCH(op->op_next);
a0d0e21e
LW
2366}
2367
2368PP(pp_leavetry)
2369{
2370 dSP;
2371 register SV **mark;
2372 SV **newsp;
2373 PMOP *newpm;
2374 I32 gimme;
2375 register CONTEXT *cx;
2376 I32 optype;
2377
2378 POPBLOCK(cx,newpm);
2379 POPEVAL(cx);
2380 pop_return();
2381
2382 if (gimme == G_SCALAR) {
2383 if (op->op_private & OPpLEAVE_VOID)
2384 MARK = newsp;
2385 else {
2386 MARK = newsp + 1;
2387 if (MARK <= SP) {
2388 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
2389 *MARK = TOPs;
2390 else
2391 *MARK = sv_mortalcopy(TOPs);
2392 }
2393 else {
2394 MEXTEND(mark,0);
2395 *MARK = &sv_undef;
2396 }
2397 }
2398 SP = MARK;
2399 }
2400 else {
2401 for (mark = newsp + 1; mark <= SP; mark++)
760ac839 2402 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP)))
a0d0e21e
LW
2403 *mark = sv_mortalcopy(*mark);
2404 /* in case LEAVE wipes old return values */
2405 }
2406 curpm = newpm; /* Don't pop $1 et al till now */
2407
2408 LEAVE;
4633a7c4 2409 sv_setpv(GvSV(errgv),"");
a0d0e21e
LW
2410 RETURN;
2411}
2412
2413static void
2414doparseform(sv)
2415SV *sv;
2416{
2417 STRLEN len;
2418 register char *s = SvPV_force(sv, len);
2419 register char *send = s + len;
2420 register char *base;
2421 register I32 skipspaces = 0;
2422 bool noblank;
2423 bool repeat;
2424 bool postspace = FALSE;
2425 U16 *fops;
2426 register U16 *fpc;
2427 U16 *linepc;
2428 register I32 arg;
2429 bool ischop;
2430
55497cff 2431 if (len == 0)
bbce6d69 2432 croak("Null picture in formline");
55497cff 2433
2434 New(804, fops, (send - s)*3+10, U16); /* Almost certainly too long... */
a0d0e21e
LW
2435 fpc = fops;
2436
2437 if (s < send) {
2438 linepc = fpc;
2439 *fpc++ = FF_LINEMARK;
2440 noblank = repeat = FALSE;
2441 base = s;
2442 }
2443
2444 while (s <= send) {
2445 switch (*s++) {
2446 default:
2447 skipspaces = 0;
2448 continue;
2449
2450 case '~':
2451 if (*s == '~') {
2452 repeat = TRUE;
2453 *s = ' ';
2454 }
2455 noblank = TRUE;
2456 s[-1] = ' ';
2457 /* FALL THROUGH */
2458 case ' ': case '\t':
2459 skipspaces++;
2460 continue;
2461
2462 case '\n': case 0:
2463 arg = s - base;
2464 skipspaces++;
2465 arg -= skipspaces;
2466 if (arg) {
5f05dabc 2467 if (postspace)
a0d0e21e 2468 *fpc++ = FF_SPACE;
a0d0e21e
LW
2469 *fpc++ = FF_LITERAL;
2470 *fpc++ = arg;
2471 }
5f05dabc 2472 postspace = FALSE;
a0d0e21e
LW
2473 if (s <= send)
2474 skipspaces--;
2475 if (skipspaces) {
2476 *fpc++ = FF_SKIP;
2477 *fpc++ = skipspaces;
2478 }
2479 skipspaces = 0;
2480 if (s <= send)
2481 *fpc++ = FF_NEWLINE;
2482 if (noblank) {
2483 *fpc++ = FF_BLANK;
2484 if (repeat)
2485 arg = fpc - linepc + 1;
2486 else
2487 arg = 0;
2488 *fpc++ = arg;
2489 }
2490 if (s < send) {
2491 linepc = fpc;
2492 *fpc++ = FF_LINEMARK;
2493 noblank = repeat = FALSE;
2494 base = s;
2495 }
2496 else
2497 s++;
2498 continue;
2499
2500 case '@':
2501 case '^':
2502 ischop = s[-1] == '^';
2503
2504 if (postspace) {
2505 *fpc++ = FF_SPACE;
2506 postspace = FALSE;
2507 }
2508 arg = (s - base) - 1;
2509 if (arg) {
2510 *fpc++ = FF_LITERAL;
2511 *fpc++ = arg;
2512 }
2513
2514 base = s - 1;
2515 *fpc++ = FF_FETCH;
2516 if (*s == '*') {
2517 s++;
2518 *fpc++ = 0;
2519 *fpc++ = FF_LINEGLOB;
2520 }
2521 else if (*s == '#' || (*s == '.' && s[1] == '#')) {
2522 arg = ischop ? 512 : 0;
2523 base = s - 1;
2524 while (*s == '#')
2525 s++;
2526 if (*s == '.') {
2527 char *f;
2528 s++;
2529 f = s;
2530 while (*s == '#')
2531 s++;
2532 arg |= 256 + (s - f);
2533 }
2534 *fpc++ = s - base; /* fieldsize for FETCH */
2535 *fpc++ = FF_DECIMAL;
2536 *fpc++ = arg;
2537 }
2538 else {
2539 I32 prespace = 0;
2540 bool ismore = FALSE;
2541
2542 if (*s == '>') {
2543 while (*++s == '>') ;
2544 prespace = FF_SPACE;
2545 }
2546 else if (*s == '|') {
2547 while (*++s == '|') ;
2548 prespace = FF_HALFSPACE;
2549 postspace = TRUE;
2550 }
2551 else {
2552 if (*s == '<')
2553 while (*++s == '<') ;
2554 postspace = TRUE;
2555 }
2556 if (*s == '.' && s[1] == '.' && s[2] == '.') {
2557 s += 3;
2558 ismore = TRUE;
2559 }
2560 *fpc++ = s - base; /* fieldsize for FETCH */
2561
2562 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
2563
2564 if (prespace)
2565 *fpc++ = prespace;
2566 *fpc++ = FF_ITEM;
2567 if (ismore)
2568 *fpc++ = FF_MORE;
2569 if (ischop)
2570 *fpc++ = FF_CHOP;
2571 }
2572 base = s;
2573 skipspaces = 0;
2574 continue;
2575 }
2576 }
2577 *fpc++ = FF_END;
2578
2579 arg = fpc - fops;
2580 { /* need to jump to the next word */
2581 int z;
2582 z = WORD_ALIGN - SvCUR(sv) % WORD_ALIGN;
2583 SvGROW(sv, SvCUR(sv) + z + arg * sizeof(U16) + 4);
2584 s = SvPVX(sv) + SvCUR(sv) + z;
2585 }
2586 Copy(fops, s, arg, U16);
2587 Safefree(fops);
55497cff 2588 sv_magic(sv, Nullsv, 'f', Nullch, 0);
a0d0e21e
LW
2589 SvCOMPILED_on(sv);
2590}