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