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