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