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