Use __declspec(thread) var rather tha TslAlloc & co.
[p5sagit/p5-mst-13.2.git] / pp_ctl.c
CommitLineData
a0d0e21e 1/* pp_ctl.c
2 *
9607fc9c 3 * Copyright (c) 1991-1997, Larry Wall
a0d0e21e 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 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 40
41static I32 sortcxix;
42
43PP(pp_wantarray)
44{
4e35701f 45 djSP;
a0d0e21e 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 61}
62
63PP(pp_regcmaybe)
64{
65 return NORMAL;
66}
67
68PP(pp_regcomp) {
4e35701f 69 djSP;
a0d0e21e 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 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 87 pm->op_pmflags = pm->op_pmpermflags; /* reset case sensitivity */
88 pm->op_pmregexp = pregcomp(t, t + len, pm);
89 }
a0d0e21e 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 98 hoistmust(pm);
99 cLOGOP->op_first->op_next = op->op_next;
a0d0e21e 100 }
101 RETURN;
102}
103
104PP(pp_substcont)
105{
4e35701f 106 djSP;
a0d0e21e 107 register PMOP *pm = (PMOP*) cLOGOP->op_other;
c09156bb 108 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
a0d0e21e 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 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 124
125 /* Are we done */
e50aee73 126 if (cx->sb_once || !pregexec(rx, s, cx->sb_strend, orig,
a0d0e21e 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 132 TAINT_IF(cx->sb_rxtainted || rx->exec_tainted);
133
4633a7c4 134 (void)SvOOK_off(targ);
cb0b1708 135 Safefree(SvPVX(targ));
748a9306 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 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 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 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 226PP(pp_formline)
227{
4e35701f 228 djSP; dMARK; dORIGMARK;
a0d0e21e 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 244 double value;
245 bool gotsome;
246 STRLEN len;
247
55497cff 248 if (!SvMAGICAL(form) || !SvCOMPILED(form)) {
a0d0e21e 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 289 } )
290 switch (*fpc++) {
291 case FF_LINEMARK:
292 linemark = t;
a0d0e21e 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 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 522 SV *src;
523
524 if (stack_base + *markstack_ptr == sp) {
525 (void)POPMARK;
54310121 526 if (GIMME_V == G_SCALAR)
a0d0e21e 527 XPUSHs(&sv_no);
528 RETURNOP(op->op_next->op_next);
529 }
530 stack_sp = stack_base + *markstack_ptr + 1;
11343788 531 pp_pushmark(ARGS); /* push dst */
532 pp_pushmark(ARGS); /* push src */
a0d0e21e 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 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 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 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 598 dTARGET;
599 XPUSHi(items);
a0d0e21e 600 }
54310121 601 else if (gimme == G_ARRAY)
602 SP += items;
a0d0e21e 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 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 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 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 681 SvTEMP_off(*up);
682 if (!sortcop && !SvPOK(*up))
a0d0e21e 683 (void)sv_2pv(*up, &na);
a0d0e21e 684 up++;
685 }
686 }
687 max = --up - myorigmark;
688 if (sortcop) {
689 if (max > 1) {
690 AV *oldstack;
c09156bb 691 PERL_CONTEXT *cx;
a0d0e21e 692 SV** newsp;
54310121 693 bool oldcatch = CATCH_GET;
a0d0e21e 694
695 SAVETMPS;
462e5cf6 696 SAVEOP();
a0d0e21e 697
1ce6579f 698 oldstack = curstack;
a0d0e21e 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 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 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 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 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 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 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 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 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 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 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 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 848 register I32 i;
c09156bb 849 register PERL_CONTEXT *cx;
a0d0e21e 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 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 875 continue;
876 }
68dc0745 877 DEBUG_l( deb("(Found label #%ld %s)\n", (long)i, label));
a0d0e21e 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 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 904 case G_ARRAY:
905 return G_ARRAY;
906 default:
907 croak("panic: bad gimme: %d\n", cxstack[cxix].blk_gimme);
4e35701f 908 case G_VOID:
909 return G_VOID;
54310121 910 }
e50aee73 911}
912
a0d0e21e 913static I32
8ac85365 914dopoptosub(I32 startingblock)
a0d0e21e 915{
11343788 916 dTHR;
a0d0e21e 917 I32 i;
c09156bb 918 register PERL_CONTEXT *cx;
a0d0e21e 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 927 return i;
928 }
929 }
930 return i;
931}
932
933static I32
8ac85365 934dopoptoeval(I32 startingblock)
a0d0e21e 935{
11343788 936 dTHR;
a0d0e21e 937 I32 i;
c09156bb 938 register PERL_CONTEXT *cx;
a0d0e21e 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 946 return i;
947 }
948 }
949 return i;
950}
951
952static I32
8ac85365 953dopoptoloop(I32 startingblock)
a0d0e21e 954{
11343788 955 dTHR;
a0d0e21e 956 I32 i;
c09156bb 957 register PERL_CONTEXT *cx;
a0d0e21e 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 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 979 return i;
980 }
981 }
982 return i;
983}
984
985void
8ac85365 986dounwind(I32 cxix)
a0d0e21e 987{
11343788 988 dTHR;
c09156bb 989 register PERL_CONTEXT *cx;
a0d0e21e 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 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 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 1012 break;
1013 }
c90c0ff4 1014 cxstack_ix--;
a0d0e21e 1015 }
1016}
1017
a0d0e21e 1018OP *
8ac85365 1019die_where(char *message)
a0d0e21e 1020{
11343788 1021 dTHR;
a0d0e21e 1022 if (in_eval) {
1023 I32 cxix;
c09156bb 1024 register PERL_CONTEXT *cx;
a0d0e21e 1025 I32 gimme;
1026 SV **newsp;
1027
4633a7c4 1028 if (in_eval & 4) {
1029 SV **svp;
1030 STRLEN klen = strlen(message);
1031
38a03e6e 1032 svp = hv_fetch(ERRHV, message, klen, TRUE);
4633a7c4 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 1038 SvGROW(ERRSV, SvCUR(ERRSV)+sizeof(prefix)+klen);
1039 sv_catpvn(ERRSV, prefix, sizeof(prefix)-1);
1040 sv_catpvn(ERRSV, message, klen);
4633a7c4 1041 }
1042 sv_inc(*svp);
1043 }
1044 }
1045 else
38a03e6e 1046 sv_setpv(ERRSV, message);
4633a7c4 1047
a0d0e21e 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 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 1070 DIE("%s", *msg ? msg : "Compilation failed in require");
1071 }
a0d0e21e 1072 return pop_return();
1073 }
1074 }
760ac839 1075 PerlIO_printf(PerlIO_stderr(), "%s",message);
1076 PerlIO_flush(PerlIO_stderr());
f86702cc 1077 my_failure_exit();
1078 /* NOTREACHED */
a0d0e21e 1079 return 0;
1080}
1081
1082PP(pp_xor)
1083{
4e35701f 1084 djSP; dPOPTOPssrl;
a0d0e21e 1085 if (SvTRUE(left) != SvTRUE(right))
1086 RETSETYES;
1087 else
1088 RETSETNO;
1089}
1090
1091PP(pp_andassign)
1092{
4e35701f 1093 djSP;
a0d0e21e 1094 if (!SvTRUE(TOPs))
1095 RETURN;
1096 else
1097 RETURNOP(cLOGOP->op_other);
1098}
1099
1100PP(pp_orassign)
1101{
4e35701f 1102 djSP;
a0d0e21e 1103 if (SvTRUE(TOPs))
1104 RETURN;
1105 else
1106 RETURNOP(cLOGOP->op_other);
1107}
1108
a0d0e21e 1109PP(pp_caller)
1110{
4e35701f 1111 djSP;
a0d0e21e 1112 register I32 cxix = dopoptosub(cxstack_ix);
c09156bb 1113 register PERL_CONTEXT *cx;
a0d0e21e 1114 I32 dbcxix;
54310121 1115 I32 gimme;
a0d0e21e 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 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 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 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 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 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 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 1182 }
1183 else if (cx->cx_type == CXt_SUB &&
1184 cx->blk_sub.hasargs &&
1185 curcop->cop_stash == debstash)
1186 {
a0d0e21e 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 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 1210 SV * const *str1 = (SV * const *)a;
1211 SV * const *str2 = (SV * const *)b;
748a9306 1212 I32 oldsaveix = savestack_ix;
a0d0e21e 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 1220 if (stack_sp != stack_base + 1)
1221 croak("Sort subroutine didn't return single value");
748a9306 1222 if (!SvNIOKp(*stack_sp))
a0d0e21e 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 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 1242}
1243
1244PP(pp_reset)
1245{
4e35701f 1246 djSP;
a0d0e21e 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;
c09156bb 1274 register PERL_CONTEXT *cx;
748a9306 1275 I32 gimme = G_ARRAY;
a0d0e21e 1276 I32 hasargs;
1277 GV *gv;
1278
a0d0e21e 1279 gv = DBgv;
1280 cv = GvCV(gv);
a0d0e21e 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 1287 ENTER;
1288 SAVETMPS;
1289
748a9306 1290 SAVEI32(debug);
55497cff 1291 SAVESTACK_POS();
748a9306 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 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;
c09156bb 1317 register PERL_CONTEXT *cx;
54310121 1318 I32 gimme = GIMME_V;
a0d0e21e 1319 SV **svp;
1320
4633a7c4 1321 ENTER;
1322 SAVETMPS;
1323
a0d0e21e 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 1329 SAVESPTR(*svp);
1330
a0d0e21e 1331 ENTER;
1332
1333 PUSHBLOCK(cx, CXt_LOOP, SP);
1334 PUSHLOOP(cx, svp, MARK);
44a8e56a 1335 if (op->op_flags & OPf_STACKED)
1336 cx->blk_loop.iterary = (AV*)SvREFCNT_inc(POPs);
4633a7c4 1337 else {
1ce6579f 1338 cx->blk_loop.iterary = curstack;
1339 AvFILL(curstack) = sp - stack_base;
4633a7c4 1340 cx->blk_loop.iterix = MARK - stack_base;
1341 }
a0d0e21e 1342
1343 RETURN;
1344}
1345
1346PP(pp_enterloop)
1347{
4e35701f 1348 djSP;
c09156bb 1349 register PERL_CONTEXT *cx;
54310121 1350 I32 gimme = GIMME_V;
a0d0e21e 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;
c09156bb 1365 register PERL_CONTEXT *cx;
f86702cc 1366 struct block_loop cxloop;
a0d0e21e 1367 I32 gimme;
1368 SV **newsp;
1369 PMOP *newpm;
1370 SV **mark;
1371
1372 POPBLOCK(cx,newpm);
4fdae800 1373 mark = newsp;
f86702cc 1374 POPLOOP1(cx); /* Delay POPLOOP2 until stack values are safe */
1375
a1f49e72 1376 TAINT_NOT;
54310121 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 1384 }
1385 else {
a1f49e72 1386 while (mark < SP) {
a0d0e21e 1387 *++newsp = sv_mortalcopy(*++mark);
a1f49e72 1388 TAINT_NOT; /* Each item is independent */
1389 }
a0d0e21e 1390 }
f86702cc 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 1397 LEAVE;
1398 LEAVE;
1399
f86702cc 1400 return NORMAL;
a0d0e21e 1401}
1402
1403PP(pp_return)
1404{
4e35701f 1405 djSP; dMARK;
a0d0e21e 1406 I32 cxix;
c09156bb 1407 register PERL_CONTEXT *cx;
f86702cc 1408 struct block_sub cxsub;
1409 bool popsub2 = FALSE;
a0d0e21e 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 1417 if (cxstack_ix > sortcxix)
1418 dounwind(sortcxix);
1ce6579f 1419 AvARRAY(curstack)[1] = *SP;
a0d0e21e 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 1434 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1435 popsub2 = TRUE;
a0d0e21e 1436 break;
1437 case CXt_EVAL:
1438 POPEVAL(cx);
748a9306 1439 if (optype == OP_REQUIRE &&
1440 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
1441 {
54310121 1442 /* Unassume the success we assumed earlier. */
748a9306 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 1447 break;
1448 default:
1449 DIE("panic: return");
a0d0e21e 1450 }
1451
a1f49e72 1452 TAINT_NOT;
a0d0e21e 1453 if (gimme == G_SCALAR) {
1454 if (MARK < SP)
f86702cc 1455 *++newsp = (popsub2 && SvTEMP(*SP))
1456 ? *SP : sv_mortalcopy(*SP);
a0d0e21e 1457 else
1458 *++newsp = &sv_undef;
a0d0e21e 1459 }
54310121 1460 else if (gimme == G_ARRAY) {
a1f49e72 1461 while (++MARK <= SP) {
f86702cc 1462 *++newsp = (popsub2 && SvTEMP(*MARK))
1463 ? *MARK : sv_mortalcopy(*MARK);
a1f49e72 1464 TAINT_NOT; /* Each item is independent */
1465 }
a0d0e21e 1466 }
a0d0e21e 1467 stack_sp = newsp;
1468
f86702cc 1469 /* Stack values are safe: */
1470 if (popsub2) {
1471 POPSUB2(); /* release CV and @_ ... */
1472 }
1473 curpm = newpm; /* ... and pop $1 et al */
1474
a0d0e21e 1475 LEAVE;
1476 return pop_return();
1477}
1478
1479PP(pp_last)
1480{
4e35701f 1481 djSP;
a0d0e21e 1482 I32 cxix;
c09156bb 1483 register PERL_CONTEXT *cx;
f86702cc 1484 struct block_loop cxloop;
1485 struct block_sub cxsub;
1486 I32 pop2 = 0;
a0d0e21e 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 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 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 1514 case CXt_SUB:
1515 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1516 pop2 = CXt_SUB;
a0d0e21e 1517 nextop = pop_return();
1518 break;
f86702cc 1519 case CXt_EVAL:
1520 POPEVAL(cx);
a0d0e21e 1521 nextop = pop_return();
1522 break;
1523 default:
1524 DIE("panic: last");
a0d0e21e 1525 }
1526
a1f49e72 1527 TAINT_NOT;
a0d0e21e 1528 if (gimme == G_SCALAR) {
f86702cc 1529 if (MARK < SP)
1530 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
1531 ? *SP : sv_mortalcopy(*SP);
a0d0e21e 1532 else
1533 *++newsp = &sv_undef;
1534 }
54310121 1535 else if (gimme == G_ARRAY) {
a1f49e72 1536 while (++MARK <= SP) {
f86702cc 1537 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
1538 ? *MARK : sv_mortalcopy(*MARK);
a1f49e72 1539 TAINT_NOT; /* Each item is independent */
1540 }
f86702cc 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 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 1556
1557 LEAVE;
f86702cc 1558 return nextop;
a0d0e21e 1559}
1560
1561PP(pp_next)
1562{
1563 I32 cxix;
c09156bb 1564 register PERL_CONTEXT *cx;
a0d0e21e 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;
c09156bb 1589 register PERL_CONTEXT *cx;
a0d0e21e 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 1615{
1616 OP *kid;
1617 OP **ops = opstack;
fc36a67e 1618 static char too_deep[] = "Target of goto is too deeply nested";
a0d0e21e 1619
fc36a67e 1620 if (ops >= oplimit)
1621 croak(too_deep);
11343788 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 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 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 1640 if (kid == lastgotoprobe)
1641 continue;
fc36a67e 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 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 1664 OP *retop = 0;
1665 I32 ix;
c09156bb 1666 register PERL_CONTEXT *cx;
fc36a67e 1667#define GOTO_DEPTH 64
1668 OP *enterops[GOTO_DEPTH];
a0d0e21e 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;
c09156bb 1679 register PERL_CONTEXT *cx;
a0d0e21e 1680 CV* cv = (CV*)SvRV(sv);
1681 SV** mark;
1682 I32 items = 0;
1683 I32 oldsave;
1684
4aa0a1f7 1685 if (!CvROOT(cv) && !CvXSUB(cv)) {
1686 if (CvGV(cv)) {
1687 SV *tmpstr = sv_newmortal();
e5cf08de 1688 gv_efullname3(tmpstr, CvGV(cv), Nullch);
4aa0a1f7 1689 DIE("Goto undefined subroutine &%s",SvPVX(tmpstr));
1690 }
1691 DIE("Goto undefined subroutine");
1692 }
1693
a0d0e21e 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 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 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 1727 while (sp > mark) {
1728 sp[1] = sp[0];
1729 sp--;
1730 }
ecfc5424 1731 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
1732 items = (*fp3)(CvXSUBANY(cv).any_i32,
1733 mark - stack_base + 1,
1734 items);
a0d0e21e 1735 sp = stack_base + items;
1736 }
1737 else {
1ce6579f 1738 stack_sp--; /* There is no cv arg. */
a0d0e21e 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 1755 if (CvDEPTH(cv) > AvFILL(padlist)) {
1756 AV *newpad = newAV();
4aa0a1f7 1757 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
a0d0e21e 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 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 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 1779 }
1780 else {
748a9306 1781 av_store(newpad, ix, sv = NEWSV(0,0));
a0d0e21e 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 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 1810 SAVESPTR(curpad);
1811 curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
6d4ff0d2 1812#ifndef USE_THREADS
1813 if (cx->blk_sub.hasargs)
1814#endif /* USE_THREADS */
1815 {
a0d0e21e 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 1822#endif /* USE_THREADS */
1823 cx->blk_sub.argarray = av;
a0d0e21e 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 1849 /*
1850 * We do not care about using sv to call CV;
1851 * it's for informational purposes only.
1852 */
1ce6579f 1853 SV *sv = GvSV(DBsub);
1854 save_item(sv);
e5cf08de 1855 gv_efullname3(sv, CvGV(cv), Nullch);
1ce6579f 1856 }
a0d0e21e 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 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 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 1900 case CXt_NULL:
1901 DIE("Can't \"goto\" outside a block");
a0d0e21e 1902 default:
1903 if (ix)
1904 DIE("panic: goto");
68dc0745 1905 gotoprobe = main_root;
a0d0e21e 1906 break;
1907 }
fc36a67e 1908 retop = dofindlabel(gotoprobe, label,
1909 enterops, enterops + GOTO_DEPTH);
a0d0e21e 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 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 1942 }
1943 op = oldop;
1944 }
1945 }
1946
1947 if (do_dump) {
a5f75d66 1948#ifdef VMS
1949 if (!retop) retop = main_start;
1950#endif
a0d0e21e 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 1963 }
1964
a0d0e21e 1965 RETURNOP(retop);
1966}
1967
1968PP(pp_exit)
1969{
4e35701f 1970 djSP;
a0d0e21e 1971 I32 anum;
1972
1973 if (MAXARG < 1)
1974 anum = 0;
ff0cee69 1975 else {
a0d0e21e 1976 anum = SvIVx(POPs);
ff0cee69 1977#ifdef VMSISH_EXIT
1978 if (anum == 1 && VMSISH_EXIT)
1979 anum = 0;
1980#endif
1981 }
a0d0e21e 1982 my_exit(anum);
1983 PUSHs(&sv_undef);
1984 RETURN;
1985}
1986
1987#ifdef NOTYET
1988PP(pp_nswitch)
1989{
4e35701f 1990 djSP;
a0d0e21e 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 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 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;
1e422769 2058 OP *oldop = op;
54310121 2059 dJMPENV;
1e422769 2060
2061 op = o;
1e422769 2062#ifdef DEBUGGING
54310121 2063 assert(CATCH_GET == TRUE);
7c06b590 2064 DEBUG_l(deb("Setting up local jumplevel %p, was %p\n", &cur_env, top_env));
1e422769 2065#endif
22921e25 2066 JMPENV_PUSH(ret);
2067 switch (ret) {
1e422769 2068 default: /* topmost level handles it */
54310121 2069 JMPENV_POP;
1e422769 2070 op = oldop;
54310121 2071 JMPENV_JUMP(ret);
1e422769 2072 /* NOTREACHED */
2073 case 3:
2074 if (!restartop) {
2075 PerlIO_printf(PerlIO_stderr(), "panic: restartop\n");
2076 break;
2077 }
1e422769 2078 op = restartop;
2079 restartop = 0;
2080 /* FALL THROUGH */
2081 case 0:
2082 runops();
2083 break;
2084 }
54310121 2085 JMPENV_POP;
1e422769 2086 op = oldop;
2087 return Nullop;
2088}
2089
0f15f207 2090/* With USE_THREADS, eval_owner must be held on entry to doeval */
1e422769 2091static OP *
8ac85365 2092doeval(int gimme)
a0d0e21e 2093{
2094 dSP;
2095 OP *saveop = op;
2096 HV *newstash;
ff3ff8d1 2097 CV *caller;
748a9306 2098 AV* comppadlist;
a0d0e21e 2099
2100 in_eval = 1;
2101
1ce6579f 2102 PUSHMARK(SP);
2103
a0d0e21e 2104 /* set up a scratch pad */
2105
55497cff 2106 SAVEI32(padix);
a0d0e21e 2107 SAVESPTR(curpad);
2108 SAVESPTR(comppad);
2109 SAVESPTR(comppad_name);
55497cff 2110 SAVEI32(comppad_name_fill);
2111 SAVEI32(min_intro_pending);
2112 SAVEI32(max_intro_pending);
748a9306 2113
ff3ff8d1 2114 caller = compcv;
748a9306 2115 SAVESPTR(compcv);
2116 compcv = (CV*)NEWSV(1104,0);
2117 sv_upgrade((SV *)compcv, SVt_PVCV);
07055b4c 2118 CvUNIQUE_on(compcv);
11343788 2119#ifdef USE_THREADS
2120 CvOWNER(compcv) = 0;
12ca11f6 2121 New(666, CvMUTEXP(compcv), 1, perl_mutex);
11343788 2122 MUTEX_INIT(CvMUTEXP(compcv));
11343788 2123#endif /* USE_THREADS */
748a9306 2124
a0d0e21e 2125 comppad = newAV();
6d4ff0d2 2126 av_push(comppad, Nullsv);
2127 curpad = AvARRAY(comppad);
a0d0e21e 2128 comppad_name = newAV();
2129 comppad_name_fill = 0;
6d4ff0d2 2130 min_intro_pending = 0;
2131 padix = 0;
11343788 2132#ifdef USE_THREADS
2133 av_store(comppad_name, 0, newSVpv("@_", 2));
6d4ff0d2 2134 curpad[0] = (SV*)newAV();
2135 SvPADMY_on(curpad[0]); /* XXX Needed? */
11343788 2136#endif /* USE_THREADS */
a0d0e21e 2137
748a9306 2138 comppadlist = newAV();
2139 AvREAL_off(comppadlist);
8e07c86e 2140 av_store(comppadlist, 0, (SV*)comppad_name);
2141 av_store(comppadlist, 1, (SV*)comppad);
748a9306 2142 CvPADLIST(compcv) = comppadlist;
2c05e328 2143
2144 if (saveop->op_type != OP_REQUIRE)
199100c8 2145 CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc(caller);
07055b4c 2146
8e07c86e 2147 SAVEFREESV(compcv);
748a9306 2148
a0d0e21e 2149 /* make sure we compile in the right package */
2150
2151 newstash = curcop->cop_stash;
2152 if (curstash != newstash) {
2153 SAVESPTR(curstash);
2154 curstash = newstash;
2155 }
2156 SAVESPTR(beginav);
2157 beginav = newAV();
2158 SAVEFREESV(beginav);
2159
2160 /* try to compile it */
2161
2162 eval_root = Nullop;
2163 error_count = 0;
2164 curcop = &compiling;
2165 curcop->cop_arybase = 0;
c07a80fd 2166 SvREFCNT_dec(rs);
2167 rs = newSVpv("\n", 1);
1ce6579f 2168 if (saveop->op_flags & OPf_SPECIAL)
2169 in_eval |= 4;
2170 else
38a03e6e 2171 sv_setpv(ERRSV,"");
a0d0e21e 2172 if (yyparse() || error_count || !eval_root) {
2173 SV **newsp;
2174 I32 gimme;
c09156bb 2175 PERL_CONTEXT *cx;
a0d0e21e 2176 I32 optype;
2177
2178 op = saveop;
2179 if (eval_root) {
2180 op_free(eval_root);
2181 eval_root = Nullop;
2182 }
1ce6579f 2183 SP = stack_base + POPMARK; /* pop original mark */
a0d0e21e 2184 POPBLOCK(cx,curpm);
2185 POPEVAL(cx);
2186 pop_return();
2187 lex_end();
2188 LEAVE;
7a2e2cd6 2189 if (optype == OP_REQUIRE) {
38a03e6e 2190 char* msg = SvPVx(ERRSV, na);
7a2e2cd6 2191 DIE("%s", *msg ? msg : "Compilation failed in require");
2192 }
c07a80fd 2193 SvREFCNT_dec(rs);
2194 rs = SvREFCNT_inc(nrs);
f2134d95 2195#ifdef USE_THREADS
2196 MUTEX_LOCK(&eval_mutex);
2197 eval_owner = 0;
2198 COND_SIGNAL(&eval_cond);
2199 MUTEX_UNLOCK(&eval_mutex);
2200#endif /* USE_THREADS */
a0d0e21e 2201 RETPUSHUNDEF;
2202 }
c07a80fd 2203 SvREFCNT_dec(rs);
2204 rs = SvREFCNT_inc(nrs);
a0d0e21e 2205 compiling.cop_line = 0;
a0d0e21e 2206 SAVEFREEOP(eval_root);
54310121 2207 if (gimme & G_VOID)
2208 scalarvoid(eval_root);
2209 else if (gimme & G_ARRAY)
a0d0e21e 2210 list(eval_root);
2211 else
2212 scalar(eval_root);
2213
2214 DEBUG_x(dump_eval());
2215
55497cff 2216 /* Register with debugger: */
84902520 2217 if (PERLDB_INTER && saveop->op_type == OP_REQUIRE) {
55497cff 2218 CV *cv = perl_get_cv("DB::postponed", FALSE);
55497cff 2219 if (cv) {
2220 dSP;
2221 PUSHMARK(sp);
2222 XPUSHs((SV*)compiling.cop_filegv);
2223 PUTBACK;
2224 perl_call_sv((SV*)cv, G_DISCARD);
2225 }
2226 }
2227
a0d0e21e 2228 /* compiled okay, so do it */
2229
4fdae800 2230 CvDEPTH(compcv) = 1;
1ce6579f 2231 SP = stack_base + POPMARK; /* pop original mark */
77fbf95b 2232 op = saveop; /* The caller may need it. */
b35b2403 2233#ifdef USE_THREADS
11343788 2234 MUTEX_LOCK(&eval_mutex);
2235 eval_owner = 0;
2236 COND_SIGNAL(&eval_cond);
2237 MUTEX_UNLOCK(&eval_mutex);
b35b2403 2238#endif /* USE_THREADS */
5dc0d613 2239
a0d0e21e 2240 RETURNOP(eval_start);
2241}
2242
2243PP(pp_require)
2244{
4e35701f 2245 djSP;
c09156bb 2246 register PERL_CONTEXT *cx;
a0d0e21e 2247 SV *sv;
2248 char *name;
46fc3d4c 2249 char *tryname;
2250 SV *namesv = Nullsv;
a0d0e21e 2251 SV** svp;
2252 I32 gimme = G_SCALAR;
760ac839 2253 PerlIO *tryrsfp = 0;
a0d0e21e 2254
2255 sv = POPs;
4633a7c4 2256 if (SvNIOKp(sv) && !SvPOKp(sv)) {
36477c24 2257 SET_NUMERIC_STANDARD();
a5f75d66 2258 if (atof(patchlevel) + 0.00000999 < SvNV(sv))
2259 DIE("Perl %s required--this is only version %s, stopped",
2260 SvPV(sv,na),patchlevel);
a0d0e21e 2261 RETPUSHYES;
2262 }
2263 name = SvPV(sv, na);
2264 if (!*name)
2265 DIE("Null filename used");
4633a7c4 2266 TAINT_PROPER("require");
a0d0e21e 2267 if (op->op_type == OP_REQUIRE &&
2268 (svp = hv_fetch(GvHVn(incgv), name, SvCUR(sv), 0)) &&
2269 *svp != &sv_undef)
2270 RETPUSHYES;
2271
2272 /* prepare to compile file */
2273
46fc3d4c 2274 if (*name == '/' ||
2275 (*name == '.' &&
2276 (name[1] == '/' ||
2277 (name[1] == '.' && name[2] == '/')))
4633a7c4 2278#ifdef DOSISH
46fc3d4c 2279 || (name[0] && name[1] == ':')
4633a7c4 2280#endif
ba42ef2f 2281#ifdef WIN32
2282 || (name[0] == '\\' && name[1] == '\\') /* UNC path */
2283#endif
748a9306 2284#ifdef VMS
46fc3d4c 2285 || (strchr(name,':') || ((*name == '[' || *name == '<') &&
2286 (isALNUM(name[1]) || strchr("$-_]>",name[1]))))
748a9306 2287#endif
2288 )
a0d0e21e 2289 {
46fc3d4c 2290 tryname = name;
2291 tryrsfp = PerlIO_open(name,"r");
a0d0e21e 2292 }
2293 else {
2294 AV *ar = GvAVn(incgv);
2295 I32 i;
748a9306 2296#ifdef VMS
46fc3d4c 2297 char *unixname;
2298 if ((unixname = tounixspec(name, Nullch)) != Nullch)
2299#endif
2300 {
2301 namesv = NEWSV(806, 0);
2302 for (i = 0; i <= AvFILL(ar); i++) {
2303 char *dir = SvPVx(*av_fetch(ar, i, TRUE), na);
2304#ifdef VMS
2305 char *unixdir;
2306 if ((unixdir = tounixpath(dir, Nullch)) == Nullch)
2307 continue;
2308 sv_setpv(namesv, unixdir);
2309 sv_catpv(namesv, unixname);
748a9306 2310#else
46fc3d4c 2311 sv_setpvf(namesv, "%s/%s", dir, name);
748a9306 2312#endif
46fc3d4c 2313 tryname = SvPVX(namesv);
2314 tryrsfp = PerlIO_open(tryname, "r");
2315 if (tryrsfp) {
2316 if (tryname[0] == '.' && tryname[1] == '/')
2317 tryname += 2;
2318 break;
2319 }
a0d0e21e 2320 }
2321 }
2322 }
2323 SAVESPTR(compiling.cop_filegv);
46fc3d4c 2324 compiling.cop_filegv = gv_fetchfile(tryrsfp ? tryname : name);
2325 SvREFCNT_dec(namesv);
a0d0e21e 2326 if (!tryrsfp) {
2327 if (op->op_type == OP_REQUIRE) {
46fc3d4c 2328 SV *msg = sv_2mortal(newSVpvf("Can't locate %s in @INC", name));
2683423c 2329 SV *dirmsgsv = NEWSV(0, 0);
2330 AV *ar = GvAVn(incgv);
2331 I32 i;
46fc3d4c 2332 if (instr(SvPVX(msg), ".h "))
2333 sv_catpv(msg, " (change .h to .ph maybe?)");
2334 if (instr(SvPVX(msg), ".ph "))
2335 sv_catpv(msg, " (did you run h2ph?)");
3e3baf6d 2336 sv_catpv(msg, " (@INC contains:");
2683423c 2337 for (i = 0; i <= AvFILL(ar); i++) {
2338 char *dir = SvPVx(*av_fetch(ar, i, TRUE), na);
3e3baf6d 2339 sv_setpvf(dirmsgsv, " %s", dir);
2683423c 2340 sv_catsv(msg, dirmsgsv);
2341 }
3e3baf6d 2342 sv_catpvn(msg, ")", 1);
2683423c 2343 SvREFCNT_dec(dirmsgsv);
fc36a67e 2344 DIE("%_", msg);
a0d0e21e 2345 }
2346
2347 RETPUSHUNDEF;
2348 }
2349
2350 /* Assume success here to prevent recursive requirement. */
2351 (void)hv_store(GvHVn(incgv), name, strlen(name),
2352 newSVsv(GvSV(compiling.cop_filegv)), 0 );
2353
2354 ENTER;
2355 SAVETMPS;
2356 lex_start(sv_2mortal(newSVpv("",0)));
e50aee73 2357 if (rsfp_filters){
2358 save_aptr(&rsfp_filters);
2359 rsfp_filters = NULL;
2360 }
2361
a0d0e21e 2362 rsfp = tryrsfp;
2363 name = savepv(name);
2364 SAVEFREEPV(name);
2365 SAVEI32(hints);
2366 hints = 0;
2367
2368 /* switch to eval mode */
2369
2370 push_return(op->op_next);
2371 PUSHBLOCK(cx, CXt_EVAL, SP);
2372 PUSHEVAL(cx, name, compiling.cop_filegv);
2373
2374 compiling.cop_line = 0;
2375
2376 PUTBACK;
0f15f207 2377#ifdef USE_THREADS
2378 MUTEX_LOCK(&eval_mutex);
2379 if (eval_owner && eval_owner != thr)
2380 while (eval_owner)
2381 COND_WAIT(&eval_cond, &eval_mutex);
2382 eval_owner = thr;
2383 MUTEX_UNLOCK(&eval_mutex);
2384#endif /* USE_THREADS */
1e422769 2385 return DOCATCH(doeval(G_SCALAR));
a0d0e21e 2386}
2387
2388PP(pp_dofile)
2389{
2390 return pp_require(ARGS);
2391}
2392
2393PP(pp_entereval)
2394{
4e35701f 2395 djSP;
c09156bb 2396 register PERL_CONTEXT *cx;
a0d0e21e 2397 dPOPss;
54310121 2398 I32 gimme = GIMME_V, was = sub_generation;
fc36a67e 2399 char tmpbuf[TYPE_DIGITS(long) + 12];
2400 char *safestr;
a0d0e21e 2401 STRLEN len;
55497cff 2402 OP *ret;
a0d0e21e 2403
2404 if (!SvPV(sv,len) || !len)
2405 RETPUSHUNDEF;
748a9306 2406 TAINT_PROPER("eval");
a0d0e21e 2407
2408 ENTER;
a0d0e21e 2409 lex_start(sv);
748a9306 2410 SAVETMPS;
a0d0e21e 2411
2412 /* switch to eval mode */
2413
748a9306 2414 SAVESPTR(compiling.cop_filegv);
ff0cee69 2415 sprintf(tmpbuf, "_<(eval %lu)", (unsigned long)++evalseq);
a0d0e21e 2416 compiling.cop_filegv = gv_fetchfile(tmpbuf+2);
2417 compiling.cop_line = 1;
55497cff 2418 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
2419 deleting the eval's FILEGV from the stash before gv_check() runs
2420 (i.e. before run-time proper). To work around the coredump that
2421 ensues, we always turn GvMULTI_on for any globals that were
2422 introduced within evals. See force_ident(). GSAR 96-10-12 */
2423 safestr = savepv(tmpbuf);
2424 SAVEDELETE(defstash, safestr, strlen(safestr));
a0d0e21e 2425 SAVEI32(hints);
2426 hints = op->op_targ;
2427
2428 push_return(op->op_next);
2429 PUSHBLOCK(cx, CXt_EVAL, SP);
2430 PUSHEVAL(cx, 0, compiling.cop_filegv);
2431
2432 /* prepare to compile string */
2433
08ea043f 2434 if (PERLDB_LINE && curstash != debstash)
a0d0e21e 2435 save_lines(GvAV(compiling.cop_filegv), linestr);
2436 PUTBACK;
0f15f207 2437#ifdef USE_THREADS
2438 MUTEX_LOCK(&eval_mutex);
2439 if (eval_owner && eval_owner != thr)
2440 while (eval_owner)
2441 COND_WAIT(&eval_cond, &eval_mutex);
2442 eval_owner = thr;
2443 MUTEX_UNLOCK(&eval_mutex);
2444#endif /* USE_THREADS */
55497cff 2445 ret = doeval(gimme);
08ea043f 2446 if (PERLDB_INTER && was != sub_generation /* Some subs defined here. */
e506e776 2447 && ret != op->op_next) { /* Successive compilation. */
55497cff 2448 strcpy(safestr, "_<(eval )"); /* Anything fake and short. */
2449 }
1e422769 2450 return DOCATCH(ret);
a0d0e21e 2451}
2452
2453PP(pp_leaveeval)
2454{
4e35701f 2455 djSP;
a0d0e21e 2456 register SV **mark;
2457 SV **newsp;
2458 PMOP *newpm;
2459 I32 gimme;
c09156bb 2460 register PERL_CONTEXT *cx;
a0d0e21e 2461 OP *retop;
760ac839 2462 U8 save_flags = op -> op_flags;
a0d0e21e 2463 I32 optype;
2464
2465 POPBLOCK(cx,newpm);
2466 POPEVAL(cx);
2467 retop = pop_return();
2468
a1f49e72 2469 TAINT_NOT;
54310121 2470 if (gimme == G_VOID)
2471 MARK = newsp;
2472 else if (gimme == G_SCALAR) {
2473 MARK = newsp + 1;
2474 if (MARK <= SP) {
2475 if (SvFLAGS(TOPs) & SVs_TEMP)
2476 *MARK = TOPs;
2477 else
2478 *MARK = sv_mortalcopy(TOPs);
2479 }
a0d0e21e 2480 else {
54310121 2481 MEXTEND(mark,0);
2482 *MARK = &sv_undef;
a0d0e21e 2483 }
a0d0e21e 2484 }
2485 else {
a1f49e72 2486 /* in case LEAVE wipes old return values */
2487 for (mark = newsp + 1; mark <= SP; mark++) {
2488 if (!(SvFLAGS(*mark) & SVs_TEMP)) {
a0d0e21e 2489 *mark = sv_mortalcopy(*mark);
a1f49e72 2490 TAINT_NOT; /* Each item is independent */
2491 }
2492 }
a0d0e21e 2493 }
2494 curpm = newpm; /* Don't pop $1 et al till now */
2495
84902520 2496 /*
2497 * Closures mentioned at top level of eval cannot be referenced
2498 * again, and their presence indirectly causes a memory leak.
2499 * (Note that the fact that compcv and friends are still set here
2500 * is, AFAIK, an accident.) --Chip
2501 */
2502 if (AvFILL(comppad_name) >= 0) {
2503 SV **svp = AvARRAY(comppad_name);
2504 I32 ix;
2505 for (ix = AvFILL(comppad_name); ix >= 0; ix--) {
2506 SV *sv = svp[ix];
2507 if (sv && sv != &sv_undef && *SvPVX(sv) == '&') {
2508 SvREFCNT_dec(sv);
2509 svp[ix] = &sv_undef;
2510
2511 sv = curpad[ix];
2512 if (CvCLONE(sv)) {
2513 SvREFCNT_dec(CvOUTSIDE(sv));
2514 CvOUTSIDE(sv) = Nullcv;
2515 }
2516 else {
2517 SvREFCNT_dec(sv);
2518 sv = NEWSV(0,0);
2519 SvPADTMP_on(sv);
2520 curpad[ix] = sv;
2521 }
2522 }
2523 }
2524 }
2525
4fdae800 2526#ifdef DEBUGGING
2527 assert(CvDEPTH(compcv) == 1);
2528#endif
2529 CvDEPTH(compcv) = 0;
2530
1ce6579f 2531 if (optype == OP_REQUIRE &&
54310121 2532 !(gimme == G_SCALAR ? SvTRUE(*sp) : sp > newsp))
2533 {
1ce6579f 2534 /* Unassume the success we assumed earlier. */
54310121 2535 char *name = cx->blk_eval.old_name;
1ce6579f 2536 (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
2537 retop = die("%s did not return a true value", name);
a0d0e21e 2538 }
2539
2540 lex_end();
2541 LEAVE;
4fdae800 2542
760ac839 2543 if (!(save_flags & OPf_SPECIAL))
38a03e6e 2544 sv_setpv(ERRSV,"");
a0d0e21e 2545
2546 RETURNOP(retop);
2547}
2548
a0d0e21e 2549PP(pp_entertry)
2550{
4e35701f 2551 djSP;
c09156bb 2552 register PERL_CONTEXT *cx;
54310121 2553 I32 gimme = GIMME_V;
a0d0e21e 2554
2555 ENTER;
2556 SAVETMPS;
2557
2558 push_return(cLOGOP->op_other->op_next);
2559 PUSHBLOCK(cx, CXt_EVAL, SP);
2560 PUSHEVAL(cx, 0, 0);
2561 eval_root = op; /* Only needed so that goto works right. */
2562
2563 in_eval = 1;
38a03e6e 2564 sv_setpv(ERRSV,"");
1e422769 2565 PUTBACK;
2566 return DOCATCH(op->op_next);
a0d0e21e 2567}
2568
2569PP(pp_leavetry)
2570{
4e35701f 2571 djSP;
a0d0e21e 2572 register SV **mark;
2573 SV **newsp;
2574 PMOP *newpm;
2575 I32 gimme;
c09156bb 2576 register PERL_CONTEXT *cx;
a0d0e21e 2577 I32 optype;
2578
2579 POPBLOCK(cx,newpm);
2580 POPEVAL(cx);
2581 pop_return();
2582
a1f49e72 2583 TAINT_NOT;
54310121 2584 if (gimme == G_VOID)
2585 SP = newsp;
2586 else if (gimme == G_SCALAR) {
2587 MARK = newsp + 1;
2588 if (MARK <= SP) {
2589 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
2590 *MARK = TOPs;
2591 else
2592 *MARK = sv_mortalcopy(TOPs);
2593 }
a0d0e21e 2594 else {
54310121 2595 MEXTEND(mark,0);
2596 *MARK = &sv_undef;
a0d0e21e 2597 }
2598 SP = MARK;
2599 }
2600 else {
a1f49e72 2601 /* in case LEAVE wipes old return values */
2602 for (mark = newsp + 1; mark <= SP; mark++) {
2603 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
a0d0e21e 2604 *mark = sv_mortalcopy(*mark);
a1f49e72 2605 TAINT_NOT; /* Each item is independent */
2606 }
2607 }
a0d0e21e 2608 }
2609 curpm = newpm; /* Don't pop $1 et al till now */
2610
2611 LEAVE;
38a03e6e 2612 sv_setpv(ERRSV,"");
a0d0e21e 2613 RETURN;
2614}
2615
2616static void
8ac85365 2617doparseform(SV *sv)
a0d0e21e 2618{
2619 STRLEN len;
2620 register char *s = SvPV_force(sv, len);
2621 register char *send = s + len;
2622 register char *base;
2623 register I32 skipspaces = 0;
2624 bool noblank;
2625 bool repeat;
2626 bool postspace = FALSE;
2627 U16 *fops;
2628 register U16 *fpc;
2629 U16 *linepc;
2630 register I32 arg;
2631 bool ischop;
2632
55497cff 2633 if (len == 0)
bbce6d69 2634 croak("Null picture in formline");
55497cff 2635
2636 New(804, fops, (send - s)*3+10, U16); /* Almost certainly too long... */
a0d0e21e 2637 fpc = fops;
2638
2639 if (s < send) {
2640 linepc = fpc;
2641 *fpc++ = FF_LINEMARK;
2642 noblank = repeat = FALSE;
2643 base = s;
2644 }
2645
2646 while (s <= send) {
2647 switch (*s++) {
2648 default:
2649 skipspaces = 0;
2650 continue;
2651
2652 case '~':
2653 if (*s == '~') {
2654 repeat = TRUE;
2655 *s = ' ';
2656 }
2657 noblank = TRUE;
2658 s[-1] = ' ';
2659 /* FALL THROUGH */
2660 case ' ': case '\t':
2661 skipspaces++;
2662 continue;
2663
2664 case '\n': case 0:
2665 arg = s - base;
2666 skipspaces++;
2667 arg -= skipspaces;
2668 if (arg) {
5f05dabc 2669 if (postspace)
a0d0e21e 2670 *fpc++ = FF_SPACE;
a0d0e21e 2671 *fpc++ = FF_LITERAL;
2672 *fpc++ = arg;
2673 }
5f05dabc 2674 postspace = FALSE;
a0d0e21e 2675 if (s <= send)
2676 skipspaces--;
2677 if (skipspaces) {
2678 *fpc++ = FF_SKIP;
2679 *fpc++ = skipspaces;
2680 }
2681 skipspaces = 0;
2682 if (s <= send)
2683 *fpc++ = FF_NEWLINE;
2684 if (noblank) {
2685 *fpc++ = FF_BLANK;
2686 if (repeat)
2687 arg = fpc - linepc + 1;
2688 else
2689 arg = 0;
2690 *fpc++ = arg;
2691 }
2692 if (s < send) {
2693 linepc = fpc;
2694 *fpc++ = FF_LINEMARK;
2695 noblank = repeat = FALSE;
2696 base = s;
2697 }
2698 else
2699 s++;
2700 continue;
2701
2702 case '@':
2703 case '^':
2704 ischop = s[-1] == '^';
2705
2706 if (postspace) {
2707 *fpc++ = FF_SPACE;
2708 postspace = FALSE;
2709 }
2710 arg = (s - base) - 1;
2711 if (arg) {
2712 *fpc++ = FF_LITERAL;
2713 *fpc++ = arg;
2714 }
2715
2716 base = s - 1;
2717 *fpc++ = FF_FETCH;
2718 if (*s == '*') {
2719 s++;
2720 *fpc++ = 0;
2721 *fpc++ = FF_LINEGLOB;
2722 }
2723 else if (*s == '#' || (*s == '.' && s[1] == '#')) {
2724 arg = ischop ? 512 : 0;
2725 base = s - 1;
2726 while (*s == '#')
2727 s++;
2728 if (*s == '.') {
2729 char *f;
2730 s++;
2731 f = s;
2732 while (*s == '#')
2733 s++;
2734 arg |= 256 + (s - f);
2735 }
2736 *fpc++ = s - base; /* fieldsize for FETCH */
2737 *fpc++ = FF_DECIMAL;
2738 *fpc++ = arg;
2739 }
2740 else {
2741 I32 prespace = 0;
2742 bool ismore = FALSE;
2743
2744 if (*s == '>') {
2745 while (*++s == '>') ;
2746 prespace = FF_SPACE;
2747 }
2748 else if (*s == '|') {
2749 while (*++s == '|') ;
2750 prespace = FF_HALFSPACE;
2751 postspace = TRUE;
2752 }
2753 else {
2754 if (*s == '<')
2755 while (*++s == '<') ;
2756 postspace = TRUE;
2757 }
2758 if (*s == '.' && s[1] == '.' && s[2] == '.') {
2759 s += 3;
2760 ismore = TRUE;
2761 }
2762 *fpc++ = s - base; /* fieldsize for FETCH */
2763
2764 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
2765
2766 if (prespace)
2767 *fpc++ = prespace;
2768 *fpc++ = FF_ITEM;
2769 if (ismore)
2770 *fpc++ = FF_MORE;
2771 if (ischop)
2772 *fpc++ = FF_CHOP;
2773 }
2774 base = s;
2775 skipspaces = 0;
2776 continue;
2777 }
2778 }
2779 *fpc++ = FF_END;
2780
2781 arg = fpc - fops;
2782 { /* need to jump to the next word */
2783 int z;
2784 z = WORD_ALIGN - SvCUR(sv) % WORD_ALIGN;
2785 SvGROW(sv, SvCUR(sv) + z + arg * sizeof(U16) + 4);
2786 s = SvPVX(sv) + SvCUR(sv) + z;
2787 }
2788 Copy(fops, s, arg, U16);
2789 Safefree(fops);
55497cff 2790 sv_magic(sv, Nullsv, 'f', Nullch, 0);
a0d0e21e 2791 SvCOMPILED_on(sv);
2792}
4e35701f 2793