This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
This is my patch patch.1k for perl5.001.
[perl5.git] / pp_ctl.c
CommitLineData
a0d0e21e
LW
1/* pp_ctl.c
2 *
3 * Copyright (c) 1991-1994, Larry Wall
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
26static OP *doeval _((int gimme));
27static OP *dofindlabel _((OP *op, char *label, OP **opstack));
28static void doparseform _((SV *sv));
29static I32 dopoptoeval _((I32 startingblock));
30static I32 dopoptolabel _((char *label));
31static I32 dopoptoloop _((I32 startingblock));
32static I32 dopoptosub _((I32 startingblock));
33static void save_lines _((AV *array, SV *sv));
34static int sortcmp _((const void *, const void *));
35static int sortcv _((const void *, const void *));
36
37static I32 sortcxix;
38
39PP(pp_wantarray)
40{
41 dSP;
42 I32 cxix;
43 EXTEND(SP, 1);
44
45 cxix = dopoptosub(cxstack_ix);
46 if (cxix < 0)
47 RETPUSHUNDEF;
48
49 if (cxstack[cxix].blk_gimme == G_ARRAY)
50 RETPUSHYES;
51 else
52 RETPUSHNO;
53}
54
55PP(pp_regcmaybe)
56{
57 return NORMAL;
58}
59
60PP(pp_regcomp) {
61 dSP;
62 register PMOP *pm = (PMOP*)cLOGOP->op_other;
63 register char *t;
64 SV *tmpstr;
65 STRLEN len;
66
67 tmpstr = POPs;
68 t = SvPV(tmpstr, len);
69
70 if (pm->op_pmregexp) {
71 regfree(pm->op_pmregexp);
72 pm->op_pmregexp = Null(REGEXP*); /* crucial if regcomp aborts */
73 }
74
75 pm->op_pmregexp = regcomp(t, t + len, pm);
76
77 if (!pm->op_pmregexp->prelen && curpm)
78 pm = curpm;
79 else if (strEQ("\\s+", pm->op_pmregexp->precomp))
80 pm->op_pmflags |= PMf_WHITE;
81
82 if (pm->op_pmflags & PMf_KEEP) {
a0d0e21e
LW
83 pm->op_pmflags &= ~PMf_RUNTIME; /* no point compiling again */
84 hoistmust(pm);
85 cLOGOP->op_first->op_next = op->op_next;
a0d0e21e
LW
86 }
87 RETURN;
88}
89
90PP(pp_substcont)
91{
92 dSP;
93 register PMOP *pm = (PMOP*) cLOGOP->op_other;
94 register CONTEXT *cx = &cxstack[cxstack_ix];
95 register SV *dstr = cx->sb_dstr;
96 register char *s = cx->sb_s;
97 register char *m = cx->sb_m;
98 char *orig = cx->sb_orig;
99 register REGEXP *rx = pm->op_pmregexp;
100
101 if (cx->sb_iters++) {
102 if (cx->sb_iters > cx->sb_maxiters)
103 DIE("Substitution loop");
104
105 sv_catsv(dstr, POPs);
106 if (rx->subbase)
107 Safefree(rx->subbase);
108 rx->subbase = cx->sb_subbase;
109
110 /* Are we done */
111 if (cx->sb_once || !regexec(rx, s, cx->sb_strend, orig,
112 s == m, Nullsv, cx->sb_safebase))
113 {
114 SV *targ = cx->sb_targ;
115 sv_catpvn(dstr, s, cx->sb_strend - s);
748a9306 116
cb0b1708 117 Safefree(SvPVX(targ));
748a9306
LW
118 SvPVX(targ) = SvPVX(dstr);
119 SvCUR_set(targ, SvCUR(dstr));
120 SvLEN_set(targ, SvLEN(dstr));
121 SvPVX(dstr) = 0;
122 sv_free(dstr);
123
a0d0e21e
LW
124 (void)SvPOK_only(targ);
125 SvSETMAGIC(targ);
126 PUSHs(sv_2mortal(newSViv((I32)cx->sb_iters - 1)));
127 POPSUBST(cx);
128 RETURNOP(pm->op_next);
129 }
130 }
131 if (rx->subbase && rx->subbase != orig) {
132 m = s;
133 s = orig;
134 cx->sb_orig = orig = rx->subbase;
135 s = orig + (m - s);
136 cx->sb_strend = s + (cx->sb_strend - m);
137 }
138 cx->sb_m = m = rx->startp[0];
139 sv_catpvn(dstr, s, m-s);
140 cx->sb_s = rx->endp[0];
141 cx->sb_subbase = rx->subbase;
142
143 rx->subbase = Nullch; /* so recursion works */
144 RETURNOP(pm->op_pmreplstart);
145}
146
147PP(pp_formline)
148{
149 dSP; dMARK; dORIGMARK;
150 register SV *form = *++MARK;
151 register U16 *fpc;
152 register char *t;
153 register char *f;
154 register char *s;
155 register char *send;
156 register I32 arg;
157 register SV *sv;
158 char *item;
159 I32 itemsize;
160 I32 fieldsize;
161 I32 lines = 0;
162 bool chopspace = (strchr(chopset, ' ') != Nullch);
163 char *chophere;
164 char *linemark;
a0d0e21e
LW
165 double value;
166 bool gotsome;
167 STRLEN len;
168
169 if (!SvCOMPILED(form)) {
170 SvREADONLY_off(form);
171 doparseform(form);
172 }
173
174 SvPV_force(formtarget, len);
175 t = SvGROW(formtarget, len + SvCUR(form) + 1); /* XXX SvCUR bad */
176 t += len;
177 f = SvPV(form, len);
178 /* need to jump to the next word */
179 s = f + len + WORD_ALIGN - SvCUR(form) % WORD_ALIGN;
180
181 fpc = (U16*)s;
182
183 for (;;) {
184 DEBUG_f( {
185 char *name = "???";
186 arg = -1;
187 switch (*fpc) {
188 case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
189 case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
190 case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
191 case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
192 case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
193
194 case FF_CHECKNL: name = "CHECKNL"; break;
195 case FF_CHECKCHOP: name = "CHECKCHOP"; break;
196 case FF_SPACE: name = "SPACE"; break;
197 case FF_HALFSPACE: name = "HALFSPACE"; break;
198 case FF_ITEM: name = "ITEM"; break;
199 case FF_CHOP: name = "CHOP"; break;
200 case FF_LINEGLOB: name = "LINEGLOB"; break;
201 case FF_NEWLINE: name = "NEWLINE"; break;
202 case FF_MORE: name = "MORE"; break;
203 case FF_LINEMARK: name = "LINEMARK"; break;
204 case FF_END: name = "END"; break;
205 }
206 if (arg >= 0)
207 fprintf(stderr, "%-16s%ld\n", name, (long) arg);
208 else
209 fprintf(stderr, "%-16s\n", name);
210 } )
211 switch (*fpc++) {
212 case FF_LINEMARK:
213 linemark = t;
a0d0e21e
LW
214 lines++;
215 gotsome = FALSE;
216 break;
217
218 case FF_LITERAL:
219 arg = *fpc++;
220 while (arg--)
221 *t++ = *f++;
222 break;
223
224 case FF_SKIP:
225 f += *fpc++;
226 break;
227
228 case FF_FETCH:
229 arg = *fpc++;
230 f += arg;
231 fieldsize = arg;
232
233 if (MARK < SP)
234 sv = *++MARK;
235 else {
236 sv = &sv_no;
237 if (dowarn)
238 warn("Not enough format arguments");
239 }
240 break;
241
242 case FF_CHECKNL:
243 item = s = SvPV(sv, len);
244 itemsize = len;
245 if (itemsize > fieldsize)
246 itemsize = fieldsize;
247 send = chophere = s + itemsize;
248 while (s < send) {
249 if (*s & ~31)
250 gotsome = TRUE;
251 else if (*s == '\n')
252 break;
253 s++;
254 }
255 itemsize = s - item;
256 break;
257
258 case FF_CHECKCHOP:
259 item = s = SvPV(sv, len);
260 itemsize = len;
261 if (itemsize <= fieldsize) {
262 send = chophere = s + itemsize;
263 while (s < send) {
264 if (*s == '\r') {
265 itemsize = s - item;
266 break;
267 }
268 if (*s++ & ~31)
269 gotsome = TRUE;
270 }
271 }
272 else {
273 itemsize = fieldsize;
274 send = chophere = s + itemsize;
275 while (s < send || (s == send && isSPACE(*s))) {
276 if (isSPACE(*s)) {
277 if (chopspace)
278 chophere = s;
279 if (*s == '\r')
280 break;
281 }
282 else {
283 if (*s & ~31)
284 gotsome = TRUE;
285 if (strchr(chopset, *s))
286 chophere = s + 1;
287 }
288 s++;
289 }
290 itemsize = chophere - item;
291 }
292 break;
293
294 case FF_SPACE:
295 arg = fieldsize - itemsize;
296 if (arg) {
297 fieldsize -= arg;
298 while (arg-- > 0)
299 *t++ = ' ';
300 }
301 break;
302
303 case FF_HALFSPACE:
304 arg = fieldsize - itemsize;
305 if (arg) {
306 arg /= 2;
307 fieldsize -= arg;
308 while (arg-- > 0)
309 *t++ = ' ';
310 }
311 break;
312
313 case FF_ITEM:
314 arg = itemsize;
315 s = item;
316 while (arg--) {
317#if 'z' - 'a' != 25
318 int ch = *t++ = *s++;
319 if (!iscntrl(ch))
320 t[-1] = ' ';
321#else
322 if ( !((*t++ = *s++) & ~31) )
323 t[-1] = ' ';
324#endif
325
326 }
327 break;
328
329 case FF_CHOP:
330 s = chophere;
331 if (chopspace) {
332 while (*s && isSPACE(*s))
333 s++;
334 }
335 sv_chop(sv,s);
336 break;
337
338 case FF_LINEGLOB:
339 item = s = SvPV(sv, len);
340 itemsize = len;
341 if (itemsize) {
342 gotsome = TRUE;
343 send = s + itemsize;
344 while (s < send) {
345 if (*s++ == '\n') {
346 if (s == send)
347 itemsize--;
348 else
349 lines++;
350 }
351 }
352 SvCUR_set(formtarget, t - SvPVX(formtarget));
353 sv_catpvn(formtarget, item, itemsize);
354 SvGROW(formtarget, SvCUR(formtarget) + SvCUR(form) + 1);
355 t = SvPVX(formtarget) + SvCUR(formtarget);
356 }
357 break;
358
359 case FF_DECIMAL:
360 /* If the field is marked with ^ and the value is undefined,
361 blank it out. */
362 arg = *fpc++;
363 if ((arg & 512) && !SvOK(sv)) {
364 arg = fieldsize;
365 while (arg--)
366 *t++ = ' ';
367 break;
368 }
369 gotsome = TRUE;
370 value = SvNV(sv);
371 if (arg & 256) {
372 sprintf(t, "%#*.*f", (int) fieldsize, (int) arg & 255, value);
373 } else {
374 sprintf(t, "%*.0f", (int) fieldsize, value);
375 }
376 t += fieldsize;
377 break;
378
379 case FF_NEWLINE:
380 f++;
381 while (t-- > linemark && *t == ' ') ;
382 t++;
383 *t++ = '\n';
384 break;
385
386 case FF_BLANK:
387 arg = *fpc++;
388 if (gotsome) {
389 if (arg) { /* repeat until fields exhausted? */
390 *t = '\0';
391 SvCUR_set(formtarget, t - SvPVX(formtarget));
392 lines += FmLINES(formtarget);
393 if (lines == 200) {
394 arg = t - linemark;
395 if (strnEQ(linemark, linemark - arg, arg))
396 DIE("Runaway format");
397 }
398 FmLINES(formtarget) = lines;
399 SP = ORIGMARK;
400 RETURNOP(cLISTOP->op_first);
401 }
402 }
403 else {
404 t = linemark;
405 lines--;
406 }
407 break;
408
409 case FF_MORE:
410 if (itemsize) {
411 arg = fieldsize - itemsize;
412 if (arg) {
413 fieldsize -= arg;
414 while (arg-- > 0)
415 *t++ = ' ';
416 }
417 s = t - 3;
418 if (strnEQ(s," ",3)) {
419 while (s > SvPVX(formtarget) && isSPACE(s[-1]))
420 s--;
421 }
422 *s++ = '.';
423 *s++ = '.';
424 *s++ = '.';
425 }
426 break;
427
428 case FF_END:
429 *t = '\0';
430 SvCUR_set(formtarget, t - SvPVX(formtarget));
431 FmLINES(formtarget) += lines;
432 SP = ORIGMARK;
433 RETPUSHYES;
434 }
435 }
436}
437
438PP(pp_grepstart)
439{
440 dSP;
441 SV *src;
442
443 if (stack_base + *markstack_ptr == sp) {
444 (void)POPMARK;
445 if (GIMME != G_ARRAY)
446 XPUSHs(&sv_no);
447 RETURNOP(op->op_next->op_next);
448 }
449 stack_sp = stack_base + *markstack_ptr + 1;
450 pp_pushmark(); /* push dst */
451 pp_pushmark(); /* push src */
452 ENTER; /* enter outer scope */
453
454 SAVETMPS;
455 SAVESPTR(GvSV(defgv));
456
457 ENTER; /* enter inner scope */
458 SAVESPTR(curpm);
459
460 src = stack_base[*markstack_ptr];
461 SvTEMP_off(src);
462 GvSV(defgv) = src;
463
464 PUTBACK;
465 if (op->op_type == OP_MAPSTART)
466 pp_pushmark(); /* push top */
467 return ((LOGOP*)op->op_next)->op_other;
468}
469
470PP(pp_mapstart)
471{
472 DIE("panic: mapstart"); /* uses grepstart */
473}
474
475PP(pp_mapwhile)
476{
477 dSP;
478 I32 diff = (sp - stack_base) - *markstack_ptr;
479 I32 count;
480 I32 shift;
481 SV** src;
482 SV** dst;
483
484 ++markstack_ptr[-1];
485 if (diff) {
486 if (diff > markstack_ptr[-1] - markstack_ptr[-2]) {
487 shift = diff - (markstack_ptr[-1] - markstack_ptr[-2]);
488 count = (sp - stack_base) - markstack_ptr[-1] + 2;
489
490 EXTEND(sp,shift);
491 src = sp;
492 dst = (sp += shift);
493 markstack_ptr[-1] += shift;
494 *markstack_ptr += shift;
495 while (--count)
496 *dst-- = *src--;
497 }
498 dst = stack_base + (markstack_ptr[-2] += diff) - 1;
499 ++diff;
500 while (--diff)
501 *dst-- = SvTEMP(TOPs) ? POPs : sv_mortalcopy(POPs);
502 }
503 LEAVE; /* exit inner scope */
504
505 /* All done yet? */
506 if (markstack_ptr[-1] > *markstack_ptr) {
507 I32 items;
508
509 (void)POPMARK; /* pop top */
510 LEAVE; /* exit outer scope */
511 (void)POPMARK; /* pop src */
512 items = --*markstack_ptr - markstack_ptr[-1];
513 (void)POPMARK; /* pop dst */
514 SP = stack_base + POPMARK; /* pop original mark */
515 if (GIMME != G_ARRAY) {
516 dTARGET;
517 XPUSHi(items);
518 RETURN;
519 }
520 SP += items;
521 RETURN;
522 }
523 else {
524 SV *src;
525
526 ENTER; /* enter inner scope */
527 SAVESPTR(curpm);
528
529 src = stack_base[markstack_ptr[-1]];
530 SvTEMP_off(src);
531 GvSV(defgv) = src;
532
533 RETURNOP(cLOGOP->op_other);
534 }
535}
536
537
538PP(pp_sort)
539{
540 dSP; dMARK; dORIGMARK;
541 register SV **up;
542 SV **myorigmark = ORIGMARK;
543 register I32 max;
544 HV *stash;
545 GV *gv;
546 CV *cv;
547 I32 gimme = GIMME;
548 OP* nextop = op->op_next;
549
550 if (gimme != G_ARRAY) {
551 SP = MARK;
552 RETPUSHUNDEF;
553 }
554
555 if (op->op_flags & OPf_STACKED) {
556 ENTER;
557 if (op->op_flags & OPf_SPECIAL) {
558 OP *kid = cLISTOP->op_first->op_sibling; /* pass pushmark */
559 kid = kUNOP->op_first; /* pass rv2gv */
560 kid = kUNOP->op_first; /* pass leave */
561 sortcop = kid->op_next;
562 stash = curcop->cop_stash;
563 }
564 else {
565 cv = sv_2cv(*++MARK, &stash, &gv, 0);
566 if (!(cv && CvROOT(cv))) {
567 if (gv) {
568 SV *tmpstr = sv_newmortal();
569 gv_efullname(tmpstr, gv);
570 if (cv && CvXSUB(cv))
571 DIE("Xsub \"%s\" called in sort", SvPVX(tmpstr));
572 DIE("Undefined sort subroutine \"%s\" called",
573 SvPVX(tmpstr));
574 }
575 if (cv) {
576 if (CvXSUB(cv))
577 DIE("Xsub called in sort");
578 DIE("Undefined subroutine in sort");
579 }
580 DIE("Not a CODE reference in sort");
581 }
582 sortcop = CvSTART(cv);
583 SAVESPTR(CvROOT(cv)->op_ppaddr);
584 CvROOT(cv)->op_ppaddr = ppaddr[OP_NULL];
585
586 SAVESPTR(curpad);
587 curpad = AvARRAY((AV*)AvARRAY(CvPADLIST(cv))[1]);
588 }
589 }
590 else {
591 sortcop = Nullop;
592 stash = curcop->cop_stash;
593 }
594
595 up = myorigmark + 1;
596 while (MARK < SP) { /* This may or may not shift down one here. */
597 /*SUPPRESS 560*/
598 if (*up = *++MARK) { /* Weed out nulls. */
599 if (!SvPOK(*up))
600 (void)sv_2pv(*up, &na);
601 else
602 SvTEMP_off(*up);
603 up++;
604 }
605 }
606 max = --up - myorigmark;
607 if (sortcop) {
608 if (max > 1) {
609 AV *oldstack;
610 CONTEXT *cx;
611 SV** newsp;
612
613 SAVETMPS;
614 SAVESPTR(op);
615
616 oldstack = stack;
617 if (!sortstack) {
618 sortstack = newAV();
619 AvREAL_off(sortstack);
620 av_extend(sortstack, 32);
621 }
622 SWITCHSTACK(stack, sortstack);
623 if (sortstash != stash) {
624 firstgv = gv_fetchpv("a", TRUE, SVt_PV);
625 secondgv = gv_fetchpv("b", TRUE, SVt_PV);
626 sortstash = stash;
627 }
628
629 SAVESPTR(GvSV(firstgv));
630 SAVESPTR(GvSV(secondgv));
631 PUSHBLOCK(cx, CXt_LOOP, stack_base);
632 sortcxix = cxstack_ix;
633
634 qsort((char*)(myorigmark+1), max, sizeof(SV*), sortcv);
635
636 POPBLOCK(cx,curpm);
637 SWITCHSTACK(sortstack, oldstack);
638 }
639 LEAVE;
640 }
641 else {
642 if (max > 1) {
643 MEXTEND(SP, 20); /* Can't afford stack realloc on signal. */
644 qsort((char*)(ORIGMARK+1), max, sizeof(SV*), sortcmp);
645 }
646 }
647 stack_sp = ORIGMARK + max;
648 return nextop;
649}
650
651/* Range stuff. */
652
653PP(pp_range)
654{
655 if (GIMME == G_ARRAY)
656 return cCONDOP->op_true;
657 return SvTRUEx(PAD_SV(op->op_targ)) ? cCONDOP->op_false : cCONDOP->op_true;
658}
659
660PP(pp_flip)
661{
662 dSP;
663
664 if (GIMME == G_ARRAY) {
665 RETURNOP(((CONDOP*)cUNOP->op_first)->op_false);
666 }
667 else {
668 dTOPss;
669 SV *targ = PAD_SV(op->op_targ);
670
671 if ((op->op_private & OPpFLIP_LINENUM)
672 ? last_in_gv && SvIV(sv) == IoLINES(GvIOp(last_in_gv))
673 : SvTRUE(sv) ) {
674 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
675 if (op->op_flags & OPf_SPECIAL) {
676 sv_setiv(targ, 1);
677 RETURN;
678 }
679 else {
680 sv_setiv(targ, 0);
681 sp--;
682 RETURNOP(((CONDOP*)cUNOP->op_first)->op_false);
683 }
684 }
685 sv_setpv(TARG, "");
686 SETs(targ);
687 RETURN;
688 }
689}
690
691PP(pp_flop)
692{
693 dSP;
694
695 if (GIMME == G_ARRAY) {
696 dPOPPOPssrl;
697 register I32 i;
698 register SV *sv;
699 I32 max;
700
701 if (SvNIOK(left) || !SvPOK(left) ||
702 (looks_like_number(left) && *SvPVX(left) != '0') ) {
703 i = SvIV(left);
704 max = SvIV(right);
705 if (max > i)
706 EXTEND(SP, max - i + 1);
707 while (i <= max) {
708 sv = sv_mortalcopy(&sv_no);
709 sv_setiv(sv,i++);
710 PUSHs(sv);
711 }
712 }
713 else {
714 SV *final = sv_mortalcopy(right);
715 STRLEN len;
716 char *tmps = SvPV(final, len);
717
718 sv = sv_mortalcopy(left);
719 while (!SvNIOK(sv) && SvCUR(sv) <= len &&
720 strNE(SvPVX(sv),tmps) ) {
721 XPUSHs(sv);
722 sv = sv_2mortal(newSVsv(sv));
723 sv_inc(sv);
724 }
725 if (strEQ(SvPVX(sv),tmps))
726 XPUSHs(sv);
727 }
728 }
729 else {
730 dTOPss;
731 SV *targ = PAD_SV(cUNOP->op_first->op_targ);
732 sv_inc(targ);
733 if ((op->op_private & OPpFLIP_LINENUM)
734 ? last_in_gv && SvIV(sv) == IoLINES(GvIOp(last_in_gv))
735 : SvTRUE(sv) ) {
736 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
737 sv_catpv(targ, "E0");
738 }
739 SETs(targ);
740 }
741
742 RETURN;
743}
744
745/* Control. */
746
747static I32
748dopoptolabel(label)
749char *label;
750{
751 register I32 i;
752 register CONTEXT *cx;
753
754 for (i = cxstack_ix; i >= 0; i--) {
755 cx = &cxstack[i];
756 switch (cx->cx_type) {
757 case CXt_SUBST:
758 if (dowarn)
759 warn("Exiting substitution via %s", op_name[op->op_type]);
760 break;
761 case CXt_SUB:
762 if (dowarn)
763 warn("Exiting subroutine via %s", op_name[op->op_type]);
764 break;
765 case CXt_EVAL:
766 if (dowarn)
767 warn("Exiting eval via %s", op_name[op->op_type]);
768 break;
769 case CXt_LOOP:
770 if (!cx->blk_loop.label ||
771 strNE(label, cx->blk_loop.label) ) {
772 DEBUG_l(deb("(Skipping label #%d %s)\n",
773 i, cx->blk_loop.label));
774 continue;
775 }
776 DEBUG_l( deb("(Found label #%d %s)\n", i, label));
777 return i;
778 }
779 }
780 return i;
781}
782
783static I32
784dopoptosub(startingblock)
785I32 startingblock;
786{
787 I32 i;
788 register CONTEXT *cx;
789 for (i = startingblock; i >= 0; i--) {
790 cx = &cxstack[i];
791 switch (cx->cx_type) {
792 default:
793 continue;
794 case CXt_EVAL:
795 case CXt_SUB:
796 DEBUG_l( deb("(Found sub #%d)\n", i));
797 return i;
798 }
799 }
800 return i;
801}
802
803static I32
804dopoptoeval(startingblock)
805I32 startingblock;
806{
807 I32 i;
808 register CONTEXT *cx;
809 for (i = startingblock; i >= 0; i--) {
810 cx = &cxstack[i];
811 switch (cx->cx_type) {
812 default:
813 continue;
814 case CXt_EVAL:
815 DEBUG_l( deb("(Found eval #%d)\n", i));
816 return i;
817 }
818 }
819 return i;
820}
821
822static I32
823dopoptoloop(startingblock)
824I32 startingblock;
825{
826 I32 i;
827 register CONTEXT *cx;
828 for (i = startingblock; i >= 0; i--) {
829 cx = &cxstack[i];
830 switch (cx->cx_type) {
831 case CXt_SUBST:
832 if (dowarn)
833 warn("Exiting substitition via %s", op_name[op->op_type]);
834 break;
835 case CXt_SUB:
836 if (dowarn)
837 warn("Exiting subroutine via %s", op_name[op->op_type]);
838 break;
839 case CXt_EVAL:
840 if (dowarn)
841 warn("Exiting eval via %s", op_name[op->op_type]);
842 break;
843 case CXt_LOOP:
844 DEBUG_l( deb("(Found loop #%d)\n", i));
845 return i;
846 }
847 }
848 return i;
849}
850
851void
852dounwind(cxix)
853I32 cxix;
854{
855 register CONTEXT *cx;
856 SV **newsp;
857 I32 optype;
858
859 while (cxstack_ix > cxix) {
860 cx = &cxstack[cxstack_ix--];
861 DEBUG_l(fprintf(stderr, "Unwinding block %ld, type %s\n", (long) cxstack_ix+1,
862 block_type[cx->cx_type]));
863 /* Note: we don't need to restore the base context info till the end. */
864 switch (cx->cx_type) {
865 case CXt_SUB:
866 POPSUB(cx);
867 break;
868 case CXt_EVAL:
869 POPEVAL(cx);
870 break;
871 case CXt_LOOP:
872 POPLOOP(cx);
873 break;
874 case CXt_SUBST:
875 break;
876 }
877 }
878}
879
ecfc5424 880#ifdef I_STDARG
a0d0e21e
LW
881OP *
882die(char* pat, ...)
883#else
884/*VARARGS0*/
885OP *
886die(pat, va_alist)
887 char *pat;
888 va_dcl
889#endif
890{
891 va_list args;
892 char *message;
893 int oldrunlevel = runlevel;
894 int was_in_eval = in_eval;
748a9306
LW
895 HV *stash;
896 GV *gv;
897 CV *cv;
a0d0e21e
LW
898
899#ifdef I_STDARG
900 va_start(args, pat);
901#else
902 va_start(args);
903#endif
904 message = mess(pat, &args);
905 va_end(args);
748a9306
LW
906 if (diehook && (cv = sv_2cv(diehook, &stash, &gv, 0)) && !CvDEPTH(cv)) {
907 dSP;
908
909 PUSHMARK(sp);
910 EXTEND(sp, 1);
911 PUSHs(sv_2mortal(newSVpv(message,0)));
912 PUTBACK;
913 perl_call_sv((SV*)cv, G_DISCARD);
914 }
a0d0e21e
LW
915 restartop = die_where(message);
916 if ((!restartop && was_in_eval) || oldrunlevel > 1)
917 longjmp(top_env, 3);
918 return restartop;
919}
920
921OP *
922die_where(message)
923char *message;
924{
925 if (in_eval) {
926 I32 cxix;
927 register CONTEXT *cx;
928 I32 gimme;
929 SV **newsp;
748a9306
LW
930 SV *errsv;
931
932 errsv = GvSV(gv_fetchpv("@",TRUE, SVt_PV));
933 /* As destructors may produce errors we set $@ at the last moment */
934 sv_setpv(errsv, ""); /* clear $@ before destroying */
a0d0e21e 935
a0d0e21e
LW
936 cxix = dopoptoeval(cxstack_ix);
937 if (cxix >= 0) {
938 I32 optype;
939
940 if (cxix < cxstack_ix)
941 dounwind(cxix);
942
943 POPBLOCK(cx,curpm);
944 if (cx->cx_type != CXt_EVAL) {
945 fprintf(stderr, "panic: die %s", message);
946 my_exit(1);
947 }
948 POPEVAL(cx);
949
950 if (gimme == G_SCALAR)
951 *++newsp = &sv_undef;
952 stack_sp = newsp;
953
954 LEAVE;
748a9306
LW
955
956 sv_insert(errsv, 0, 0, message, strlen(message));
a0d0e21e
LW
957 if (optype == OP_REQUIRE)
958 DIE("%s", SvPVx(GvSV(gv_fetchpv("@",TRUE, SVt_PV)), na));
959 return pop_return();
960 }
961 }
962 fputs(message, stderr);
963 (void)fflush(stderr);
964 if (e_fp)
965 (void)UNLINK(e_tmpname);
748a9306
LW
966 statusvalue = SHIFTSTATUS(statusvalue);
967#ifdef VMS
968 my_exit((U32)vaxc$errno?vaxc$errno:errno?errno:statusvalue?statusvalue:SS$_ABORT);
969#else
a0d0e21e 970 my_exit((I32)((errno&255)?errno:((statusvalue&255)?statusvalue:255)));
748a9306 971#endif
a0d0e21e
LW
972 return 0;
973}
974
975PP(pp_xor)
976{
977 dSP; dPOPTOPssrl;
978 if (SvTRUE(left) != SvTRUE(right))
979 RETSETYES;
980 else
981 RETSETNO;
982}
983
984PP(pp_andassign)
985{
986 dSP;
987 if (!SvTRUE(TOPs))
988 RETURN;
989 else
990 RETURNOP(cLOGOP->op_other);
991}
992
993PP(pp_orassign)
994{
995 dSP;
996 if (SvTRUE(TOPs))
997 RETURN;
998 else
999 RETURNOP(cLOGOP->op_other);
1000}
1001
1002#ifdef DEPRECATED
1003PP(pp_entersubr)
1004{
1005 dSP;
1006 SV** mark = (stack_base + *markstack_ptr + 1);
1007 SV* cv = *mark;
1008 while (mark < sp) { /* emulate old interface */
1009 *mark = mark[1];
1010 mark++;
1011 }
1012 *sp = cv;
1013 return pp_entersub();
1014}
1015#endif
1016
1017PP(pp_caller)
1018{
1019 dSP;
1020 register I32 cxix = dopoptosub(cxstack_ix);
1021 register CONTEXT *cx;
1022 I32 dbcxix;
1023 SV *sv;
1024 I32 count = 0;
1025
1026 if (MAXARG)
1027 count = POPi;
1028 EXTEND(SP, 6);
1029 for (;;) {
1030 if (cxix < 0) {
1031 if (GIMME != G_ARRAY)
1032 RETPUSHUNDEF;
1033 RETURN;
1034 }
1035 if (DBsub && cxix >= 0 &&
1036 cxstack[cxix].blk_sub.cv == GvCV(DBsub))
1037 count++;
1038 if (!count--)
1039 break;
1040 cxix = dopoptosub(cxix - 1);
1041 }
1042 cx = &cxstack[cxix];
1043 if (GIMME != G_ARRAY) {
1044 dTARGET;
1045
1046 sv_setpv(TARG, HvNAME(cx->blk_oldcop->cop_stash));
1047 PUSHs(TARG);
1048 RETURN;
1049 }
1050 dbcxix = dopoptosub(cxix - 1);
1051 if (DBsub && dbcxix >= 0 && cxstack[dbcxix].blk_sub.cv == GvCV(DBsub))
1052 cx = &cxstack[dbcxix];
1053
1054 PUSHs(sv_2mortal(newSVpv(HvNAME(cx->blk_oldcop->cop_stash), 0)));
1055 PUSHs(sv_2mortal(newSVpv(SvPVX(GvSV(cx->blk_oldcop->cop_filegv)), 0)));
1056 PUSHs(sv_2mortal(newSViv((I32)cx->blk_oldcop->cop_line)));
1057 if (!MAXARG)
1058 RETURN;
1059 if (cx->cx_type == CXt_SUB) {
1060 sv = NEWSV(49, 0);
1061 gv_efullname(sv, CvGV(cxstack[cxix].blk_sub.cv));
1062 PUSHs(sv_2mortal(sv));
1063 PUSHs(sv_2mortal(newSViv((I32)cx->blk_sub.hasargs)));
1064 }
1065 else {
1066 PUSHs(sv_2mortal(newSVpv("(eval)",0)));
1067 PUSHs(sv_2mortal(newSViv(0)));
1068 }
1069 PUSHs(sv_2mortal(newSViv((I32)cx->blk_gimme)));
748a9306
LW
1070 if (cx->cx_type == CXt_EVAL && cx->blk_eval.old_op_type == OP_ENTEREVAL)
1071 PUSHs(cx->blk_eval.cur_text);
1072
a0d0e21e
LW
1073 if (cx->blk_sub.hasargs && curcop->cop_stash == debstash) {
1074 AV *ary = cx->blk_sub.argarray;
1075 int off = AvARRAY(ary) - AvALLOC(ary);
1076
1077 if (!dbargs) {
1078 GV* tmpgv;
1079 dbargs = GvAV(gv_AVadd(tmpgv = gv_fetchpv("DB::args", TRUE,
1080 SVt_PVAV)));
1081 SvMULTI_on(tmpgv);
1082 AvREAL_off(dbargs); /* XXX Should be REIFY */
1083 }
1084
1085 if (AvMAX(dbargs) < AvFILL(ary) + off)
1086 av_extend(dbargs, AvFILL(ary) + off);
1087 Copy(AvALLOC(ary), AvARRAY(dbargs), AvFILL(ary) + 1 + off, SV*);
1088 AvFILL(dbargs) = AvFILL(ary) + off;
1089 }
1090 RETURN;
1091}
1092
1093static int
1094sortcv(a, b)
1095const void *a;
1096const void *b;
1097{
1098 SV **str1 = (SV **) a;
1099 SV **str2 = (SV **) b;
748a9306 1100 I32 oldsaveix = savestack_ix;
a0d0e21e
LW
1101 I32 oldscopeix = scopestack_ix;
1102 I32 result;
1103 GvSV(firstgv) = *str1;
1104 GvSV(secondgv) = *str2;
1105 stack_sp = stack_base;
1106 op = sortcop;
1107 run();
1108 if (stack_sp != stack_base + 1)
1109 croak("Sort subroutine didn't return single value");
748a9306 1110 if (!SvNIOKp(*stack_sp))
a0d0e21e
LW
1111 croak("Sort subroutine didn't return a numeric value");
1112 result = SvIV(*stack_sp);
1113 while (scopestack_ix > oldscopeix) {
1114 LEAVE;
1115 }
748a9306 1116 leave_scope(oldsaveix);
a0d0e21e
LW
1117 return result;
1118}
1119
1120static int
1121sortcmp(a, b)
1122const void *a;
1123const void *b;
1124{
1125 register SV *str1 = *(SV **) a;
1126 register SV *str2 = *(SV **) b;
1127 I32 retval;
1128
1129 if (SvCUR(str1) < SvCUR(str2)) {
1130 /*SUPPRESS 560*/
1131 if (retval = memcmp(SvPVX(str1), SvPVX(str2), SvCUR(str1)))
1132 return retval;
1133 else
1134 return -1;
1135 }
1136 /*SUPPRESS 560*/
1137 else if (retval = memcmp(SvPVX(str1), SvPVX(str2), SvCUR(str2)))
1138 return retval;
1139 else if (SvCUR(str1) == SvCUR(str2))
1140 return 0;
1141 else
1142 return 1;
1143}
1144
1145PP(pp_reset)
1146{
1147 dSP;
1148 char *tmps;
1149
1150 if (MAXARG < 1)
1151 tmps = "";
1152 else
1153 tmps = POPp;
1154 sv_reset(tmps, curcop->cop_stash);
1155 PUSHs(&sv_yes);
1156 RETURN;
1157}
1158
1159PP(pp_lineseq)
1160{
1161 return NORMAL;
1162}
1163
1164PP(pp_dbstate)
1165{
1166 curcop = (COP*)op;
1167 TAINT_NOT; /* Each statement is presumed innocent */
1168 stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
1169 FREETMPS;
1170
1171 if (op->op_private || SvIV(DBsingle) || SvIV(DBsignal) || SvIV(DBtrace))
1172 {
1173 SV **sp;
1174 register CV *cv;
1175 register CONTEXT *cx;
748a9306 1176 I32 gimme = G_ARRAY;
a0d0e21e
LW
1177 I32 hasargs;
1178 GV *gv;
1179
1180 ENTER;
1181 SAVETMPS;
1182
a0d0e21e
LW
1183 gv = DBgv;
1184 cv = GvCV(gv);
a0d0e21e
LW
1185 if (!cv)
1186 DIE("No DB::DB routine defined");
1187
1188 if (CvDEPTH(cv) >= 1) /* don't do recursive DB::DB call */
1189 return NORMAL;
748a9306
LW
1190
1191 SAVEI32(debug);
1192 SAVESPTR(stack_sp);
1193 debug = 0;
1194 hasargs = 0;
1195 sp = stack_sp;
1196
a0d0e21e 1197 push_return(op->op_next);
748a9306 1198 PUSHBLOCK(cx, CXt_SUB, sp);
a0d0e21e
LW
1199 PUSHSUB(cx);
1200 CvDEPTH(cv)++;
1201 (void)SvREFCNT_inc(cv);
1202 SAVESPTR(curpad);
1203 curpad = AvARRAY((AV*)*av_fetch(CvPADLIST(cv),1,FALSE));
1204 RETURNOP(CvSTART(cv));
1205 }
1206 else
1207 return NORMAL;
1208}
1209
1210PP(pp_scope)
1211{
1212 return NORMAL;
1213}
1214
1215PP(pp_enteriter)
1216{
1217 dSP; dMARK;
1218 register CONTEXT *cx;
1219 I32 gimme = GIMME;
1220 SV **svp;
1221
1222 if (op->op_targ)
1223 svp = &curpad[op->op_targ]; /* "my" variable */
1224 else
1225 svp = &GvSV((GV*)POPs); /* symbol table variable */
1226
1227 ENTER;
1228 SAVETMPS;
1229 ENTER;
1230
1231 PUSHBLOCK(cx, CXt_LOOP, SP);
1232 PUSHLOOP(cx, svp, MARK);
1233 cx->blk_loop.iterary = stack;
1234 cx->blk_loop.iterix = MARK - stack_base;
1235
1236 RETURN;
1237}
1238
1239PP(pp_enterloop)
1240{
1241 dSP;
1242 register CONTEXT *cx;
1243 I32 gimme = GIMME;
1244
1245 ENTER;
1246 SAVETMPS;
1247 ENTER;
1248
1249 PUSHBLOCK(cx, CXt_LOOP, SP);
1250 PUSHLOOP(cx, 0, SP);
1251
1252 RETURN;
1253}
1254
1255PP(pp_leaveloop)
1256{
1257 dSP;
1258 register CONTEXT *cx;
1259 I32 gimme;
1260 SV **newsp;
1261 PMOP *newpm;
1262 SV **mark;
1263
1264 POPBLOCK(cx,newpm);
1265 mark = newsp;
1266 POPLOOP(cx);
1267 if (gimme == G_SCALAR) {
1268 if (op->op_private & OPpLEAVE_VOID)
1269 ;
1270 else {
1271 if (mark < SP)
1272 *++newsp = sv_mortalcopy(*SP);
1273 else
1274 *++newsp = &sv_undef;
1275 }
1276 }
1277 else {
1278 while (mark < SP)
1279 *++newsp = sv_mortalcopy(*++mark);
1280 }
1281 curpm = newpm; /* Don't pop $1 et al till now */
1282 sp = newsp;
1283 LEAVE;
1284 LEAVE;
1285
1286 RETURN;
1287}
1288
1289PP(pp_return)
1290{
1291 dSP; dMARK;
1292 I32 cxix;
1293 register CONTEXT *cx;
1294 I32 gimme;
1295 SV **newsp;
1296 PMOP *newpm;
1297 I32 optype = 0;
1298
1299 if (stack == sortstack) {
1300 if (cxstack_ix == sortcxix || dopoptosub(cxstack_ix) < sortcxix) {
16d20bd9
AD
1301 if (cxstack_ix > sortcxix)
1302 dounwind(sortcxix);
a0d0e21e
LW
1303 AvARRAY(stack)[1] = *SP;
1304 stack_sp = stack_base + 1;
1305 return 0;
1306 }
1307 }
1308
1309 cxix = dopoptosub(cxstack_ix);
1310 if (cxix < 0)
1311 DIE("Can't return outside a subroutine");
1312 if (cxix < cxstack_ix)
1313 dounwind(cxix);
1314
1315 POPBLOCK(cx,newpm);
1316 switch (cx->cx_type) {
1317 case CXt_SUB:
1318 POPSUB(cx);
1319 break;
1320 case CXt_EVAL:
1321 POPEVAL(cx);
748a9306
LW
1322 if (optype == OP_REQUIRE &&
1323 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
1324 {
1325 char *name = cx->blk_eval.old_name;
1326 (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
1327 DIE("%s did not return a true value", name);
1328 }
a0d0e21e
LW
1329 break;
1330 default:
1331 DIE("panic: return");
1332 break;
1333 }
1334
1335 if (gimme == G_SCALAR) {
1336 if (MARK < SP)
1337 *++newsp = sv_mortalcopy(*SP);
1338 else
1339 *++newsp = &sv_undef;
a0d0e21e
LW
1340 }
1341 else {
a0d0e21e
LW
1342 while (MARK < SP)
1343 *++newsp = sv_mortalcopy(*++MARK);
1344 }
1345 curpm = newpm; /* Don't pop $1 et al till now */
1346 stack_sp = newsp;
1347
1348 LEAVE;
1349 return pop_return();
1350}
1351
1352PP(pp_last)
1353{
1354 dSP;
1355 I32 cxix;
1356 register CONTEXT *cx;
1357 I32 gimme;
1358 I32 optype;
1359 OP *nextop;
1360 SV **newsp;
1361 PMOP *newpm;
1362 SV **mark = stack_base + cxstack[cxstack_ix].blk_oldsp;
a0d0e21e
LW
1363
1364 if (op->op_flags & OPf_SPECIAL) {
1365 cxix = dopoptoloop(cxstack_ix);
1366 if (cxix < 0)
1367 DIE("Can't \"last\" outside a block");
1368 }
1369 else {
1370 cxix = dopoptolabel(cPVOP->op_pv);
1371 if (cxix < 0)
1372 DIE("Label not found for \"last %s\"", cPVOP->op_pv);
1373 }
1374 if (cxix < cxstack_ix)
1375 dounwind(cxix);
1376
1377 POPBLOCK(cx,newpm);
1378 switch (cx->cx_type) {
1379 case CXt_LOOP:
1380 POPLOOP(cx);
1381 nextop = cx->blk_loop.last_op->op_next;
1382 LEAVE;
1383 break;
1384 case CXt_EVAL:
1385 POPEVAL(cx);
1386 nextop = pop_return();
1387 break;
1388 case CXt_SUB:
1389 POPSUB(cx);
1390 nextop = pop_return();
1391 break;
1392 default:
1393 DIE("panic: last");
1394 break;
1395 }
1396
1397 if (gimme == G_SCALAR) {
1398 if (mark < SP)
1399 *++newsp = sv_mortalcopy(*SP);
1400 else
1401 *++newsp = &sv_undef;
1402 }
1403 else {
1404 while (mark < SP)
1405 *++newsp = sv_mortalcopy(*++mark);
1406 }
1407 curpm = newpm; /* Don't pop $1 et al till now */
1408 sp = newsp;
1409
1410 LEAVE;
1411 RETURNOP(nextop);
1412}
1413
1414PP(pp_next)
1415{
1416 I32 cxix;
1417 register CONTEXT *cx;
1418 I32 oldsave;
1419
1420 if (op->op_flags & OPf_SPECIAL) {
1421 cxix = dopoptoloop(cxstack_ix);
1422 if (cxix < 0)
1423 DIE("Can't \"next\" outside a block");
1424 }
1425 else {
1426 cxix = dopoptolabel(cPVOP->op_pv);
1427 if (cxix < 0)
1428 DIE("Label not found for \"next %s\"", cPVOP->op_pv);
1429 }
1430 if (cxix < cxstack_ix)
1431 dounwind(cxix);
1432
1433 TOPBLOCK(cx);
1434 oldsave = scopestack[scopestack_ix - 1];
1435 LEAVE_SCOPE(oldsave);
1436 return cx->blk_loop.next_op;
1437}
1438
1439PP(pp_redo)
1440{
1441 I32 cxix;
1442 register CONTEXT *cx;
1443 I32 oldsave;
1444
1445 if (op->op_flags & OPf_SPECIAL) {
1446 cxix = dopoptoloop(cxstack_ix);
1447 if (cxix < 0)
1448 DIE("Can't \"redo\" outside a block");
1449 }
1450 else {
1451 cxix = dopoptolabel(cPVOP->op_pv);
1452 if (cxix < 0)
1453 DIE("Label not found for \"redo %s\"", cPVOP->op_pv);
1454 }
1455 if (cxix < cxstack_ix)
1456 dounwind(cxix);
1457
1458 TOPBLOCK(cx);
1459 oldsave = scopestack[scopestack_ix - 1];
1460 LEAVE_SCOPE(oldsave);
1461 return cx->blk_loop.redo_op;
1462}
1463
1464static OP* lastgotoprobe;
1465
1466static OP *
1467dofindlabel(op,label,opstack)
1468OP *op;
1469char *label;
1470OP **opstack;
1471{
1472 OP *kid;
1473 OP **ops = opstack;
1474
1475 if (op->op_type == OP_LEAVE ||
1476 op->op_type == OP_SCOPE ||
1477 op->op_type == OP_LEAVELOOP ||
1478 op->op_type == OP_LEAVETRY)
1479 *ops++ = cUNOP->op_first;
1480 *ops = 0;
1481 if (op->op_flags & OPf_KIDS) {
1482 /* First try all the kids at this level, since that's likeliest. */
1483 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling) {
1484 if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
1485 kCOP->cop_label && strEQ(kCOP->cop_label, label))
1486 return kid;
1487 }
1488 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling) {
1489 if (kid == lastgotoprobe)
1490 continue;
1491 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
1492 if (ops > opstack &&
1493 (ops[-1]->op_type == OP_NEXTSTATE ||
1494 ops[-1]->op_type == OP_DBSTATE))
1495 *ops = kid;
1496 else
1497 *ops++ = kid;
1498 }
1499 if (op = dofindlabel(kid,label,ops))
1500 return op;
1501 }
1502 }
1503 *ops = 0;
1504 return 0;
1505}
1506
1507PP(pp_dump)
1508{
1509 return pp_goto(ARGS);
1510 /*NOTREACHED*/
1511}
1512
1513PP(pp_goto)
1514{
1515 dSP;
1516 OP *retop = 0;
1517 I32 ix;
1518 register CONTEXT *cx;
1519 OP *enterops[64];
1520 char *label;
1521 int do_dump = (op->op_type == OP_DUMP);
1522
1523 label = 0;
1524 if (op->op_flags & OPf_STACKED) {
1525 SV *sv = POPs;
1526
1527 /* This egregious kludge implements goto &subroutine */
1528 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
1529 I32 cxix;
1530 register CONTEXT *cx;
1531 CV* cv = (CV*)SvRV(sv);
1532 SV** mark;
1533 I32 items = 0;
1534 I32 oldsave;
1535
1536 /* First do some returnish stuff. */
1537 cxix = dopoptosub(cxstack_ix);
1538 if (cxix < 0)
1539 DIE("Can't goto subroutine outside a subroutine");
1540 if (cxix < cxstack_ix)
1541 dounwind(cxix);
1542 TOPBLOCK(cx);
1543 mark = stack_sp;
1544 if (cx->blk_sub.hasargs) { /* put @_ back onto stack */
1545 AV* av = cx->blk_sub.argarray;
1546
1547 items = AvFILL(av) + 1;
1548 Copy(AvARRAY(av), ++stack_sp, items, SV*);
1549 stack_sp += items;
1550 GvAV(defgv) = cx->blk_sub.savearray;
1551 av_clear(av);
1552 AvREAL_off(av);
1553 }
1554 if (!(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
1555 SvREFCNT_dec(cx->blk_sub.cv);
1556 oldsave = scopestack[scopestack_ix - 1];
1557 LEAVE_SCOPE(oldsave);
1558
1559 /* Now do some callish stuff. */
1560 SAVETMPS;
1561 if (CvXSUB(cv)) {
1562 if (CvOLDSTYLE(cv)) {
ecfc5424 1563 I32 (*fp3)_((int,int,int));
a0d0e21e
LW
1564 while (sp > mark) {
1565 sp[1] = sp[0];
1566 sp--;
1567 }
ecfc5424
AD
1568 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
1569 items = (*fp3)(CvXSUBANY(cv).any_i32,
1570 mark - stack_base + 1,
1571 items);
a0d0e21e
LW
1572 sp = stack_base + items;
1573 }
1574 else {
1575 (void)(*CvXSUB(cv))(cv);
1576 }
1577 LEAVE;
1578 return pop_return();
1579 }
1580 else {
1581 AV* padlist = CvPADLIST(cv);
1582 SV** svp = AvARRAY(padlist);
1583 cx->blk_sub.cv = cv;
1584 cx->blk_sub.olddepth = CvDEPTH(cv);
1585 CvDEPTH(cv)++;
1586 if (CvDEPTH(cv) < 2)
1587 (void)SvREFCNT_inc(cv);
1588 else { /* save temporaries on recursion? */
1589 if (CvDEPTH(cv) == 100 && dowarn)
1590 warn("Deep recursion on subroutine \"%s\"",
1591 GvENAME(CvGV(cv)));
1592 if (CvDEPTH(cv) > AvFILL(padlist)) {
1593 AV *newpad = newAV();
748a9306 1594 AV *oldpad = (AV*)AvARRAY(svp[CvDEPTH(cv)-1]);
a0d0e21e
LW
1595 I32 ix = AvFILL((AV*)svp[1]);
1596 svp = AvARRAY(svp[0]);
748a9306 1597 for ( ;ix > 0; ix--) {
a0d0e21e 1598 if (svp[ix] != &sv_undef) {
748a9306
LW
1599 char *name = SvPVX(svp[ix]);
1600 if (SvFLAGS(svp[ix]) & SVf_FAKE) {
1601 /* outer lexical? */
1602 av_store(newpad, ix,
1603 SvREFCNT_inc(AvARRAY(oldpad)[ix]) );
1604 }
1605 else { /* our own lexical */
1606 if (*name == '@')
1607 av_store(newpad, ix, sv = (SV*)newAV());
1608 else if (*name == '%')
1609 av_store(newpad, ix, sv = (SV*)newHV());
1610 else
1611 av_store(newpad, ix, sv = NEWSV(0,0));
1612 SvPADMY_on(sv);
1613 }
a0d0e21e
LW
1614 }
1615 else {
748a9306 1616 av_store(newpad, ix, sv = NEWSV(0,0));
a0d0e21e
LW
1617 SvPADTMP_on(sv);
1618 }
1619 }
1620 if (cx->blk_sub.hasargs) {
1621 AV* av = newAV();
1622 av_extend(av, 0);
1623 av_store(newpad, 0, (SV*)av);
1624 AvFLAGS(av) = AVf_REIFY;
1625 }
1626 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
1627 AvFILL(padlist) = CvDEPTH(cv);
1628 svp = AvARRAY(padlist);
1629 }
1630 }
1631 SAVESPTR(curpad);
1632 curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
1633 if (cx->blk_sub.hasargs) {
1634 AV* av = (AV*)curpad[0];
1635 SV** ary;
1636
1637 cx->blk_sub.savearray = GvAV(defgv);
1638 cx->blk_sub.argarray = av;
1639 GvAV(defgv) = cx->blk_sub.argarray;
1640 ++mark;
1641
1642 if (items >= AvMAX(av) + 1) {
1643 ary = AvALLOC(av);
1644 if (AvARRAY(av) != ary) {
1645 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
1646 SvPVX(av) = (char*)ary;
1647 }
1648 if (items >= AvMAX(av) + 1) {
1649 AvMAX(av) = items - 1;
1650 Renew(ary,items+1,SV*);
1651 AvALLOC(av) = ary;
1652 SvPVX(av) = (char*)ary;
1653 }
1654 }
1655 Copy(mark,AvARRAY(av),items,SV*);
1656 AvFILL(av) = items - 1;
1657
1658 while (items--) {
1659 if (*mark)
1660 SvTEMP_off(*mark);
1661 mark++;
1662 }
1663 }
1664 RETURNOP(CvSTART(cv));
1665 }
1666 }
1667 else
1668 label = SvPV(sv,na);
1669 }
1670 else if (op->op_flags & OPf_SPECIAL) {
1671 if (! do_dump)
1672 DIE("goto must have label");
1673 }
1674 else
1675 label = cPVOP->op_pv;
1676
1677 if (label && *label) {
1678 OP *gotoprobe = 0;
1679
1680 /* find label */
1681
1682 lastgotoprobe = 0;
1683 *enterops = 0;
1684 for (ix = cxstack_ix; ix >= 0; ix--) {
1685 cx = &cxstack[ix];
1686 switch (cx->cx_type) {
1687 case CXt_SUB:
1688 gotoprobe = CvROOT(cx->blk_sub.cv);
1689 break;
1690 case CXt_EVAL:
1691 gotoprobe = eval_root; /* XXX not good for nested eval */
1692 break;
1693 case CXt_LOOP:
1694 gotoprobe = cx->blk_oldcop->op_sibling;
1695 break;
1696 case CXt_SUBST:
1697 continue;
1698 case CXt_BLOCK:
1699 if (ix)
1700 gotoprobe = cx->blk_oldcop->op_sibling;
1701 else
1702 gotoprobe = main_root;
1703 break;
1704 default:
1705 if (ix)
1706 DIE("panic: goto");
1707 else
1708 gotoprobe = main_root;
1709 break;
1710 }
1711 retop = dofindlabel(gotoprobe, label, enterops);
1712 if (retop)
1713 break;
1714 lastgotoprobe = gotoprobe;
1715 }
1716 if (!retop)
1717 DIE("Can't find label %s", label);
1718
1719 /* pop unwanted frames */
1720
1721 if (ix < cxstack_ix) {
1722 I32 oldsave;
1723
1724 if (ix < 0)
1725 ix = 0;
1726 dounwind(ix);
1727 TOPBLOCK(cx);
1728 oldsave = scopestack[scopestack_ix];
1729 LEAVE_SCOPE(oldsave);
1730 }
1731
1732 /* push wanted frames */
1733
748a9306 1734 if (*enterops && enterops[1]) {
a0d0e21e 1735 OP *oldop = op;
748a9306 1736 for (ix = 1; enterops[ix]; ix++) {
a0d0e21e
LW
1737 op = enterops[ix];
1738 (*op->op_ppaddr)();
1739 }
1740 op = oldop;
1741 }
1742 }
1743
1744 if (do_dump) {
1745 restartop = retop;
1746 do_undump = TRUE;
1747
1748 my_unexec();
1749
1750 restartop = 0; /* hmm, must be GNU unexec().. */
1751 do_undump = FALSE;
1752 }
1753
748a9306
LW
1754 if (stack == signalstack) {
1755 restartop = retop;
1756 longjmp(top_env, 3);
1757 }
1758
a0d0e21e
LW
1759 RETURNOP(retop);
1760}
1761
1762PP(pp_exit)
1763{
1764 dSP;
1765 I32 anum;
1766
1767 if (MAXARG < 1)
1768 anum = 0;
1769 else
1770 anum = SvIVx(POPs);
1771 my_exit(anum);
1772 PUSHs(&sv_undef);
1773 RETURN;
1774}
1775
1776#ifdef NOTYET
1777PP(pp_nswitch)
1778{
1779 dSP;
1780 double value = SvNVx(GvSV(cCOP->cop_gv));
1781 register I32 match = I_32(value);
1782
1783 if (value < 0.0) {
1784 if (((double)match) > value)
1785 --match; /* was fractional--truncate other way */
1786 }
1787 match -= cCOP->uop.scop.scop_offset;
1788 if (match < 0)
1789 match = 0;
1790 else if (match > cCOP->uop.scop.scop_max)
1791 match = cCOP->uop.scop.scop_max;
1792 op = cCOP->uop.scop.scop_next[match];
1793 RETURNOP(op);
1794}
1795
1796PP(pp_cswitch)
1797{
1798 dSP;
1799 register I32 match;
1800
1801 if (multiline)
1802 op = op->op_next; /* can't assume anything */
1803 else {
1804 match = *(SvPVx(GvSV(cCOP->cop_gv), na)) & 255;
1805 match -= cCOP->uop.scop.scop_offset;
1806 if (match < 0)
1807 match = 0;
1808 else if (match > cCOP->uop.scop.scop_max)
1809 match = cCOP->uop.scop.scop_max;
1810 op = cCOP->uop.scop.scop_next[match];
1811 }
1812 RETURNOP(op);
1813}
1814#endif
1815
1816/* Eval. */
1817
1818static void
1819save_lines(array, sv)
1820AV *array;
1821SV *sv;
1822{
1823 register char *s = SvPVX(sv);
1824 register char *send = SvPVX(sv) + SvCUR(sv);
1825 register char *t;
1826 register I32 line = 1;
1827
1828 while (s && s < send) {
1829 SV *tmpstr = NEWSV(85,0);
1830
1831 sv_upgrade(tmpstr, SVt_PVMG);
1832 t = strchr(s, '\n');
1833 if (t)
1834 t++;
1835 else
1836 t = send;
1837
1838 sv_setpvn(tmpstr, s, t - s);
1839 av_store(array, line++, tmpstr);
1840 s = t;
1841 }
1842}
1843
1844static OP *
1845doeval(gimme)
1846int gimme;
1847{
1848 dSP;
1849 OP *saveop = op;
1850 HV *newstash;
748a9306 1851 AV* comppadlist;
a0d0e21e
LW
1852
1853 in_eval = 1;
1854
1855 /* set up a scratch pad */
1856
1857 SAVEINT(padix);
1858 SAVESPTR(curpad);
1859 SAVESPTR(comppad);
1860 SAVESPTR(comppad_name);
1861 SAVEINT(comppad_name_fill);
1862 SAVEINT(min_intro_pending);
1863 SAVEINT(max_intro_pending);
748a9306
LW
1864
1865 SAVESPTR(compcv);
1866 compcv = (CV*)NEWSV(1104,0);
1867 sv_upgrade((SV *)compcv, SVt_PVCV);
1868
a0d0e21e
LW
1869 comppad = newAV();
1870 comppad_name = newAV();
1871 comppad_name_fill = 0;
1872 min_intro_pending = 0;
1873 av_push(comppad, Nullsv);
1874 curpad = AvARRAY(comppad);
1875 padix = 0;
1876
748a9306
LW
1877 comppadlist = newAV();
1878 AvREAL_off(comppadlist);
1879 av_store(comppadlist, 0, SvREFCNT_inc((SV*)comppad_name));
1880 av_store(comppadlist, 1, SvREFCNT_inc((SV*)comppad));
1881 CvPADLIST(compcv) = comppadlist;
1882
a0d0e21e
LW
1883 /* make sure we compile in the right package */
1884
1885 newstash = curcop->cop_stash;
1886 if (curstash != newstash) {
1887 SAVESPTR(curstash);
1888 curstash = newstash;
1889 }
1890 SAVESPTR(beginav);
1891 beginav = newAV();
1892 SAVEFREESV(beginav);
1893
1894 /* try to compile it */
1895
1896 eval_root = Nullop;
1897 error_count = 0;
1898 curcop = &compiling;
1899 curcop->cop_arybase = 0;
1900 rs = "\n";
1901 rslen = 1;
1902 rschar = '\n';
1903 rspara = 0;
1904 sv_setpv(GvSV(gv_fetchpv("@",TRUE, SVt_PV)),"");
1905 if (yyparse() || error_count || !eval_root) {
1906 SV **newsp;
1907 I32 gimme;
1908 CONTEXT *cx;
1909 I32 optype;
1910
1911 op = saveop;
1912 if (eval_root) {
1913 op_free(eval_root);
1914 eval_root = Nullop;
1915 }
1916 POPBLOCK(cx,curpm);
1917 POPEVAL(cx);
1918 pop_return();
1919 lex_end();
1920 LEAVE;
1921 if (optype == OP_REQUIRE)
1922 DIE("%s", SvPVx(GvSV(gv_fetchpv("@",TRUE, SVt_PV)), na));
1923 rs = nrs;
1924 rslen = nrslen;
1925 rschar = nrschar;
1926 rspara = (nrslen == 2);
1927 RETPUSHUNDEF;
1928 }
1929 rs = nrs;
1930 rslen = nrslen;
1931 rschar = nrschar;
1932 rspara = (nrslen == 2);
1933 compiling.cop_line = 0;
748a9306 1934 SAVEFREESV(compcv);
a0d0e21e
LW
1935 SAVEFREEOP(eval_root);
1936 if (gimme & G_ARRAY)
1937 list(eval_root);
1938 else
1939 scalar(eval_root);
1940
1941 DEBUG_x(dump_eval());
1942
1943 /* compiled okay, so do it */
1944
1945 RETURNOP(eval_start);
1946}
1947
1948PP(pp_require)
1949{
1950 dSP;
1951 register CONTEXT *cx;
1952 SV *sv;
1953 char *name;
1954 char *tmpname;
1955 SV** svp;
1956 I32 gimme = G_SCALAR;
1957 FILE *tryrsfp = 0;
1958
1959 sv = POPs;
1960 if (SvNIOK(sv) && !SvPOKp(sv)) {
1961 if (atof(patchlevel) + 0.000999 < SvNV(sv))
1962 DIE("Perl %3.3f required--this is only version %s, stopped",
1963 SvNV(sv),patchlevel);
1964 RETPUSHYES;
1965 }
1966 name = SvPV(sv, na);
1967 if (!*name)
1968 DIE("Null filename used");
1969 if (op->op_type == OP_REQUIRE &&
1970 (svp = hv_fetch(GvHVn(incgv), name, SvCUR(sv), 0)) &&
1971 *svp != &sv_undef)
1972 RETPUSHYES;
1973
1974 /* prepare to compile file */
1975
1976 tmpname = savepv(name);
1977 if (*tmpname == '/' ||
1978 (*tmpname == '.' &&
1979 (tmpname[1] == '/' ||
748a9306
LW
1980 (tmpname[1] == '.' && tmpname[2] == '/')))
1981#ifdef VMS
1982 || ((*tmpname == '[' || *tmpname == '<') &&
1983 (tmpname[1] == '-' || tmpname[1] == ']' || tmpname[1] == '>'))
1984#endif
1985 )
a0d0e21e
LW
1986 {
1987 tryrsfp = fopen(tmpname,"r");
1988 }
1989 else {
1990 AV *ar = GvAVn(incgv);
1991 I32 i;
1992
1993 for (i = 0; i <= AvFILL(ar); i++) {
748a9306
LW
1994#ifdef VMS
1995 if (tounixpath_ts(SvPVx(*av_fetch(ar, i, TRUE), na),buf) == NULL)
1996 croak("Error converting file specification %s",
1997 SvPVx(*av_fetch(ar, i, TRUE), na));
1998 strcat(buf,name);
1999#else
a0d0e21e
LW
2000 (void)sprintf(buf, "%s/%s",
2001 SvPVx(*av_fetch(ar, i, TRUE), na), name);
748a9306 2002#endif
a0d0e21e
LW
2003 tryrsfp = fopen(buf, "r");
2004 if (tryrsfp) {
2005 char *s = buf;
2006
2007 if (*s == '.' && s[1] == '/')
2008 s += 2;
2009 Safefree(tmpname);
2010 tmpname = savepv(s);
2011 break;
2012 }
2013 }
2014 }
2015 SAVESPTR(compiling.cop_filegv);
2016 compiling.cop_filegv = gv_fetchfile(tmpname);
2017 Safefree(tmpname);
2018 tmpname = Nullch;
2019 if (!tryrsfp) {
2020 if (op->op_type == OP_REQUIRE) {
2021 sprintf(tokenbuf,"Can't locate %s in @INC", name);
2022 if (instr(tokenbuf,".h "))
2023 strcat(tokenbuf," (change .h to .ph maybe?)");
2024 if (instr(tokenbuf,".ph "))
2025 strcat(tokenbuf," (did you run h2ph?)");
2026 DIE("%s",tokenbuf);
2027 }
2028
2029 RETPUSHUNDEF;
2030 }
2031
2032 /* Assume success here to prevent recursive requirement. */
2033 (void)hv_store(GvHVn(incgv), name, strlen(name),
2034 newSVsv(GvSV(compiling.cop_filegv)), 0 );
2035
2036 ENTER;
2037 SAVETMPS;
2038 lex_start(sv_2mortal(newSVpv("",0)));
2039 rsfp = tryrsfp;
2040 name = savepv(name);
2041 SAVEFREEPV(name);
2042 SAVEI32(hints);
2043 hints = 0;
2044
2045 /* switch to eval mode */
2046
2047 push_return(op->op_next);
2048 PUSHBLOCK(cx, CXt_EVAL, SP);
2049 PUSHEVAL(cx, name, compiling.cop_filegv);
2050
2051 compiling.cop_line = 0;
2052
2053 PUTBACK;
2054 return doeval(G_SCALAR);
2055}
2056
2057PP(pp_dofile)
2058{
2059 return pp_require(ARGS);
2060}
2061
2062PP(pp_entereval)
2063{
2064 dSP;
2065 register CONTEXT *cx;
2066 dPOPss;
2067 I32 gimme = GIMME;
2068 char tmpbuf[32];
2069 STRLEN len;
2070
2071 if (!SvPV(sv,len) || !len)
2072 RETPUSHUNDEF;
748a9306 2073 TAINT_PROPER("eval");
a0d0e21e
LW
2074
2075 ENTER;
a0d0e21e 2076 lex_start(sv);
748a9306 2077 SAVETMPS;
a0d0e21e
LW
2078
2079 /* switch to eval mode */
2080
748a9306 2081 SAVESPTR(compiling.cop_filegv);
a0d0e21e
LW
2082 sprintf(tmpbuf, "_<(eval %d)", ++evalseq);
2083 compiling.cop_filegv = gv_fetchfile(tmpbuf+2);
2084 compiling.cop_line = 1;
2085 SAVEDELETE(defstash, savepv(tmpbuf), strlen(tmpbuf));
2086 SAVEI32(hints);
2087 hints = op->op_targ;
2088
2089 push_return(op->op_next);
2090 PUSHBLOCK(cx, CXt_EVAL, SP);
2091 PUSHEVAL(cx, 0, compiling.cop_filegv);
2092
2093 /* prepare to compile string */
2094
2095 if (perldb && curstash != debstash)
2096 save_lines(GvAV(compiling.cop_filegv), linestr);
2097 PUTBACK;
2098 return doeval(gimme);
2099}
2100
2101PP(pp_leaveeval)
2102{
2103 dSP;
2104 register SV **mark;
2105 SV **newsp;
2106 PMOP *newpm;
2107 I32 gimme;
2108 register CONTEXT *cx;
2109 OP *retop;
2110 I32 optype;
2111
2112 POPBLOCK(cx,newpm);
2113 POPEVAL(cx);
2114 retop = pop_return();
2115
2116 if (gimme == G_SCALAR) {
2117 if (op->op_private & OPpLEAVE_VOID)
2118 MARK = newsp;
2119 else {
2120 MARK = newsp + 1;
2121 if (MARK <= SP) {
2122 if (SvFLAGS(TOPs) & SVs_TEMP)
2123 *MARK = TOPs;
2124 else
2125 *MARK = sv_mortalcopy(TOPs);
2126 }
2127 else {
2128 MEXTEND(mark,0);
2129 *MARK = &sv_undef;
2130 }
2131 }
2132 SP = MARK;
2133 }
2134 else {
2135 for (mark = newsp + 1; mark <= SP; mark++)
2136 if (!(SvFLAGS(TOPs) & SVs_TEMP))
2137 *mark = sv_mortalcopy(*mark);
2138 /* in case LEAVE wipes old return values */
2139 }
2140 curpm = newpm; /* Don't pop $1 et al till now */
2141
2142 if (optype != OP_ENTEREVAL) {
2143 char *name = cx->blk_eval.old_name;
2144
2145 if (!(gimme == G_SCALAR ? SvTRUE(*sp) : sp > newsp)) {
2146 /* Unassume the success we assumed earlier. */
748a9306 2147 (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
a0d0e21e
LW
2148
2149 if (optype == OP_REQUIRE)
2150 retop = die("%s did not return a true value", name);
2151 }
2152 }
2153
2154 lex_end();
2155 LEAVE;
2156 sv_setpv(GvSV(gv_fetchpv("@",TRUE, SVt_PV)),"");
2157
2158 RETURNOP(retop);
2159}
2160
a0d0e21e
LW
2161PP(pp_entertry)
2162{
2163 dSP;
2164 register CONTEXT *cx;
2165 I32 gimme = GIMME;
2166
2167 ENTER;
2168 SAVETMPS;
2169
2170 push_return(cLOGOP->op_other->op_next);
2171 PUSHBLOCK(cx, CXt_EVAL, SP);
2172 PUSHEVAL(cx, 0, 0);
2173 eval_root = op; /* Only needed so that goto works right. */
2174
2175 in_eval = 1;
2176 sv_setpv(GvSV(gv_fetchpv("@",TRUE, SVt_PV)),"");
2177 RETURN;
2178}
2179
2180PP(pp_leavetry)
2181{
2182 dSP;
2183 register SV **mark;
2184 SV **newsp;
2185 PMOP *newpm;
2186 I32 gimme;
2187 register CONTEXT *cx;
2188 I32 optype;
2189
2190 POPBLOCK(cx,newpm);
2191 POPEVAL(cx);
2192 pop_return();
2193
2194 if (gimme == G_SCALAR) {
2195 if (op->op_private & OPpLEAVE_VOID)
2196 MARK = newsp;
2197 else {
2198 MARK = newsp + 1;
2199 if (MARK <= SP) {
2200 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
2201 *MARK = TOPs;
2202 else
2203 *MARK = sv_mortalcopy(TOPs);
2204 }
2205 else {
2206 MEXTEND(mark,0);
2207 *MARK = &sv_undef;
2208 }
2209 }
2210 SP = MARK;
2211 }
2212 else {
2213 for (mark = newsp + 1; mark <= SP; mark++)
2214 if (!(SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP)))
2215 *mark = sv_mortalcopy(*mark);
2216 /* in case LEAVE wipes old return values */
2217 }
2218 curpm = newpm; /* Don't pop $1 et al till now */
2219
2220 LEAVE;
2221 sv_setpv(GvSV(gv_fetchpv("@",TRUE, SVt_PV)),"");
2222 RETURN;
2223}
2224
2225static void
2226doparseform(sv)
2227SV *sv;
2228{
2229 STRLEN len;
2230 register char *s = SvPV_force(sv, len);
2231 register char *send = s + len;
2232 register char *base;
2233 register I32 skipspaces = 0;
2234 bool noblank;
2235 bool repeat;
2236 bool postspace = FALSE;
2237 U16 *fops;
2238 register U16 *fpc;
2239 U16 *linepc;
2240 register I32 arg;
2241 bool ischop;
2242
2243 New(804, fops, (send - s)*3+2, U16); /* Almost certainly too long... */
2244 fpc = fops;
2245
2246 if (s < send) {
2247 linepc = fpc;
2248 *fpc++ = FF_LINEMARK;
2249 noblank = repeat = FALSE;
2250 base = s;
2251 }
2252
2253 while (s <= send) {
2254 switch (*s++) {
2255 default:
2256 skipspaces = 0;
2257 continue;
2258
2259 case '~':
2260 if (*s == '~') {
2261 repeat = TRUE;
2262 *s = ' ';
2263 }
2264 noblank = TRUE;
2265 s[-1] = ' ';
2266 /* FALL THROUGH */
2267 case ' ': case '\t':
2268 skipspaces++;
2269 continue;
2270
2271 case '\n': case 0:
2272 arg = s - base;
2273 skipspaces++;
2274 arg -= skipspaces;
2275 if (arg) {
2276 if (postspace) {
2277 *fpc++ = FF_SPACE;
2278 postspace = FALSE;
2279 }
2280 *fpc++ = FF_LITERAL;
2281 *fpc++ = arg;
2282 }
2283 if (s <= send)
2284 skipspaces--;
2285 if (skipspaces) {
2286 *fpc++ = FF_SKIP;
2287 *fpc++ = skipspaces;
2288 }
2289 skipspaces = 0;
2290 if (s <= send)
2291 *fpc++ = FF_NEWLINE;
2292 if (noblank) {
2293 *fpc++ = FF_BLANK;
2294 if (repeat)
2295 arg = fpc - linepc + 1;
2296 else
2297 arg = 0;
2298 *fpc++ = arg;
2299 }
2300 if (s < send) {
2301 linepc = fpc;
2302 *fpc++ = FF_LINEMARK;
2303 noblank = repeat = FALSE;
2304 base = s;
2305 }
2306 else
2307 s++;
2308 continue;
2309
2310 case '@':
2311 case '^':
2312 ischop = s[-1] == '^';
2313
2314 if (postspace) {
2315 *fpc++ = FF_SPACE;
2316 postspace = FALSE;
2317 }
2318 arg = (s - base) - 1;
2319 if (arg) {
2320 *fpc++ = FF_LITERAL;
2321 *fpc++ = arg;
2322 }
2323
2324 base = s - 1;
2325 *fpc++ = FF_FETCH;
2326 if (*s == '*') {
2327 s++;
2328 *fpc++ = 0;
2329 *fpc++ = FF_LINEGLOB;
2330 }
2331 else if (*s == '#' || (*s == '.' && s[1] == '#')) {
2332 arg = ischop ? 512 : 0;
2333 base = s - 1;
2334 while (*s == '#')
2335 s++;
2336 if (*s == '.') {
2337 char *f;
2338 s++;
2339 f = s;
2340 while (*s == '#')
2341 s++;
2342 arg |= 256 + (s - f);
2343 }
2344 *fpc++ = s - base; /* fieldsize for FETCH */
2345 *fpc++ = FF_DECIMAL;
2346 *fpc++ = arg;
2347 }
2348 else {
2349 I32 prespace = 0;
2350 bool ismore = FALSE;
2351
2352 if (*s == '>') {
2353 while (*++s == '>') ;
2354 prespace = FF_SPACE;
2355 }
2356 else if (*s == '|') {
2357 while (*++s == '|') ;
2358 prespace = FF_HALFSPACE;
2359 postspace = TRUE;
2360 }
2361 else {
2362 if (*s == '<')
2363 while (*++s == '<') ;
2364 postspace = TRUE;
2365 }
2366 if (*s == '.' && s[1] == '.' && s[2] == '.') {
2367 s += 3;
2368 ismore = TRUE;
2369 }
2370 *fpc++ = s - base; /* fieldsize for FETCH */
2371
2372 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
2373
2374 if (prespace)
2375 *fpc++ = prespace;
2376 *fpc++ = FF_ITEM;
2377 if (ismore)
2378 *fpc++ = FF_MORE;
2379 if (ischop)
2380 *fpc++ = FF_CHOP;
2381 }
2382 base = s;
2383 skipspaces = 0;
2384 continue;
2385 }
2386 }
2387 *fpc++ = FF_END;
2388
2389 arg = fpc - fops;
2390 { /* need to jump to the next word */
2391 int z;
2392 z = WORD_ALIGN - SvCUR(sv) % WORD_ALIGN;
2393 SvGROW(sv, SvCUR(sv) + z + arg * sizeof(U16) + 4);
2394 s = SvPVX(sv) + SvCUR(sv) + z;
2395 }
2396 Copy(fops, s, arg, U16);
2397 Safefree(fops);
2398 SvCOMPILED_on(sv);
2399}
2400