This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Compile(d) at least once with threads on win32
[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));
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 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
8ac85365 167rxres_save(void **rsp, REGEXP *rx)
c90c0ff4 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 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 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{
228 dSP; dMARK; dORIGMARK;
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{
521 dSP;
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{
558 dSP;
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 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{
622 dSP; dMARK; dORIGMARK;
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 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{
756 dSP;
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{
788 dSP;
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 797 (looks_like_number(left) && *SvPVX(left) != '0') )
798 {
a0d0e21e
LW
799 i = SvIV(left);
800 max = SvIV(right);
bbce6d69 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 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 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 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) {
902 case G_VOID:
903 return G_VOID;
904 case G_SCALAR:
e50aee73 905 return G_SCALAR;
54310121 906 case G_ARRAY:
907 return G_ARRAY;
908 default:
909 croak("panic: bad gimme: %d\n", cxstack[cxix].blk_gimme);
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 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 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 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
a863c7d1 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);
a863c7d1
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
a863c7d1 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) {
a863c7d1 1069 char* msg = SvPV(errsv, na);
7a2e2cd6 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 1077 my_failure_exit();
1078 /* NOTREACHED */
a0d0e21e
LW
1079 return 0;
1080}
1081
1082PP(pp_xor)
1083{
1084 dSP; dPOPTOPssrl;
1085 if (SvTRUE(left) != SvTRUE(right))
1086 RETSETYES;
1087 else
1088 RETSETNO;
1089}
1090
1091PP(pp_andassign)
1092{
1093 dSP;
1094 if (!SvTRUE(TOPs))
1095 RETURN;
1096 else
1097 RETURNOP(cLOGOP->op_other);
1098}
1099
1100PP(pp_orassign)
1101{
1102 dSP;
1103 if (SvTRUE(TOPs))
1104 RETURN;
1105 else
1106 RETURNOP(cLOGOP->op_other);
1107}
1108
1109#ifdef DEPRECATED
1110PP(pp_entersubr)
1111{
1112 dSP;
1113 SV** mark = (stack_base + *markstack_ptr + 1);
1114 SV* cv = *mark;
1115 while (mark < sp) { /* emulate old interface */
1116 *mark = mark[1];
1117 mark++;
1118 }
1119 *sp = cv;
11343788 1120 return pp_entersub(ARGS);
a0d0e21e
LW
1121}
1122#endif
1123
1124PP(pp_caller)
1125{
1126 dSP;
1127 register I32 cxix = dopoptosub(cxstack_ix);
1128 register CONTEXT *cx;
1129 I32 dbcxix;
54310121 1130 I32 gimme;
a0d0e21e
LW
1131 SV *sv;
1132 I32 count = 0;
1133
1134 if (MAXARG)
1135 count = POPi;
1136 EXTEND(SP, 6);
1137 for (;;) {
1138 if (cxix < 0) {
1139 if (GIMME != G_ARRAY)
1140 RETPUSHUNDEF;
1141 RETURN;
1142 }
1143 if (DBsub && cxix >= 0 &&
1144 cxstack[cxix].blk_sub.cv == GvCV(DBsub))
1145 count++;
1146 if (!count--)
1147 break;
1148 cxix = dopoptosub(cxix - 1);
1149 }
1150 cx = &cxstack[cxix];
06a5b730 1151 if (cxstack[cxix].cx_type == CXt_SUB) {
1152 dbcxix = dopoptosub(cxix - 1);
1153 /* We expect that cxstack[dbcxix] is CXt_SUB, anyway, the
1154 field below is defined for any cx. */
1155 if (DBsub && dbcxix >= 0 && cxstack[dbcxix].blk_sub.cv == GvCV(DBsub))
1156 cx = &cxstack[dbcxix];
1157 }
1158
a0d0e21e
LW
1159 if (GIMME != G_ARRAY) {
1160 dTARGET;
1161
1162 sv_setpv(TARG, HvNAME(cx->blk_oldcop->cop_stash));
1163 PUSHs(TARG);
1164 RETURN;
1165 }
a0d0e21e
LW
1166
1167 PUSHs(sv_2mortal(newSVpv(HvNAME(cx->blk_oldcop->cop_stash), 0)));
1168 PUSHs(sv_2mortal(newSVpv(SvPVX(GvSV(cx->blk_oldcop->cop_filegv)), 0)));
1169 PUSHs(sv_2mortal(newSViv((I32)cx->blk_oldcop->cop_line)));
1170 if (!MAXARG)
1171 RETURN;
06a5b730 1172 if (cx->cx_type == CXt_SUB) { /* So is cxstack[dbcxix]. */
a0d0e21e 1173 sv = NEWSV(49, 0);
e5cf08de 1174 gv_efullname3(sv, CvGV(cxstack[cxix].blk_sub.cv), Nullch);
a0d0e21e
LW
1175 PUSHs(sv_2mortal(sv));
1176 PUSHs(sv_2mortal(newSViv((I32)cx->blk_sub.hasargs)));
1177 }
1178 else {
1179 PUSHs(sv_2mortal(newSVpv("(eval)",0)));
1180 PUSHs(sv_2mortal(newSViv(0)));
1181 }
54310121 1182 gimme = (I32)cx->blk_gimme;
1183 if (gimme == G_VOID)
1184 PUSHs(&sv_undef);
1185 else
1186 PUSHs(sv_2mortal(newSViv(gimme & G_ARRAY)));
4633a7c4 1187 if (cx->cx_type == CXt_EVAL) {
06a5b730 1188 if (cx->blk_eval.old_op_type == OP_ENTEREVAL) {
4633a7c4 1189 PUSHs(cx->blk_eval.cur_text);
06a5b730 1190 PUSHs(&sv_no);
1191 }
1192 else if (cx->blk_eval.old_name) { /* Try blocks have old_name == 0. */
1193 /* Require, put the name. */
1194 PUSHs(sv_2mortal(newSVpv(cx->blk_eval.old_name, 0)));
1195 PUSHs(&sv_yes);
1196 }
4633a7c4
LW
1197 }
1198 else if (cx->cx_type == CXt_SUB &&
1199 cx->blk_sub.hasargs &&
1200 curcop->cop_stash == debstash)
1201 {
a0d0e21e
LW
1202 AV *ary = cx->blk_sub.argarray;
1203 int off = AvARRAY(ary) - AvALLOC(ary);
1204
1205 if (!dbargs) {
1206 GV* tmpgv;
1207 dbargs = GvAV(gv_AVadd(tmpgv = gv_fetchpv("DB::args", TRUE,
1208 SVt_PVAV)));
a5f75d66 1209 GvMULTI_on(tmpgv);
a0d0e21e
LW
1210 AvREAL_off(dbargs); /* XXX Should be REIFY */
1211 }
1212
1213 if (AvMAX(dbargs) < AvFILL(ary) + off)
1214 av_extend(dbargs, AvFILL(ary) + off);
1215 Copy(AvALLOC(ary), AvARRAY(dbargs), AvFILL(ary) + 1 + off, SV*);
1216 AvFILL(dbargs) = AvFILL(ary) + off;
1217 }
1218 RETURN;
1219}
1220
1221static int
8ac85365 1222sortcv(const void *a, const void *b)
a0d0e21e 1223{
11343788 1224 dTHR;
9607fc9c 1225 SV * const *str1 = (SV * const *)a;
1226 SV * const *str2 = (SV * const *)b;
748a9306 1227 I32 oldsaveix = savestack_ix;
a0d0e21e
LW
1228 I32 oldscopeix = scopestack_ix;
1229 I32 result;
1230 GvSV(firstgv) = *str1;
1231 GvSV(secondgv) = *str2;
1232 stack_sp = stack_base;
1233 op = sortcop;
a6c477ed 1234 runops();
a0d0e21e
LW
1235 if (stack_sp != stack_base + 1)
1236 croak("Sort subroutine didn't return single value");
748a9306 1237 if (!SvNIOKp(*stack_sp))
a0d0e21e
LW
1238 croak("Sort subroutine didn't return a numeric value");
1239 result = SvIV(*stack_sp);
1240 while (scopestack_ix > oldscopeix) {
1241 LEAVE;
1242 }
748a9306 1243 leave_scope(oldsaveix);
a0d0e21e
LW
1244 return result;
1245}
1246
1247static int
8ac85365 1248sortcmp(const void *a, const void *b)
a0d0e21e 1249{
9607fc9c 1250 return sv_cmp(*(SV * const *)a, *(SV * const *)b);
bbce6d69 1251}
e5cf08de 1252
bbce6d69 1253static int
8ac85365 1254sortcmp_locale(const void *a, const void *b)
bbce6d69 1255{
9607fc9c 1256 return sv_cmp_locale(*(SV * const *)a, *(SV * const *)b);
a0d0e21e
LW
1257}
1258
1259PP(pp_reset)
1260{
1261 dSP;
1262 char *tmps;
1263
1264 if (MAXARG < 1)
1265 tmps = "";
1266 else
1267 tmps = POPp;
1268 sv_reset(tmps, curcop->cop_stash);
1269 PUSHs(&sv_yes);
1270 RETURN;
1271}
1272
1273PP(pp_lineseq)
1274{
1275 return NORMAL;
1276}
1277
1278PP(pp_dbstate)
1279{
1280 curcop = (COP*)op;
1281 TAINT_NOT; /* Each statement is presumed innocent */
1282 stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
1283 FREETMPS;
1284
1285 if (op->op_private || SvIV(DBsingle) || SvIV(DBsignal) || SvIV(DBtrace))
1286 {
1287 SV **sp;
1288 register CV *cv;
1289 register CONTEXT *cx;
748a9306 1290 I32 gimme = G_ARRAY;
a0d0e21e
LW
1291 I32 hasargs;
1292 GV *gv;
1293
a0d0e21e
LW
1294 gv = DBgv;
1295 cv = GvCV(gv);
a0d0e21e
LW
1296 if (!cv)
1297 DIE("No DB::DB routine defined");
1298
06a5b730 1299 if (CvDEPTH(cv) >= 1 && !(debug & (1<<30))) /* don't do recursive DB::DB call */
a0d0e21e 1300 return NORMAL;
748a9306 1301
4633a7c4
LW
1302 ENTER;
1303 SAVETMPS;
1304
748a9306 1305 SAVEI32(debug);
55497cff 1306 SAVESTACK_POS();
748a9306
LW
1307 debug = 0;
1308 hasargs = 0;
1309 sp = stack_sp;
1310
a0d0e21e 1311 push_return(op->op_next);
748a9306 1312 PUSHBLOCK(cx, CXt_SUB, sp);
a0d0e21e
LW
1313 PUSHSUB(cx);
1314 CvDEPTH(cv)++;
1315 (void)SvREFCNT_inc(cv);
1316 SAVESPTR(curpad);
1317 curpad = AvARRAY((AV*)*av_fetch(CvPADLIST(cv),1,FALSE));
1318 RETURNOP(CvSTART(cv));
1319 }
1320 else
1321 return NORMAL;
1322}
1323
1324PP(pp_scope)
1325{
1326 return NORMAL;
1327}
1328
1329PP(pp_enteriter)
1330{
1331 dSP; dMARK;
1332 register CONTEXT *cx;
54310121 1333 I32 gimme = GIMME_V;
a0d0e21e
LW
1334 SV **svp;
1335
4633a7c4
LW
1336 ENTER;
1337 SAVETMPS;
1338
a0d0e21e
LW
1339 if (op->op_targ)
1340 svp = &curpad[op->op_targ]; /* "my" variable */
1341 else
1342 svp = &GvSV((GV*)POPs); /* symbol table variable */
1343
4633a7c4
LW
1344 SAVESPTR(*svp);
1345
a0d0e21e
LW
1346 ENTER;
1347
1348 PUSHBLOCK(cx, CXt_LOOP, SP);
1349 PUSHLOOP(cx, svp, MARK);
44a8e56a 1350 if (op->op_flags & OPf_STACKED)
1351 cx->blk_loop.iterary = (AV*)SvREFCNT_inc(POPs);
4633a7c4 1352 else {
1ce6579f 1353 cx->blk_loop.iterary = curstack;
1354 AvFILL(curstack) = sp - stack_base;
4633a7c4
LW
1355 cx->blk_loop.iterix = MARK - stack_base;
1356 }
a0d0e21e
LW
1357
1358 RETURN;
1359}
1360
1361PP(pp_enterloop)
1362{
1363 dSP;
1364 register CONTEXT *cx;
54310121 1365 I32 gimme = GIMME_V;
a0d0e21e
LW
1366
1367 ENTER;
1368 SAVETMPS;
1369 ENTER;
1370
1371 PUSHBLOCK(cx, CXt_LOOP, SP);
1372 PUSHLOOP(cx, 0, SP);
1373
1374 RETURN;
1375}
1376
1377PP(pp_leaveloop)
1378{
1379 dSP;
1380 register CONTEXT *cx;
f86702cc 1381 struct block_loop cxloop;
a0d0e21e
LW
1382 I32 gimme;
1383 SV **newsp;
1384 PMOP *newpm;
1385 SV **mark;
1386
1387 POPBLOCK(cx,newpm);
4fdae800 1388 mark = newsp;
f86702cc 1389 POPLOOP1(cx); /* Delay POPLOOP2 until stack values are safe */
1390
a1f49e72 1391 TAINT_NOT;
54310121 1392 if (gimme == G_VOID)
1393 ; /* do nothing */
1394 else if (gimme == G_SCALAR) {
1395 if (mark < SP)
1396 *++newsp = sv_mortalcopy(*SP);
1397 else
1398 *++newsp = &sv_undef;
a0d0e21e
LW
1399 }
1400 else {
a1f49e72 1401 while (mark < SP) {
a0d0e21e 1402 *++newsp = sv_mortalcopy(*++mark);
a1f49e72
CS
1403 TAINT_NOT; /* Each item is independent */
1404 }
a0d0e21e 1405 }
f86702cc 1406 SP = newsp;
1407 PUTBACK;
1408
1409 POPLOOP2(); /* Stack values are safe: release loop vars ... */
1410 curpm = newpm; /* ... and pop $1 et al */
1411
a0d0e21e
LW
1412 LEAVE;
1413 LEAVE;
1414
f86702cc 1415 return NORMAL;
a0d0e21e
LW
1416}
1417
1418PP(pp_return)
1419{
1420 dSP; dMARK;
1421 I32 cxix;
1422 register CONTEXT *cx;
f86702cc 1423 struct block_sub cxsub;
1424 bool popsub2 = FALSE;
a0d0e21e
LW
1425 I32 gimme;
1426 SV **newsp;
1427 PMOP *newpm;
1428 I32 optype = 0;
1429
1ce6579f 1430 if (curstack == sortstack) {
b3933176 1431 if (cxstack_ix == sortcxix || dopoptosub(cxstack_ix) <= sortcxix) {
16d20bd9
AD
1432 if (cxstack_ix > sortcxix)
1433 dounwind(sortcxix);
1ce6579f 1434 AvARRAY(curstack)[1] = *SP;
a0d0e21e
LW
1435 stack_sp = stack_base + 1;
1436 return 0;
1437 }
1438 }
1439
1440 cxix = dopoptosub(cxstack_ix);
1441 if (cxix < 0)
1442 DIE("Can't return outside a subroutine");
1443 if (cxix < cxstack_ix)
1444 dounwind(cxix);
1445
1446 POPBLOCK(cx,newpm);
1447 switch (cx->cx_type) {
1448 case CXt_SUB:
f86702cc 1449 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1450 popsub2 = TRUE;
a0d0e21e
LW
1451 break;
1452 case CXt_EVAL:
1453 POPEVAL(cx);
748a9306
LW
1454 if (optype == OP_REQUIRE &&
1455 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
1456 {
54310121 1457 /* Unassume the success we assumed earlier. */
748a9306
LW
1458 char *name = cx->blk_eval.old_name;
1459 (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
1460 DIE("%s did not return a true value", name);
1461 }
a0d0e21e
LW
1462 break;
1463 default:
1464 DIE("panic: return");
a0d0e21e
LW
1465 }
1466
a1f49e72 1467 TAINT_NOT;
a0d0e21e
LW
1468 if (gimme == G_SCALAR) {
1469 if (MARK < SP)
f86702cc 1470 *++newsp = (popsub2 && SvTEMP(*SP))
1471 ? *SP : sv_mortalcopy(*SP);
a0d0e21e
LW
1472 else
1473 *++newsp = &sv_undef;
a0d0e21e 1474 }
54310121 1475 else if (gimme == G_ARRAY) {
a1f49e72 1476 while (++MARK <= SP) {
f86702cc 1477 *++newsp = (popsub2 && SvTEMP(*MARK))
1478 ? *MARK : sv_mortalcopy(*MARK);
a1f49e72
CS
1479 TAINT_NOT; /* Each item is independent */
1480 }
a0d0e21e 1481 }
a0d0e21e
LW
1482 stack_sp = newsp;
1483
f86702cc 1484 /* Stack values are safe: */
1485 if (popsub2) {
1486 POPSUB2(); /* release CV and @_ ... */
1487 }
1488 curpm = newpm; /* ... and pop $1 et al */
1489
a0d0e21e
LW
1490 LEAVE;
1491 return pop_return();
1492}
1493
1494PP(pp_last)
1495{
1496 dSP;
1497 I32 cxix;
1498 register CONTEXT *cx;
f86702cc 1499 struct block_loop cxloop;
1500 struct block_sub cxsub;
1501 I32 pop2 = 0;
a0d0e21e
LW
1502 I32 gimme;
1503 I32 optype;
1504 OP *nextop;
1505 SV **newsp;
1506 PMOP *newpm;
1507 SV **mark = stack_base + cxstack[cxstack_ix].blk_oldsp;
a0d0e21e
LW
1508
1509 if (op->op_flags & OPf_SPECIAL) {
1510 cxix = dopoptoloop(cxstack_ix);
1511 if (cxix < 0)
1512 DIE("Can't \"last\" outside a block");
1513 }
1514 else {
1515 cxix = dopoptolabel(cPVOP->op_pv);
1516 if (cxix < 0)
1517 DIE("Label not found for \"last %s\"", cPVOP->op_pv);
1518 }
1519 if (cxix < cxstack_ix)
1520 dounwind(cxix);
1521
1522 POPBLOCK(cx,newpm);
1523 switch (cx->cx_type) {
1524 case CXt_LOOP:
f86702cc 1525 POPLOOP1(cx); /* Delay POPLOOP2 until stack values are safe */
1526 pop2 = CXt_LOOP;
4fdae800 1527 nextop = cxloop.last_op->op_next;
a0d0e21e 1528 break;
f86702cc 1529 case CXt_SUB:
1530 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1531 pop2 = CXt_SUB;
a0d0e21e
LW
1532 nextop = pop_return();
1533 break;
f86702cc 1534 case CXt_EVAL:
1535 POPEVAL(cx);
a0d0e21e
LW
1536 nextop = pop_return();
1537 break;
1538 default:
1539 DIE("panic: last");
a0d0e21e
LW
1540 }
1541
a1f49e72 1542 TAINT_NOT;
a0d0e21e 1543 if (gimme == G_SCALAR) {
f86702cc 1544 if (MARK < SP)
1545 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
1546 ? *SP : sv_mortalcopy(*SP);
a0d0e21e
LW
1547 else
1548 *++newsp = &sv_undef;
1549 }
54310121 1550 else if (gimme == G_ARRAY) {
a1f49e72 1551 while (++MARK <= SP) {
f86702cc 1552 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
1553 ? *MARK : sv_mortalcopy(*MARK);
a1f49e72
CS
1554 TAINT_NOT; /* Each item is independent */
1555 }
f86702cc 1556 }
1557 SP = newsp;
1558 PUTBACK;
1559
1560 /* Stack values are safe: */
1561 switch (pop2) {
1562 case CXt_LOOP:
1563 POPLOOP2(); /* release loop vars ... */
4fdae800 1564 LEAVE;
f86702cc 1565 break;
1566 case CXt_SUB:
1567 POPSUB2(); /* release CV and @_ ... */
1568 break;
a0d0e21e 1569 }
f86702cc 1570 curpm = newpm; /* ... and pop $1 et al */
a0d0e21e
LW
1571
1572 LEAVE;
f86702cc 1573 return nextop;
a0d0e21e
LW
1574}
1575
1576PP(pp_next)
1577{
1578 I32 cxix;
1579 register CONTEXT *cx;
1580 I32 oldsave;
1581
1582 if (op->op_flags & OPf_SPECIAL) {
1583 cxix = dopoptoloop(cxstack_ix);
1584 if (cxix < 0)
1585 DIE("Can't \"next\" outside a block");
1586 }
1587 else {
1588 cxix = dopoptolabel(cPVOP->op_pv);
1589 if (cxix < 0)
1590 DIE("Label not found for \"next %s\"", cPVOP->op_pv);
1591 }
1592 if (cxix < cxstack_ix)
1593 dounwind(cxix);
1594
1595 TOPBLOCK(cx);
1596 oldsave = scopestack[scopestack_ix - 1];
1597 LEAVE_SCOPE(oldsave);
1598 return cx->blk_loop.next_op;
1599}
1600
1601PP(pp_redo)
1602{
1603 I32 cxix;
1604 register CONTEXT *cx;
1605 I32 oldsave;
1606
1607 if (op->op_flags & OPf_SPECIAL) {
1608 cxix = dopoptoloop(cxstack_ix);
1609 if (cxix < 0)
1610 DIE("Can't \"redo\" outside a block");
1611 }
1612 else {
1613 cxix = dopoptolabel(cPVOP->op_pv);
1614 if (cxix < 0)
1615 DIE("Label not found for \"redo %s\"", cPVOP->op_pv);
1616 }
1617 if (cxix < cxstack_ix)
1618 dounwind(cxix);
1619
1620 TOPBLOCK(cx);
1621 oldsave = scopestack[scopestack_ix - 1];
1622 LEAVE_SCOPE(oldsave);
1623 return cx->blk_loop.redo_op;
1624}
1625
1626static OP* lastgotoprobe;
1627
1628static OP *
8ac85365 1629dofindlabel(OP *o, char *label, OP **opstack, OP **oplimit)
a0d0e21e
LW
1630{
1631 OP *kid;
1632 OP **ops = opstack;
fc36a67e 1633 static char too_deep[] = "Target of goto is too deeply nested";
a0d0e21e 1634
fc36a67e 1635 if (ops >= oplimit)
1636 croak(too_deep);
11343788
MB
1637 if (o->op_type == OP_LEAVE ||
1638 o->op_type == OP_SCOPE ||
1639 o->op_type == OP_LEAVELOOP ||
1640 o->op_type == OP_LEAVETRY)
fc36a67e 1641 {
5dc0d613 1642 *ops++ = cUNOPo->op_first;
fc36a67e 1643 if (ops >= oplimit)
1644 croak(too_deep);
1645 }
a0d0e21e 1646 *ops = 0;
11343788 1647 if (o->op_flags & OPf_KIDS) {
a0d0e21e 1648 /* First try all the kids at this level, since that's likeliest. */
11343788 1649 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
a0d0e21e
LW
1650 if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
1651 kCOP->cop_label && strEQ(kCOP->cop_label, label))
1652 return kid;
1653 }
11343788 1654 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
a0d0e21e
LW
1655 if (kid == lastgotoprobe)
1656 continue;
fc36a67e 1657 if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
1658 (ops == opstack ||
1659 (ops[-1]->op_type != OP_NEXTSTATE &&
1660 ops[-1]->op_type != OP_DBSTATE)))
1661 *ops++ = kid;
5dc0d613 1662 if (o = dofindlabel(kid, label, ops, oplimit))
11343788 1663 return o;
a0d0e21e
LW
1664 }
1665 }
1666 *ops = 0;
1667 return 0;
1668}
1669
1670PP(pp_dump)
1671{
1672 return pp_goto(ARGS);
1673 /*NOTREACHED*/
1674}
1675
1676PP(pp_goto)
1677{
1678 dSP;
1679 OP *retop = 0;
1680 I32 ix;
1681 register CONTEXT *cx;
fc36a67e 1682#define GOTO_DEPTH 64
1683 OP *enterops[GOTO_DEPTH];
a0d0e21e
LW
1684 char *label;
1685 int do_dump = (op->op_type == OP_DUMP);
1686
1687 label = 0;
1688 if (op->op_flags & OPf_STACKED) {
1689 SV *sv = POPs;
1690
1691 /* This egregious kludge implements goto &subroutine */
1692 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
1693 I32 cxix;
1694 register CONTEXT *cx;
1695 CV* cv = (CV*)SvRV(sv);
1696 SV** mark;
1697 I32 items = 0;
1698 I32 oldsave;
1699
4aa0a1f7
AD
1700 if (!CvROOT(cv) && !CvXSUB(cv)) {
1701 if (CvGV(cv)) {
1702 SV *tmpstr = sv_newmortal();
e5cf08de 1703 gv_efullname3(tmpstr, CvGV(cv), Nullch);
4aa0a1f7
AD
1704 DIE("Goto undefined subroutine &%s",SvPVX(tmpstr));
1705 }
1706 DIE("Goto undefined subroutine");
1707 }
1708
a0d0e21e
LW
1709 /* First do some returnish stuff. */
1710 cxix = dopoptosub(cxstack_ix);
1711 if (cxix < 0)
1712 DIE("Can't goto subroutine outside a subroutine");
1713 if (cxix < cxstack_ix)
1714 dounwind(cxix);
1715 TOPBLOCK(cx);
1716 mark = stack_sp;
1717 if (cx->blk_sub.hasargs) { /* put @_ back onto stack */
1718 AV* av = cx->blk_sub.argarray;
1719
1720 items = AvFILL(av) + 1;
1ce6579f 1721 stack_sp++;
1722 EXTEND(stack_sp, items); /* @_ could have been extended. */
1723 Copy(AvARRAY(av), stack_sp, items, SV*);
a0d0e21e 1724 stack_sp += items;
6d4ff0d2 1725#ifndef USE_THREADS
2c05e328 1726 SvREFCNT_dec(GvAV(defgv));
a0d0e21e 1727 GvAV(defgv) = cx->blk_sub.savearray;
6d4ff0d2 1728#endif /* USE_THREADS */
a0d0e21e 1729 AvREAL_off(av);
4633a7c4 1730 av_clear(av);
a0d0e21e
LW
1731 }
1732 if (!(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
1733 SvREFCNT_dec(cx->blk_sub.cv);
1734 oldsave = scopestack[scopestack_ix - 1];
1735 LEAVE_SCOPE(oldsave);
1736
1737 /* Now do some callish stuff. */
1738 SAVETMPS;
1739 if (CvXSUB(cv)) {
1740 if (CvOLDSTYLE(cv)) {
ecfc5424 1741 I32 (*fp3)_((int,int,int));
a0d0e21e
LW
1742 while (sp > mark) {
1743 sp[1] = sp[0];
1744 sp--;
1745 }
ecfc5424
AD
1746 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
1747 items = (*fp3)(CvXSUBANY(cv).any_i32,
1748 mark - stack_base + 1,
1749 items);
a0d0e21e
LW
1750 sp = stack_base + items;
1751 }
1752 else {
1ce6579f 1753 stack_sp--; /* There is no cv arg. */
a0d0e21e
LW
1754 (void)(*CvXSUB(cv))(cv);
1755 }
1756 LEAVE;
1757 return pop_return();
1758 }
1759 else {
1760 AV* padlist = CvPADLIST(cv);
1761 SV** svp = AvARRAY(padlist);
1762 cx->blk_sub.cv = cv;
1763 cx->blk_sub.olddepth = CvDEPTH(cv);
1764 CvDEPTH(cv)++;
1765 if (CvDEPTH(cv) < 2)
1766 (void)SvREFCNT_inc(cv);
1767 else { /* save temporaries on recursion? */
1768 if (CvDEPTH(cv) == 100 && dowarn)
44a8e56a 1769 sub_crush_depth(cv);
a0d0e21e
LW
1770 if (CvDEPTH(cv) > AvFILL(padlist)) {
1771 AV *newpad = newAV();
4aa0a1f7 1772 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
a0d0e21e
LW
1773 I32 ix = AvFILL((AV*)svp[1]);
1774 svp = AvARRAY(svp[0]);
748a9306 1775 for ( ;ix > 0; ix--) {
a0d0e21e 1776 if (svp[ix] != &sv_undef) {
748a9306 1777 char *name = SvPVX(svp[ix]);
5f05dabc 1778 if ((SvFLAGS(svp[ix]) & SVf_FAKE)
1779 || *name == '&')
1780 {
1781 /* outer lexical or anon code */
748a9306 1782 av_store(newpad, ix,
4aa0a1f7 1783 SvREFCNT_inc(oldpad[ix]) );
748a9306
LW
1784 }
1785 else { /* our own lexical */
1786 if (*name == '@')
1787 av_store(newpad, ix, sv = (SV*)newAV());
1788 else if (*name == '%')
1789 av_store(newpad, ix, sv = (SV*)newHV());
1790 else
1791 av_store(newpad, ix, sv = NEWSV(0,0));
1792 SvPADMY_on(sv);
1793 }
a0d0e21e
LW
1794 }
1795 else {
748a9306 1796 av_store(newpad, ix, sv = NEWSV(0,0));
a0d0e21e
LW
1797 SvPADTMP_on(sv);
1798 }
1799 }
1800 if (cx->blk_sub.hasargs) {
1801 AV* av = newAV();
1802 av_extend(av, 0);
1803 av_store(newpad, 0, (SV*)av);
1804 AvFLAGS(av) = AVf_REIFY;
1805 }
1806 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
1807 AvFILL(padlist) = CvDEPTH(cv);
1808 svp = AvARRAY(padlist);
1809 }
1810 }
6d4ff0d2
MB
1811#ifdef USE_THREADS
1812 if (!cx->blk_sub.hasargs) {
1813 AV* av = (AV*)curpad[0];
1814
1815 items = AvFILL(av) + 1;
1816 if (items) {
1817 /* Mark is at the end of the stack. */
1818 EXTEND(sp, items);
1819 Copy(AvARRAY(av), sp + 1, items, SV*);
1820 sp += items;
1821 PUTBACK ;
1822 }
1823 }
1824#endif /* USE_THREADS */
a0d0e21e
LW
1825 SAVESPTR(curpad);
1826 curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
6d4ff0d2
MB
1827#ifndef USE_THREADS
1828 if (cx->blk_sub.hasargs)
1829#endif /* USE_THREADS */
1830 {
a0d0e21e
LW
1831 AV* av = (AV*)curpad[0];
1832 SV** ary;
1833
6d4ff0d2 1834#ifndef USE_THREADS
a0d0e21e 1835 cx->blk_sub.savearray = GvAV(defgv);
2c05e328 1836 GvAV(defgv) = (AV*)SvREFCNT_inc(av);
6d4ff0d2
MB
1837#endif /* USE_THREADS */
1838 cx->blk_sub.argarray = av;
a0d0e21e
LW
1839 ++mark;
1840
1841 if (items >= AvMAX(av) + 1) {
1842 ary = AvALLOC(av);
1843 if (AvARRAY(av) != ary) {
1844 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
1845 SvPVX(av) = (char*)ary;
1846 }
1847 if (items >= AvMAX(av) + 1) {
1848 AvMAX(av) = items - 1;
1849 Renew(ary,items+1,SV*);
1850 AvALLOC(av) = ary;
1851 SvPVX(av) = (char*)ary;
1852 }
1853 }
1854 Copy(mark,AvARRAY(av),items,SV*);
1855 AvFILL(av) = items - 1;
1856
1857 while (items--) {
1858 if (*mark)
1859 SvTEMP_off(*mark);
1860 mark++;
1861 }
1862 }
84902520 1863 if (PERLDB_SUB && curstash != debstash) {
44a8e56a 1864 /*
1865 * We do not care about using sv to call CV;
1866 * it's for informational purposes only.
1867 */
1ce6579f 1868 SV *sv = GvSV(DBsub);
1869 save_item(sv);
e5cf08de 1870 gv_efullname3(sv, CvGV(cv), Nullch);
1ce6579f 1871 }
a0d0e21e
LW
1872 RETURNOP(CvSTART(cv));
1873 }
1874 }
1875 else
1876 label = SvPV(sv,na);
1877 }
1878 else if (op->op_flags & OPf_SPECIAL) {
1879 if (! do_dump)
1880 DIE("goto must have label");
1881 }
1882 else
1883 label = cPVOP->op_pv;
1884
1885 if (label && *label) {
1886 OP *gotoprobe = 0;
1887
1888 /* find label */
1889
1890 lastgotoprobe = 0;
1891 *enterops = 0;
1892 for (ix = cxstack_ix; ix >= 0; ix--) {
1893 cx = &cxstack[ix];
1894 switch (cx->cx_type) {
a0d0e21e
LW
1895 case CXt_EVAL:
1896 gotoprobe = eval_root; /* XXX not good for nested eval */
1897 break;
1898 case CXt_LOOP:
1899 gotoprobe = cx->blk_oldcop->op_sibling;
1900 break;
1901 case CXt_SUBST:
1902 continue;
1903 case CXt_BLOCK:
1904 if (ix)
1905 gotoprobe = cx->blk_oldcop->op_sibling;
1906 else
1907 gotoprobe = main_root;
1908 break;
b3933176
CS
1909 case CXt_SUB:
1910 if (CvDEPTH(cx->blk_sub.cv)) {
1911 gotoprobe = CvROOT(cx->blk_sub.cv);
1912 break;
1913 }
1914 /* FALL THROUGH */
0a753a76 1915 case CXt_NULL:
1916 DIE("Can't \"goto\" outside a block");
a0d0e21e
LW
1917 default:
1918 if (ix)
1919 DIE("panic: goto");
68dc0745 1920 gotoprobe = main_root;
a0d0e21e
LW
1921 break;
1922 }
fc36a67e 1923 retop = dofindlabel(gotoprobe, label,
1924 enterops, enterops + GOTO_DEPTH);
a0d0e21e
LW
1925 if (retop)
1926 break;
1927 lastgotoprobe = gotoprobe;
1928 }
1929 if (!retop)
1930 DIE("Can't find label %s", label);
1931
1932 /* pop unwanted frames */
1933
1934 if (ix < cxstack_ix) {
1935 I32 oldsave;
1936
1937 if (ix < 0)
1938 ix = 0;
1939 dounwind(ix);
1940 TOPBLOCK(cx);
1941 oldsave = scopestack[scopestack_ix];
1942 LEAVE_SCOPE(oldsave);
1943 }
1944
1945 /* push wanted frames */
1946
748a9306 1947 if (*enterops && enterops[1]) {
a0d0e21e 1948 OP *oldop = op;
748a9306 1949 for (ix = 1; enterops[ix]; ix++) {
a0d0e21e 1950 op = enterops[ix];
84902520
TB
1951 /* Eventually we may want to stack the needed arguments
1952 * for each op. For now, we punt on the hard ones. */
1953 if (op->op_type == OP_ENTERITER)
1954 DIE("Can't \"goto\" into the middle of a foreach loop",
1955 label);
11343788 1956 (*op->op_ppaddr)(ARGS);
a0d0e21e
LW
1957 }
1958 op = oldop;
1959 }
1960 }
1961
1962 if (do_dump) {
a5f75d66
AD
1963#ifdef VMS
1964 if (!retop) retop = main_start;
1965#endif
a0d0e21e
LW
1966 restartop = retop;
1967 do_undump = TRUE;
1968
1969 my_unexec();
1970
1971 restartop = 0; /* hmm, must be GNU unexec().. */
1972 do_undump = FALSE;
1973 }
1974
1ce6579f 1975 if (curstack == signalstack) {
748a9306 1976 restartop = retop;
54310121 1977 JMPENV_JUMP(3);
748a9306
LW
1978 }
1979
a0d0e21e
LW
1980 RETURNOP(retop);
1981}
1982
1983PP(pp_exit)
1984{
1985 dSP;
1986 I32 anum;
1987
1988 if (MAXARG < 1)
1989 anum = 0;
ff0cee69 1990 else {
a0d0e21e 1991 anum = SvIVx(POPs);
ff0cee69 1992#ifdef VMSISH_EXIT
1993 if (anum == 1 && VMSISH_EXIT)
1994 anum = 0;
1995#endif
1996 }
a0d0e21e
LW
1997 my_exit(anum);
1998 PUSHs(&sv_undef);
1999 RETURN;
2000}
2001
2002#ifdef NOTYET
2003PP(pp_nswitch)
2004{
2005 dSP;
2006 double value = SvNVx(GvSV(cCOP->cop_gv));
2007 register I32 match = I_32(value);
2008
2009 if (value < 0.0) {
2010 if (((double)match) > value)
2011 --match; /* was fractional--truncate other way */
2012 }
2013 match -= cCOP->uop.scop.scop_offset;
2014 if (match < 0)
2015 match = 0;
2016 else if (match > cCOP->uop.scop.scop_max)
2017 match = cCOP->uop.scop.scop_max;
2018 op = cCOP->uop.scop.scop_next[match];
2019 RETURNOP(op);
2020}
2021
2022PP(pp_cswitch)
2023{
2024 dSP;
2025 register I32 match;
2026
2027 if (multiline)
2028 op = op->op_next; /* can't assume anything */
2029 else {
2030 match = *(SvPVx(GvSV(cCOP->cop_gv), na)) & 255;
2031 match -= cCOP->uop.scop.scop_offset;
2032 if (match < 0)
2033 match = 0;
2034 else if (match > cCOP->uop.scop.scop_max)
2035 match = cCOP->uop.scop.scop_max;
2036 op = cCOP->uop.scop.scop_next[match];
2037 }
2038 RETURNOP(op);
2039}
2040#endif
2041
2042/* Eval. */
2043
2044static void
8ac85365 2045save_lines(AV *array, SV *sv)
a0d0e21e
LW
2046{
2047 register char *s = SvPVX(sv);
2048 register char *send = SvPVX(sv) + SvCUR(sv);
2049 register char *t;
2050 register I32 line = 1;
2051
2052 while (s && s < send) {
2053 SV *tmpstr = NEWSV(85,0);
2054
2055 sv_upgrade(tmpstr, SVt_PVMG);
2056 t = strchr(s, '\n');
2057 if (t)
2058 t++;
2059 else
2060 t = send;
2061
2062 sv_setpvn(tmpstr, s, t - s);
2063 av_store(array, line++, tmpstr);
2064 s = t;
2065 }
2066}
2067
2068static OP *
8ac85365 2069docatch(OP *o)
1e422769 2070{
e858de61 2071 dTHR;
1e422769 2072 int ret;
035902c7 2073 I32 oldrunlevel = runlevel;
1e422769 2074 OP *oldop = op;
54310121 2075 dJMPENV;
1e422769 2076
2077 op = o;
1e422769 2078#ifdef DEBUGGING
54310121 2079 assert(CATCH_GET == TRUE);
035902c7 2080 DEBUG_l(deb("(Setting up local jumplevel, runlevel = %ld)\n", (long)runlevel+1));
1e422769 2081#endif
22921e25
CS
2082 JMPENV_PUSH(ret);
2083 switch (ret) {
1e422769 2084 default: /* topmost level handles it */
54310121 2085 JMPENV_POP;
1e422769 2086 runlevel = oldrunlevel;
1e422769 2087 op = oldop;
54310121 2088 JMPENV_JUMP(ret);
1e422769 2089 /* NOTREACHED */
2090 case 3:
2091 if (!restartop) {
2092 PerlIO_printf(PerlIO_stderr(), "panic: restartop\n");
2093 break;
2094 }
1e422769 2095 op = restartop;
2096 restartop = 0;
2097 /* FALL THROUGH */
2098 case 0:
2099 runops();
2100 break;
2101 }
54310121 2102 JMPENV_POP;
1e422769 2103 runlevel = oldrunlevel;
1e422769 2104 op = oldop;
2105 return Nullop;
2106}
2107
0f15f207 2108/* With USE_THREADS, eval_owner must be held on entry to doeval */
1e422769 2109static OP *
8ac85365 2110doeval(int gimme)
a0d0e21e 2111{
11343788 2112 dTHR;
a0d0e21e
LW
2113 dSP;
2114 OP *saveop = op;
2115 HV *newstash;
ff3ff8d1 2116 CV *caller;
748a9306 2117 AV* comppadlist;
a0d0e21e
LW
2118
2119 in_eval = 1;
2120
1ce6579f 2121 PUSHMARK(SP);
2122
a0d0e21e
LW
2123 /* set up a scratch pad */
2124
55497cff 2125 SAVEI32(padix);
a0d0e21e
LW
2126 SAVESPTR(curpad);
2127 SAVESPTR(comppad);
2128 SAVESPTR(comppad_name);
55497cff 2129 SAVEI32(comppad_name_fill);
2130 SAVEI32(min_intro_pending);
2131 SAVEI32(max_intro_pending);
748a9306 2132
ff3ff8d1 2133 caller = compcv;
748a9306
LW
2134 SAVESPTR(compcv);
2135 compcv = (CV*)NEWSV(1104,0);
2136 sv_upgrade((SV *)compcv, SVt_PVCV);
07055b4c 2137 CvUNIQUE_on(compcv);
11343788
MB
2138#ifdef USE_THREADS
2139 CvOWNER(compcv) = 0;
12ca11f6 2140 New(666, CvMUTEXP(compcv), 1, perl_mutex);
11343788 2141 MUTEX_INIT(CvMUTEXP(compcv));
11343788 2142#endif /* USE_THREADS */
748a9306 2143
a0d0e21e 2144 comppad = newAV();
6d4ff0d2
MB
2145 av_push(comppad, Nullsv);
2146 curpad = AvARRAY(comppad);
a0d0e21e
LW
2147 comppad_name = newAV();
2148 comppad_name_fill = 0;
6d4ff0d2
MB
2149 min_intro_pending = 0;
2150 padix = 0;
11343788
MB
2151#ifdef USE_THREADS
2152 av_store(comppad_name, 0, newSVpv("@_", 2));
6d4ff0d2
MB
2153 curpad[0] = (SV*)newAV();
2154 SvPADMY_on(curpad[0]); /* XXX Needed? */
11343788 2155#endif /* USE_THREADS */
a0d0e21e 2156
748a9306
LW
2157 comppadlist = newAV();
2158 AvREAL_off(comppadlist);
8e07c86e
AD
2159 av_store(comppadlist, 0, (SV*)comppad_name);
2160 av_store(comppadlist, 1, (SV*)comppad);
748a9306 2161 CvPADLIST(compcv) = comppadlist;
2c05e328
CS
2162
2163 if (saveop->op_type != OP_REQUIRE)
a863c7d1 2164 CvOUTSIDE(compcv) = caller ? (CV*)SvREFCNT_inc(caller) : 0;
07055b4c 2165
8e07c86e 2166 SAVEFREESV(compcv);
748a9306 2167
a0d0e21e
LW
2168 /* make sure we compile in the right package */
2169
2170 newstash = curcop->cop_stash;
2171 if (curstash != newstash) {
2172 SAVESPTR(curstash);
2173 curstash = newstash;
2174 }
2175 SAVESPTR(beginav);
2176 beginav = newAV();
2177 SAVEFREESV(beginav);
2178
2179 /* try to compile it */
2180
2181 eval_root = Nullop;
2182 error_count = 0;
2183 curcop = &compiling;
2184 curcop->cop_arybase = 0;
c07a80fd 2185 SvREFCNT_dec(rs);
2186 rs = newSVpv("\n", 1);
1ce6579f 2187 if (saveop->op_flags & OPf_SPECIAL)
2188 in_eval |= 4;
2189 else
a863c7d1 2190 sv_setpv(errsv,"");
a0d0e21e
LW
2191 if (yyparse() || error_count || !eval_root) {
2192 SV **newsp;
2193 I32 gimme;
2194 CONTEXT *cx;
2195 I32 optype;
2196
2197 op = saveop;
2198 if (eval_root) {
2199 op_free(eval_root);
2200 eval_root = Nullop;
2201 }
1ce6579f 2202 SP = stack_base + POPMARK; /* pop original mark */
a0d0e21e
LW
2203 POPBLOCK(cx,curpm);
2204 POPEVAL(cx);
2205 pop_return();
2206 lex_end();
2207 LEAVE;
7a2e2cd6 2208 if (optype == OP_REQUIRE) {
a863c7d1 2209 char* msg = SvPV(errsv, na);
7a2e2cd6 2210 DIE("%s", *msg ? msg : "Compilation failed in require");
2211 }
c07a80fd 2212 SvREFCNT_dec(rs);
2213 rs = SvREFCNT_inc(nrs);
f2134d95
MB
2214#ifdef USE_THREADS
2215 MUTEX_LOCK(&eval_mutex);
2216 eval_owner = 0;
2217 COND_SIGNAL(&eval_cond);
2218 MUTEX_UNLOCK(&eval_mutex);
2219#endif /* USE_THREADS */
a0d0e21e
LW
2220 RETPUSHUNDEF;
2221 }
c07a80fd 2222 SvREFCNT_dec(rs);
2223 rs = SvREFCNT_inc(nrs);
a0d0e21e 2224 compiling.cop_line = 0;
a0d0e21e 2225 SAVEFREEOP(eval_root);
54310121 2226 if (gimme & G_VOID)
2227 scalarvoid(eval_root);
2228 else if (gimme & G_ARRAY)
a0d0e21e
LW
2229 list(eval_root);
2230 else
2231 scalar(eval_root);
2232
2233 DEBUG_x(dump_eval());
2234
55497cff 2235 /* Register with debugger: */
84902520 2236 if (PERLDB_INTER && saveop->op_type == OP_REQUIRE) {
55497cff 2237 CV *cv = perl_get_cv("DB::postponed", FALSE);
55497cff 2238 if (cv) {
2239 dSP;
2240 PUSHMARK(sp);
2241 XPUSHs((SV*)compiling.cop_filegv);
2242 PUTBACK;
2243 perl_call_sv((SV*)cv, G_DISCARD);
2244 }
2245 }
2246
a0d0e21e
LW
2247 /* compiled okay, so do it */
2248
4fdae800 2249 CvDEPTH(compcv) = 1;
1ce6579f 2250 SP = stack_base + POPMARK; /* pop original mark */
77fbf95b 2251 op = saveop; /* The caller may need it. */
b35b2403 2252#ifdef USE_THREADS
11343788
MB
2253 MUTEX_LOCK(&eval_mutex);
2254 eval_owner = 0;
2255 COND_SIGNAL(&eval_cond);
2256 MUTEX_UNLOCK(&eval_mutex);
b35b2403 2257#endif /* USE_THREADS */
5dc0d613 2258
a0d0e21e
LW
2259 RETURNOP(eval_start);
2260}
2261
2262PP(pp_require)
2263{
2264 dSP;
2265 register CONTEXT *cx;
2266 SV *sv;
2267 char *name;
46fc3d4c 2268 char *tryname;
2269 SV *namesv = Nullsv;
a0d0e21e
LW
2270 SV** svp;
2271 I32 gimme = G_SCALAR;
760ac839 2272 PerlIO *tryrsfp = 0;
a0d0e21e
LW
2273
2274 sv = POPs;
4633a7c4 2275 if (SvNIOKp(sv) && !SvPOKp(sv)) {
36477c24 2276 SET_NUMERIC_STANDARD();
a5f75d66
AD
2277 if (atof(patchlevel) + 0.00000999 < SvNV(sv))
2278 DIE("Perl %s required--this is only version %s, stopped",
2279 SvPV(sv,na),patchlevel);
a0d0e21e
LW
2280 RETPUSHYES;
2281 }
2282 name = SvPV(sv, na);
2283 if (!*name)
2284 DIE("Null filename used");
4633a7c4 2285 TAINT_PROPER("require");
a0d0e21e
LW
2286 if (op->op_type == OP_REQUIRE &&
2287 (svp = hv_fetch(GvHVn(incgv), name, SvCUR(sv), 0)) &&
2288 *svp != &sv_undef)
2289 RETPUSHYES;
2290
2291 /* prepare to compile file */
2292
46fc3d4c 2293 if (*name == '/' ||
2294 (*name == '.' &&
2295 (name[1] == '/' ||
2296 (name[1] == '.' && name[2] == '/')))
4633a7c4 2297#ifdef DOSISH
46fc3d4c 2298 || (name[0] && name[1] == ':')
4633a7c4 2299#endif
ba42ef2f
WJ
2300#ifdef WIN32
2301 || (name[0] == '\\' && name[1] == '\\') /* UNC path */
2302#endif
748a9306 2303#ifdef VMS
46fc3d4c 2304 || (strchr(name,':') || ((*name == '[' || *name == '<') &&
2305 (isALNUM(name[1]) || strchr("$-_]>",name[1]))))
748a9306
LW
2306#endif
2307 )
a0d0e21e 2308 {
46fc3d4c 2309 tryname = name;
2310 tryrsfp = PerlIO_open(name,"r");
a0d0e21e
LW
2311 }
2312 else {
2313 AV *ar = GvAVn(incgv);
2314 I32 i;
748a9306 2315#ifdef VMS
46fc3d4c 2316 char *unixname;
2317 if ((unixname = tounixspec(name, Nullch)) != Nullch)
2318#endif
2319 {
2320 namesv = NEWSV(806, 0);
2321 for (i = 0; i <= AvFILL(ar); i++) {
2322 char *dir = SvPVx(*av_fetch(ar, i, TRUE), na);
2323#ifdef VMS
2324 char *unixdir;
2325 if ((unixdir = tounixpath(dir, Nullch)) == Nullch)
2326 continue;
2327 sv_setpv(namesv, unixdir);
2328 sv_catpv(namesv, unixname);
748a9306 2329#else
46fc3d4c 2330 sv_setpvf(namesv, "%s/%s", dir, name);
748a9306 2331#endif
46fc3d4c 2332 tryname = SvPVX(namesv);
2333 tryrsfp = PerlIO_open(tryname, "r");
2334 if (tryrsfp) {
2335 if (tryname[0] == '.' && tryname[1] == '/')
2336 tryname += 2;
2337 break;
2338 }
a0d0e21e
LW
2339 }
2340 }
2341 }
2342 SAVESPTR(compiling.cop_filegv);
46fc3d4c 2343 compiling.cop_filegv = gv_fetchfile(tryrsfp ? tryname : name);
2344 SvREFCNT_dec(namesv);
a0d0e21e
LW
2345 if (!tryrsfp) {
2346 if (op->op_type == OP_REQUIRE) {
46fc3d4c 2347 SV *msg = sv_2mortal(newSVpvf("Can't locate %s in @INC", name));
2683423c
JA
2348 SV *dirmsgsv = NEWSV(0, 0);
2349 AV *ar = GvAVn(incgv);
2350 I32 i;
46fc3d4c 2351 if (instr(SvPVX(msg), ".h "))
2352 sv_catpv(msg, " (change .h to .ph maybe?)");
2353 if (instr(SvPVX(msg), ".ph "))
2354 sv_catpv(msg, " (did you run h2ph?)");
3e3baf6d 2355 sv_catpv(msg, " (@INC contains:");
2683423c
JA
2356 for (i = 0; i <= AvFILL(ar); i++) {
2357 char *dir = SvPVx(*av_fetch(ar, i, TRUE), na);
3e3baf6d 2358 sv_setpvf(dirmsgsv, " %s", dir);
2683423c
JA
2359 sv_catsv(msg, dirmsgsv);
2360 }
3e3baf6d 2361 sv_catpvn(msg, ")", 1);
2683423c 2362 SvREFCNT_dec(dirmsgsv);
fc36a67e 2363 DIE("%_", msg);
a0d0e21e
LW
2364 }
2365
2366 RETPUSHUNDEF;
2367 }
2368
2369 /* Assume success here to prevent recursive requirement. */
2370 (void)hv_store(GvHVn(incgv), name, strlen(name),
2371 newSVsv(GvSV(compiling.cop_filegv)), 0 );
2372
2373 ENTER;
2374 SAVETMPS;
2375 lex_start(sv_2mortal(newSVpv("",0)));
e50aee73
AD
2376 if (rsfp_filters){
2377 save_aptr(&rsfp_filters);
2378 rsfp_filters = NULL;
2379 }
2380
a0d0e21e
LW
2381 rsfp = tryrsfp;
2382 name = savepv(name);
2383 SAVEFREEPV(name);
2384 SAVEI32(hints);
2385 hints = 0;
2386
2387 /* switch to eval mode */
2388
2389 push_return(op->op_next);
2390 PUSHBLOCK(cx, CXt_EVAL, SP);
2391 PUSHEVAL(cx, name, compiling.cop_filegv);
2392
2393 compiling.cop_line = 0;
2394
2395 PUTBACK;
0f15f207
MB
2396#ifdef USE_THREADS
2397 MUTEX_LOCK(&eval_mutex);
2398 if (eval_owner && eval_owner != thr)
2399 while (eval_owner)
2400 COND_WAIT(&eval_cond, &eval_mutex);
2401 eval_owner = thr;
2402 MUTEX_UNLOCK(&eval_mutex);
2403#endif /* USE_THREADS */
1e422769 2404 return DOCATCH(doeval(G_SCALAR));
a0d0e21e
LW
2405}
2406
2407PP(pp_dofile)
2408{
2409 return pp_require(ARGS);
2410}
2411
2412PP(pp_entereval)
2413{
2414 dSP;
2415 register CONTEXT *cx;
2416 dPOPss;
54310121 2417 I32 gimme = GIMME_V, was = sub_generation;
fc36a67e 2418 char tmpbuf[TYPE_DIGITS(long) + 12];
2419 char *safestr;
a0d0e21e 2420 STRLEN len;
55497cff 2421 OP *ret;
a0d0e21e
LW
2422
2423 if (!SvPV(sv,len) || !len)
2424 RETPUSHUNDEF;
748a9306 2425 TAINT_PROPER("eval");
a0d0e21e
LW
2426
2427 ENTER;
a0d0e21e 2428 lex_start(sv);
748a9306 2429 SAVETMPS;
a0d0e21e
LW
2430
2431 /* switch to eval mode */
2432
748a9306 2433 SAVESPTR(compiling.cop_filegv);
ff0cee69 2434 sprintf(tmpbuf, "_<(eval %lu)", (unsigned long)++evalseq);
a0d0e21e
LW
2435 compiling.cop_filegv = gv_fetchfile(tmpbuf+2);
2436 compiling.cop_line = 1;
55497cff 2437 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
2438 deleting the eval's FILEGV from the stash before gv_check() runs
2439 (i.e. before run-time proper). To work around the coredump that
2440 ensues, we always turn GvMULTI_on for any globals that were
2441 introduced within evals. See force_ident(). GSAR 96-10-12 */
2442 safestr = savepv(tmpbuf);
2443 SAVEDELETE(defstash, safestr, strlen(safestr));
a0d0e21e
LW
2444 SAVEI32(hints);
2445 hints = op->op_targ;
2446
2447 push_return(op->op_next);
2448 PUSHBLOCK(cx, CXt_EVAL, SP);
2449 PUSHEVAL(cx, 0, compiling.cop_filegv);
2450
2451 /* prepare to compile string */
2452
08ea043f 2453 if (PERLDB_LINE && curstash != debstash)
a0d0e21e
LW
2454 save_lines(GvAV(compiling.cop_filegv), linestr);
2455 PUTBACK;
0f15f207
MB
2456#ifdef USE_THREADS
2457 MUTEX_LOCK(&eval_mutex);
2458 if (eval_owner && eval_owner != thr)
2459 while (eval_owner)
2460 COND_WAIT(&eval_cond, &eval_mutex);
2461 eval_owner = thr;
2462 MUTEX_UNLOCK(&eval_mutex);
2463#endif /* USE_THREADS */
55497cff 2464 ret = doeval(gimme);
08ea043f 2465 if (PERLDB_INTER && was != sub_generation /* Some subs defined here. */
e506e776 2466 && ret != op->op_next) { /* Successive compilation. */
55497cff 2467 strcpy(safestr, "_<(eval )"); /* Anything fake and short. */
2468 }
1e422769 2469 return DOCATCH(ret);
a0d0e21e
LW
2470}
2471
2472PP(pp_leaveeval)
2473{
2474 dSP;
2475 register SV **mark;
2476 SV **newsp;
2477 PMOP *newpm;
2478 I32 gimme;
2479 register CONTEXT *cx;
2480 OP *retop;
760ac839 2481 U8 save_flags = op -> op_flags;
a0d0e21e
LW
2482 I32 optype;
2483
2484 POPBLOCK(cx,newpm);
2485 POPEVAL(cx);
2486 retop = pop_return();
2487
a1f49e72 2488 TAINT_NOT;
54310121 2489 if (gimme == G_VOID)
2490 MARK = newsp;
2491 else if (gimme == G_SCALAR) {
2492 MARK = newsp + 1;
2493 if (MARK <= SP) {
2494 if (SvFLAGS(TOPs) & SVs_TEMP)
2495 *MARK = TOPs;
2496 else
2497 *MARK = sv_mortalcopy(TOPs);
2498 }
a0d0e21e 2499 else {
54310121 2500 MEXTEND(mark,0);
2501 *MARK = &sv_undef;
a0d0e21e 2502 }
a0d0e21e
LW
2503 }
2504 else {
a1f49e72
CS
2505 /* in case LEAVE wipes old return values */
2506 for (mark = newsp + 1; mark <= SP; mark++) {
2507 if (!(SvFLAGS(*mark) & SVs_TEMP)) {
a0d0e21e 2508 *mark = sv_mortalcopy(*mark);
a1f49e72
CS
2509 TAINT_NOT; /* Each item is independent */
2510 }
2511 }
a0d0e21e
LW
2512 }
2513 curpm = newpm; /* Don't pop $1 et al till now */
2514
84902520
TB
2515 /*
2516 * Closures mentioned at top level of eval cannot be referenced
2517 * again, and their presence indirectly causes a memory leak.
2518 * (Note that the fact that compcv and friends are still set here
2519 * is, AFAIK, an accident.) --Chip
2520 */
2521 if (AvFILL(comppad_name) >= 0) {
2522 SV **svp = AvARRAY(comppad_name);
2523 I32 ix;
2524 for (ix = AvFILL(comppad_name); ix >= 0; ix--) {
2525 SV *sv = svp[ix];
2526 if (sv && sv != &sv_undef && *SvPVX(sv) == '&') {
2527 SvREFCNT_dec(sv);
2528 svp[ix] = &sv_undef;
2529
2530 sv = curpad[ix];
2531 if (CvCLONE(sv)) {
2532 SvREFCNT_dec(CvOUTSIDE(sv));
2533 CvOUTSIDE(sv) = Nullcv;
2534 }
2535 else {
2536 SvREFCNT_dec(sv);
2537 sv = NEWSV(0,0);
2538 SvPADTMP_on(sv);
2539 curpad[ix] = sv;
2540 }
2541 }
2542 }
2543 }
2544
4fdae800 2545#ifdef DEBUGGING
2546 assert(CvDEPTH(compcv) == 1);
2547#endif
2548 CvDEPTH(compcv) = 0;
2549
1ce6579f 2550 if (optype == OP_REQUIRE &&
54310121 2551 !(gimme == G_SCALAR ? SvTRUE(*sp) : sp > newsp))
2552 {
1ce6579f 2553 /* Unassume the success we assumed earlier. */
54310121 2554 char *name = cx->blk_eval.old_name;
1ce6579f 2555 (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
2556 retop = die("%s did not return a true value", name);
a0d0e21e
LW
2557 }
2558
2559 lex_end();
2560 LEAVE;
4fdae800 2561
760ac839 2562 if (!(save_flags & OPf_SPECIAL))
a863c7d1 2563 sv_setpv(errsv,"");
a0d0e21e
LW
2564
2565 RETURNOP(retop);
2566}
2567
a0d0e21e
LW
2568PP(pp_entertry)
2569{
2570 dSP;
2571 register CONTEXT *cx;
54310121 2572 I32 gimme = GIMME_V;
a0d0e21e
LW
2573
2574 ENTER;
2575 SAVETMPS;
2576
2577 push_return(cLOGOP->op_other->op_next);
2578 PUSHBLOCK(cx, CXt_EVAL, SP);
2579 PUSHEVAL(cx, 0, 0);
2580 eval_root = op; /* Only needed so that goto works right. */
2581
2582 in_eval = 1;
a863c7d1 2583 sv_setpv(errsv,"");
1e422769 2584 PUTBACK;
2585 return DOCATCH(op->op_next);
a0d0e21e
LW
2586}
2587
2588PP(pp_leavetry)
2589{
2590 dSP;
2591 register SV **mark;
2592 SV **newsp;
2593 PMOP *newpm;
2594 I32 gimme;
2595 register CONTEXT *cx;
2596 I32 optype;
2597
2598 POPBLOCK(cx,newpm);
2599 POPEVAL(cx);
2600 pop_return();
2601
a1f49e72 2602 TAINT_NOT;
54310121 2603 if (gimme == G_VOID)
2604 SP = newsp;
2605 else if (gimme == G_SCALAR) {
2606 MARK = newsp + 1;
2607 if (MARK <= SP) {
2608 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
2609 *MARK = TOPs;
2610 else
2611 *MARK = sv_mortalcopy(TOPs);
2612 }
a0d0e21e 2613 else {
54310121 2614 MEXTEND(mark,0);
2615 *MARK = &sv_undef;
a0d0e21e
LW
2616 }
2617 SP = MARK;
2618 }
2619 else {
a1f49e72
CS
2620 /* in case LEAVE wipes old return values */
2621 for (mark = newsp + 1; mark <= SP; mark++) {
2622 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
a0d0e21e 2623 *mark = sv_mortalcopy(*mark);
a1f49e72
CS
2624 TAINT_NOT; /* Each item is independent */
2625 }
2626 }
a0d0e21e
LW
2627 }
2628 curpm = newpm; /* Don't pop $1 et al till now */
2629
2630 LEAVE;
a863c7d1 2631 sv_setpv(errsv,"");
a0d0e21e
LW
2632 RETURN;
2633}
2634
2635static void
8ac85365 2636doparseform(SV *sv)
a0d0e21e
LW
2637{
2638 STRLEN len;
2639 register char *s = SvPV_force(sv, len);
2640 register char *send = s + len;
2641 register char *base;
2642 register I32 skipspaces = 0;
2643 bool noblank;
2644 bool repeat;
2645 bool postspace = FALSE;
2646 U16 *fops;
2647 register U16 *fpc;
2648 U16 *linepc;
2649 register I32 arg;
2650 bool ischop;
2651
55497cff 2652 if (len == 0)
bbce6d69 2653 croak("Null picture in formline");
55497cff 2654
2655 New(804, fops, (send - s)*3+10, U16); /* Almost certainly too long... */
a0d0e21e
LW
2656 fpc = fops;
2657
2658 if (s < send) {
2659 linepc = fpc;
2660 *fpc++ = FF_LINEMARK;
2661 noblank = repeat = FALSE;
2662 base = s;
2663 }
2664
2665 while (s <= send) {
2666 switch (*s++) {
2667 default:
2668 skipspaces = 0;
2669 continue;
2670
2671 case '~':
2672 if (*s == '~') {
2673 repeat = TRUE;
2674 *s = ' ';
2675 }
2676 noblank = TRUE;
2677 s[-1] = ' ';
2678 /* FALL THROUGH */
2679 case ' ': case '\t':
2680 skipspaces++;
2681 continue;
2682
2683 case '\n': case 0:
2684 arg = s - base;
2685 skipspaces++;
2686 arg -= skipspaces;
2687 if (arg) {
5f05dabc 2688 if (postspace)
a0d0e21e 2689 *fpc++ = FF_SPACE;
a0d0e21e
LW
2690 *fpc++ = FF_LITERAL;
2691 *fpc++ = arg;
2692 }
5f05dabc 2693 postspace = FALSE;
a0d0e21e
LW
2694 if (s <= send)
2695 skipspaces--;
2696 if (skipspaces) {
2697 *fpc++ = FF_SKIP;
2698 *fpc++ = skipspaces;
2699 }
2700 skipspaces = 0;
2701 if (s <= send)
2702 *fpc++ = FF_NEWLINE;
2703 if (noblank) {
2704 *fpc++ = FF_BLANK;
2705 if (repeat)
2706 arg = fpc - linepc + 1;
2707 else
2708 arg = 0;
2709 *fpc++ = arg;
2710 }
2711 if (s < send) {
2712 linepc = fpc;
2713 *fpc++ = FF_LINEMARK;
2714 noblank = repeat = FALSE;
2715 base = s;
2716 }
2717 else
2718 s++;
2719 continue;
2720
2721 case '@':
2722 case '^':
2723 ischop = s[-1] == '^';
2724
2725 if (postspace) {
2726 *fpc++ = FF_SPACE;
2727 postspace = FALSE;
2728 }
2729 arg = (s - base) - 1;
2730 if (arg) {
2731 *fpc++ = FF_LITERAL;
2732 *fpc++ = arg;
2733 }
2734
2735 base = s - 1;
2736 *fpc++ = FF_FETCH;
2737 if (*s == '*') {
2738 s++;
2739 *fpc++ = 0;
2740 *fpc++ = FF_LINEGLOB;
2741 }
2742 else if (*s == '#' || (*s == '.' && s[1] == '#')) {
2743 arg = ischop ? 512 : 0;
2744 base = s - 1;
2745 while (*s == '#')
2746 s++;
2747 if (*s == '.') {
2748 char *f;
2749 s++;
2750 f = s;
2751 while (*s == '#')
2752 s++;
2753 arg |= 256 + (s - f);
2754 }
2755 *fpc++ = s - base; /* fieldsize for FETCH */
2756 *fpc++ = FF_DECIMAL;
2757 *fpc++ = arg;
2758 }
2759 else {
2760 I32 prespace = 0;
2761 bool ismore = FALSE;
2762
2763 if (*s == '>') {
2764 while (*++s == '>') ;
2765 prespace = FF_SPACE;
2766 }
2767 else if (*s == '|') {
2768 while (*++s == '|') ;
2769 prespace = FF_HALFSPACE;
2770 postspace = TRUE;
2771 }
2772 else {
2773 if (*s == '<')
2774 while (*++s == '<') ;
2775 postspace = TRUE;
2776 }
2777 if (*s == '.' && s[1] == '.' && s[2] == '.') {
2778 s += 3;
2779 ismore = TRUE;
2780 }
2781 *fpc++ = s - base; /* fieldsize for FETCH */
2782
2783 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
2784
2785 if (prespace)
2786 *fpc++ = prespace;
2787 *fpc++ = FF_ITEM;
2788 if (ismore)
2789 *fpc++ = FF_MORE;
2790 if (ischop)
2791 *fpc++ = FF_CHOP;
2792 }
2793 base = s;
2794 skipspaces = 0;
2795 continue;
2796 }
2797 }
2798 *fpc++ = FF_END;
2799
2800 arg = fpc - fops;
2801 { /* need to jump to the next word */
2802 int z;
2803 z = WORD_ALIGN - SvCUR(sv) % WORD_ALIGN;
2804 SvGROW(sv, SvCUR(sv) + z + arg * sizeof(U16) + 4);
2805 s = SvPVX(sv) + SvCUR(sv) + z;
2806 }
2807 Copy(fops, s, arg, U16);
2808 Safefree(fops);
55497cff 2809 sv_magic(sv, Nullsv, 'f', Nullch, 0);
a0d0e21e
LW
2810 SvCOMPILED_on(sv);
2811}