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