perl 5.003_06: utils/h2xs.PL
[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;
c07a80fd 105 register REGEXP *rx = cx->sb_rx;
a0d0e21e 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)
760ac839 215 PerlIO_printf(PerlIO_stderr(), "%-16s%ld\n", name, (long) arg);
a0d0e21e 216 else
760ac839 217 PerlIO_printf(PerlIO_stderr(), "%-16s\n", name);
a0d0e21e 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();
e5cf08de 577 gv_efullname3(tmpstr, gv, Nullch);
a0d0e21e 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
1ce6579f 624 oldstack = curstack;
a0d0e21e 625 if (!sortstack) {
626 sortstack = newAV();
627 AvREAL_off(sortstack);
628 av_extend(sortstack, 32);
629 }
1ce6579f 630 SWITCHSTACK(curstack, sortstack);
a0d0e21e 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--];
760ac839 884 DEBUG_l(PerlIO_printf(Perl_debug_log, "Unwinding block %ld, type %s\n", (long) cxstack_ix+1,
a0d0e21e 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
1ce6579f 922 /* We have to switch back to mainstack or die_where may try to pop
923 * the eval block from the wrong stack if die is being called from a
924 * signal handler. - dkindred@cs.cmu.edu */
925 if (curstack != mainstack) {
926 dSP;
927 SWITCHSTACK(curstack, mainstack);
928 }
a0d0e21e 929#ifdef I_STDARG
930 va_start(args, pat);
931#else
932 va_start(args);
933#endif
934 message = mess(pat, &args);
935 va_end(args);
748a9306 936 if (diehook && (cv = sv_2cv(diehook, &stash, &gv, 0)) && !CvDEPTH(cv)) {
937 dSP;
938
939 PUSHMARK(sp);
940 EXTEND(sp, 1);
941 PUSHs(sv_2mortal(newSVpv(message,0)));
942 PUTBACK;
943 perl_call_sv((SV*)cv, G_DISCARD);
944 }
a0d0e21e 945 restartop = die_where(message);
946 if ((!restartop && was_in_eval) || oldrunlevel > 1)
a5f75d66 947 Siglongjmp(top_env, 3);
a0d0e21e 948 return restartop;
949}
950
951OP *
952die_where(message)
953char *message;
954{
955 if (in_eval) {
956 I32 cxix;
957 register CONTEXT *cx;
958 I32 gimme;
959 SV **newsp;
960
4633a7c4 961 if (in_eval & 4) {
962 SV **svp;
963 STRLEN klen = strlen(message);
964
965 svp = hv_fetch(GvHV(errgv), message, klen, TRUE);
966 if (svp) {
967 if (!SvIOK(*svp)) {
968 static char prefix[] = "\t(in cleanup) ";
969 sv_upgrade(*svp, SVt_IV);
970 (void)SvIOK_only(*svp);
971 SvGROW(GvSV(errgv), SvCUR(GvSV(errgv))+sizeof(prefix)+klen);
972 sv_catpvn(GvSV(errgv), prefix, sizeof(prefix)-1);
973 sv_catpvn(GvSV(errgv), message, klen);
974 }
975 sv_inc(*svp);
976 }
977 }
978 else
c07a80fd 979 sv_setpv(GvSV(errgv), message);
4633a7c4 980
a0d0e21e 981 cxix = dopoptoeval(cxstack_ix);
982 if (cxix >= 0) {
983 I32 optype;
984
985 if (cxix < cxstack_ix)
986 dounwind(cxix);
987
988 POPBLOCK(cx,curpm);
989 if (cx->cx_type != CXt_EVAL) {
760ac839 990 PerlIO_printf(PerlIO_stderr(), "panic: die %s", message);
a0d0e21e 991 my_exit(1);
992 }
993 POPEVAL(cx);
994
995 if (gimme == G_SCALAR)
996 *++newsp = &sv_undef;
997 stack_sp = newsp;
998
999 LEAVE;
748a9306 1000
a0d0e21e 1001 if (optype == OP_REQUIRE)
4633a7c4 1002 DIE("%s", SvPVx(GvSV(errgv), na));
a0d0e21e 1003 return pop_return();
1004 }
1005 }
760ac839 1006 PerlIO_printf(PerlIO_stderr(), "%s",message);
1007 PerlIO_flush(PerlIO_stderr());
a6c477ed 1008 if (e_tmpname) {
1009 if (e_fp) {
760ac839 1010 PerlIO_close(e_fp);
a6c477ed 1011 e_fp = Nullfp;
1012 }
a0d0e21e 1013 (void)UNLINK(e_tmpname);
a6c477ed 1014 Safefree(e_tmpname);
1015 e_tmpname = Nullch;
e632a5f6 1016 }
748a9306 1017 statusvalue = SHIFTSTATUS(statusvalue);
1018#ifdef VMS
1019 my_exit((U32)vaxc$errno?vaxc$errno:errno?errno:statusvalue?statusvalue:SS$_ABORT);
1020#else
a0d0e21e 1021 my_exit((I32)((errno&255)?errno:((statusvalue&255)?statusvalue:255)));
748a9306 1022#endif
a0d0e21e 1023 return 0;
1024}
1025
1026PP(pp_xor)
1027{
1028 dSP; dPOPTOPssrl;
1029 if (SvTRUE(left) != SvTRUE(right))
1030 RETSETYES;
1031 else
1032 RETSETNO;
1033}
1034
1035PP(pp_andassign)
1036{
1037 dSP;
1038 if (!SvTRUE(TOPs))
1039 RETURN;
1040 else
1041 RETURNOP(cLOGOP->op_other);
1042}
1043
1044PP(pp_orassign)
1045{
1046 dSP;
1047 if (SvTRUE(TOPs))
1048 RETURN;
1049 else
1050 RETURNOP(cLOGOP->op_other);
1051}
1052
1053#ifdef DEPRECATED
1054PP(pp_entersubr)
1055{
1056 dSP;
1057 SV** mark = (stack_base + *markstack_ptr + 1);
1058 SV* cv = *mark;
1059 while (mark < sp) { /* emulate old interface */
1060 *mark = mark[1];
1061 mark++;
1062 }
1063 *sp = cv;
1064 return pp_entersub();
1065}
1066#endif
1067
1068PP(pp_caller)
1069{
1070 dSP;
1071 register I32 cxix = dopoptosub(cxstack_ix);
1072 register CONTEXT *cx;
1073 I32 dbcxix;
1074 SV *sv;
1075 I32 count = 0;
1076
1077 if (MAXARG)
1078 count = POPi;
1079 EXTEND(SP, 6);
1080 for (;;) {
1081 if (cxix < 0) {
1082 if (GIMME != G_ARRAY)
1083 RETPUSHUNDEF;
1084 RETURN;
1085 }
1086 if (DBsub && cxix >= 0 &&
1087 cxstack[cxix].blk_sub.cv == GvCV(DBsub))
1088 count++;
1089 if (!count--)
1090 break;
1091 cxix = dopoptosub(cxix - 1);
1092 }
1093 cx = &cxstack[cxix];
06a5b730 1094 if (cxstack[cxix].cx_type == CXt_SUB) {
1095 dbcxix = dopoptosub(cxix - 1);
1096 /* We expect that cxstack[dbcxix] is CXt_SUB, anyway, the
1097 field below is defined for any cx. */
1098 if (DBsub && dbcxix >= 0 && cxstack[dbcxix].blk_sub.cv == GvCV(DBsub))
1099 cx = &cxstack[dbcxix];
1100 }
1101
a0d0e21e 1102 if (GIMME != G_ARRAY) {
1103 dTARGET;
1104
1105 sv_setpv(TARG, HvNAME(cx->blk_oldcop->cop_stash));
1106 PUSHs(TARG);
1107 RETURN;
1108 }
a0d0e21e 1109
1110 PUSHs(sv_2mortal(newSVpv(HvNAME(cx->blk_oldcop->cop_stash), 0)));
1111 PUSHs(sv_2mortal(newSVpv(SvPVX(GvSV(cx->blk_oldcop->cop_filegv)), 0)));
1112 PUSHs(sv_2mortal(newSViv((I32)cx->blk_oldcop->cop_line)));
1113 if (!MAXARG)
1114 RETURN;
06a5b730 1115 if (cx->cx_type == CXt_SUB) { /* So is cxstack[dbcxix]. */
a0d0e21e 1116 sv = NEWSV(49, 0);
e5cf08de 1117 gv_efullname3(sv, CvGV(cxstack[cxix].blk_sub.cv), Nullch);
a0d0e21e 1118 PUSHs(sv_2mortal(sv));
1119 PUSHs(sv_2mortal(newSViv((I32)cx->blk_sub.hasargs)));
1120 }
1121 else {
1122 PUSHs(sv_2mortal(newSVpv("(eval)",0)));
1123 PUSHs(sv_2mortal(newSViv(0)));
1124 }
1125 PUSHs(sv_2mortal(newSViv((I32)cx->blk_gimme)));
4633a7c4 1126 if (cx->cx_type == CXt_EVAL) {
06a5b730 1127 if (cx->blk_eval.old_op_type == OP_ENTEREVAL) {
4633a7c4 1128 PUSHs(cx->blk_eval.cur_text);
06a5b730 1129 PUSHs(&sv_no);
1130 }
1131 else if (cx->blk_eval.old_name) { /* Try blocks have old_name == 0. */
1132 /* Require, put the name. */
1133 PUSHs(sv_2mortal(newSVpv(cx->blk_eval.old_name, 0)));
1134 PUSHs(&sv_yes);
1135 }
4633a7c4 1136 }
1137 else if (cx->cx_type == CXt_SUB &&
1138 cx->blk_sub.hasargs &&
1139 curcop->cop_stash == debstash)
1140 {
a0d0e21e 1141 AV *ary = cx->blk_sub.argarray;
1142 int off = AvARRAY(ary) - AvALLOC(ary);
1143
1144 if (!dbargs) {
1145 GV* tmpgv;
1146 dbargs = GvAV(gv_AVadd(tmpgv = gv_fetchpv("DB::args", TRUE,
1147 SVt_PVAV)));
a5f75d66 1148 GvMULTI_on(tmpgv);
a0d0e21e 1149 AvREAL_off(dbargs); /* XXX Should be REIFY */
1150 }
1151
1152 if (AvMAX(dbargs) < AvFILL(ary) + off)
1153 av_extend(dbargs, AvFILL(ary) + off);
1154 Copy(AvALLOC(ary), AvARRAY(dbargs), AvFILL(ary) + 1 + off, SV*);
1155 AvFILL(dbargs) = AvFILL(ary) + off;
1156 }
1157 RETURN;
1158}
1159
1160static int
1161sortcv(a, b)
1162const void *a;
1163const void *b;
1164{
1165 SV **str1 = (SV **) a;
1166 SV **str2 = (SV **) b;
748a9306 1167 I32 oldsaveix = savestack_ix;
a0d0e21e 1168 I32 oldscopeix = scopestack_ix;
1169 I32 result;
1170 GvSV(firstgv) = *str1;
1171 GvSV(secondgv) = *str2;
1172 stack_sp = stack_base;
1173 op = sortcop;
a6c477ed 1174 runops();
a0d0e21e 1175 if (stack_sp != stack_base + 1)
1176 croak("Sort subroutine didn't return single value");
748a9306 1177 if (!SvNIOKp(*stack_sp))
a0d0e21e 1178 croak("Sort subroutine didn't return a numeric value");
1179 result = SvIV(*stack_sp);
1180 while (scopestack_ix > oldscopeix) {
1181 LEAVE;
1182 }
748a9306 1183 leave_scope(oldsaveix);
a0d0e21e 1184 return result;
1185}
1186
1187static int
1188sortcmp(a, b)
1189const void *a;
1190const void *b;
1191{
1192 register SV *str1 = *(SV **) a;
1193 register SV *str2 = *(SV **) b;
1194 I32 retval;
1195
4633a7c4 1196 if (!SvPOKp(str1)) {
1197 if (!SvPOKp(str2))
1198 return 0;
1199 else
1200 return -1;
1201 }
1202 if (!SvPOKp(str2))
1203 return 1;
1204
e5cf08de 1205 if (lc_collate_active) { /* NOTE: this is the LC_COLLATE branch */
1206 register char * pv1, * pv2, * pvx;
1207 STRLEN cur1, cur2, curx;
1208
1209 pv1 = SvPV(str1, cur1);
1210 pvx = mem_collxfrm(pv1, cur1, &curx);
1211 pv1 = pvx;
1212 cur1 = curx;
1213
1214 pv2 = SvPV(str2, cur2);
1215 pvx = mem_collxfrm(pv2, cur2, &curx);
1216 pv2 = pvx;
1217 cur2 = curx;
1218
1219 retval = memcmp((void *)pv1, (void *)pv2, cur1 < cur2 ? cur1 : cur2);
1220
1221 Safefree(pv1);
1222 Safefree(pv2);
1223
1224 if (retval)
1225 return retval < 0 ? -1 : 1;
1226
1227 if (cur1 == cur2)
1228 return 0;
1229 else
1230 return cur1 < cur2 ? -1 : 1;
1231 }
1232
1233 /* NOTE: this is the non-LC_COLLATE area */
1234
a0d0e21e 1235 if (SvCUR(str1) < SvCUR(str2)) {
1236 /*SUPPRESS 560*/
1237 if (retval = memcmp(SvPVX(str1), SvPVX(str2), SvCUR(str1)))
1238 return retval;
1239 else
1240 return -1;
1241 }
1242 /*SUPPRESS 560*/
1243 else if (retval = memcmp(SvPVX(str1), SvPVX(str2), SvCUR(str2)))
1244 return retval;
1245 else if (SvCUR(str1) == SvCUR(str2))
1246 return 0;
1247 else
1248 return 1;
1249}
1250
1251PP(pp_reset)
1252{
1253 dSP;
1254 char *tmps;
1255
1256 if (MAXARG < 1)
1257 tmps = "";
1258 else
1259 tmps = POPp;
1260 sv_reset(tmps, curcop->cop_stash);
1261 PUSHs(&sv_yes);
1262 RETURN;
1263}
1264
1265PP(pp_lineseq)
1266{
1267 return NORMAL;
1268}
1269
1270PP(pp_dbstate)
1271{
1272 curcop = (COP*)op;
1273 TAINT_NOT; /* Each statement is presumed innocent */
1274 stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
1275 FREETMPS;
1276
1277 if (op->op_private || SvIV(DBsingle) || SvIV(DBsignal) || SvIV(DBtrace))
1278 {
1279 SV **sp;
1280 register CV *cv;
1281 register CONTEXT *cx;
748a9306 1282 I32 gimme = G_ARRAY;
a0d0e21e 1283 I32 hasargs;
1284 GV *gv;
1285
a0d0e21e 1286 gv = DBgv;
1287 cv = GvCV(gv);
a0d0e21e 1288 if (!cv)
1289 DIE("No DB::DB routine defined");
1290
06a5b730 1291 if (CvDEPTH(cv) >= 1 && !(debug & (1<<30))) /* don't do recursive DB::DB call */
a0d0e21e 1292 return NORMAL;
748a9306 1293
4633a7c4 1294 ENTER;
1295 SAVETMPS;
1296
748a9306 1297 SAVEI32(debug);
1298 SAVESPTR(stack_sp);
1299 debug = 0;
1300 hasargs = 0;
1301 sp = stack_sp;
1302
a0d0e21e 1303 push_return(op->op_next);
748a9306 1304 PUSHBLOCK(cx, CXt_SUB, sp);
a0d0e21e 1305 PUSHSUB(cx);
1306 CvDEPTH(cv)++;
1307 (void)SvREFCNT_inc(cv);
1308 SAVESPTR(curpad);
1309 curpad = AvARRAY((AV*)*av_fetch(CvPADLIST(cv),1,FALSE));
1310 RETURNOP(CvSTART(cv));
1311 }
1312 else
1313 return NORMAL;
1314}
1315
1316PP(pp_scope)
1317{
1318 return NORMAL;
1319}
1320
1321PP(pp_enteriter)
1322{
1323 dSP; dMARK;
1324 register CONTEXT *cx;
1325 I32 gimme = GIMME;
1326 SV **svp;
1327
4633a7c4 1328 ENTER;
1329 SAVETMPS;
1330
a0d0e21e 1331 if (op->op_targ)
1332 svp = &curpad[op->op_targ]; /* "my" variable */
1333 else
1334 svp = &GvSV((GV*)POPs); /* symbol table variable */
1335
4633a7c4 1336 SAVESPTR(*svp);
1337
a0d0e21e 1338 ENTER;
1339
1340 PUSHBLOCK(cx, CXt_LOOP, SP);
1341 PUSHLOOP(cx, svp, MARK);
4633a7c4 1342 if (op->op_flags & OPf_STACKED) {
1343 AV* av = (AV*)POPs;
1344 cx->blk_loop.iterary = av;
1345 cx->blk_loop.iterix = -1;
1346 }
1347 else {
1ce6579f 1348 cx->blk_loop.iterary = curstack;
1349 AvFILL(curstack) = sp - stack_base;
4633a7c4 1350 cx->blk_loop.iterix = MARK - stack_base;
1351 }
a0d0e21e 1352
1353 RETURN;
1354}
1355
1356PP(pp_enterloop)
1357{
1358 dSP;
1359 register CONTEXT *cx;
1360 I32 gimme = GIMME;
1361
1362 ENTER;
1363 SAVETMPS;
1364 ENTER;
1365
1366 PUSHBLOCK(cx, CXt_LOOP, SP);
1367 PUSHLOOP(cx, 0, SP);
1368
1369 RETURN;
1370}
1371
1372PP(pp_leaveloop)
1373{
1374 dSP;
1375 register CONTEXT *cx;
1376 I32 gimme;
1377 SV **newsp;
1378 PMOP *newpm;
1379 SV **mark;
1380
1381 POPBLOCK(cx,newpm);
1382 mark = newsp;
1383 POPLOOP(cx);
1384 if (gimme == G_SCALAR) {
1385 if (op->op_private & OPpLEAVE_VOID)
1386 ;
1387 else {
1388 if (mark < SP)
1389 *++newsp = sv_mortalcopy(*SP);
1390 else
1391 *++newsp = &sv_undef;
1392 }
1393 }
1394 else {
1395 while (mark < SP)
1396 *++newsp = sv_mortalcopy(*++mark);
1397 }
1398 curpm = newpm; /* Don't pop $1 et al till now */
1399 sp = newsp;
1400 LEAVE;
1401 LEAVE;
1402
1403 RETURN;
1404}
1405
1406PP(pp_return)
1407{
1408 dSP; dMARK;
1409 I32 cxix;
1410 register CONTEXT *cx;
1411 I32 gimme;
1412 SV **newsp;
1413 PMOP *newpm;
1414 I32 optype = 0;
1415
1ce6579f 1416 if (curstack == sortstack) {
a0d0e21e 1417 if (cxstack_ix == sortcxix || dopoptosub(cxstack_ix) < sortcxix) {
16d20bd9 1418 if (cxstack_ix > sortcxix)
1419 dounwind(sortcxix);
1ce6579f 1420 AvARRAY(curstack)[1] = *SP;
a0d0e21e 1421 stack_sp = stack_base + 1;
1422 return 0;
1423 }
1424 }
1425
1426 cxix = dopoptosub(cxstack_ix);
1427 if (cxix < 0)
1428 DIE("Can't return outside a subroutine");
1429 if (cxix < cxstack_ix)
1430 dounwind(cxix);
1431
1432 POPBLOCK(cx,newpm);
1433 switch (cx->cx_type) {
1434 case CXt_SUB:
1435 POPSUB(cx);
1436 break;
1437 case CXt_EVAL:
1438 POPEVAL(cx);
748a9306 1439 if (optype == OP_REQUIRE &&
1440 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
1441 {
1442 char *name = cx->blk_eval.old_name;
1443 (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
1444 DIE("%s did not return a true value", name);
1445 }
a0d0e21e 1446 break;
1447 default:
1448 DIE("panic: return");
1449 break;
1450 }
1451
1452 if (gimme == G_SCALAR) {
1453 if (MARK < SP)
1454 *++newsp = sv_mortalcopy(*SP);
1455 else
1456 *++newsp = &sv_undef;
a0d0e21e 1457 }
1458 else {
a0d0e21e 1459 while (MARK < SP)
1460 *++newsp = sv_mortalcopy(*++MARK);
1461 }
1462 curpm = newpm; /* Don't pop $1 et al till now */
1463 stack_sp = newsp;
1464
1465 LEAVE;
1466 return pop_return();
1467}
1468
1469PP(pp_last)
1470{
1471 dSP;
1472 I32 cxix;
1473 register CONTEXT *cx;
1474 I32 gimme;
1475 I32 optype;
1476 OP *nextop;
1477 SV **newsp;
1478 PMOP *newpm;
1479 SV **mark = stack_base + cxstack[cxstack_ix].blk_oldsp;
a0d0e21e 1480
1481 if (op->op_flags & OPf_SPECIAL) {
1482 cxix = dopoptoloop(cxstack_ix);
1483 if (cxix < 0)
1484 DIE("Can't \"last\" outside a block");
1485 }
1486 else {
1487 cxix = dopoptolabel(cPVOP->op_pv);
1488 if (cxix < 0)
1489 DIE("Label not found for \"last %s\"", cPVOP->op_pv);
1490 }
1491 if (cxix < cxstack_ix)
1492 dounwind(cxix);
1493
1494 POPBLOCK(cx,newpm);
1495 switch (cx->cx_type) {
1496 case CXt_LOOP:
1497 POPLOOP(cx);
1498 nextop = cx->blk_loop.last_op->op_next;
1499 LEAVE;
1500 break;
1501 case CXt_EVAL:
1502 POPEVAL(cx);
1503 nextop = pop_return();
1504 break;
1505 case CXt_SUB:
1506 POPSUB(cx);
1507 nextop = pop_return();
1508 break;
1509 default:
1510 DIE("panic: last");
1511 break;
1512 }
1513
1514 if (gimme == G_SCALAR) {
1515 if (mark < SP)
1516 *++newsp = sv_mortalcopy(*SP);
1517 else
1518 *++newsp = &sv_undef;
1519 }
1520 else {
1521 while (mark < SP)
1522 *++newsp = sv_mortalcopy(*++mark);
1523 }
1524 curpm = newpm; /* Don't pop $1 et al till now */
1525 sp = newsp;
1526
1527 LEAVE;
1528 RETURNOP(nextop);
1529}
1530
1531PP(pp_next)
1532{
1533 I32 cxix;
1534 register CONTEXT *cx;
1535 I32 oldsave;
1536
1537 if (op->op_flags & OPf_SPECIAL) {
1538 cxix = dopoptoloop(cxstack_ix);
1539 if (cxix < 0)
1540 DIE("Can't \"next\" outside a block");
1541 }
1542 else {
1543 cxix = dopoptolabel(cPVOP->op_pv);
1544 if (cxix < 0)
1545 DIE("Label not found for \"next %s\"", cPVOP->op_pv);
1546 }
1547 if (cxix < cxstack_ix)
1548 dounwind(cxix);
1549
1550 TOPBLOCK(cx);
1551 oldsave = scopestack[scopestack_ix - 1];
1552 LEAVE_SCOPE(oldsave);
1553 return cx->blk_loop.next_op;
1554}
1555
1556PP(pp_redo)
1557{
1558 I32 cxix;
1559 register CONTEXT *cx;
1560 I32 oldsave;
1561
1562 if (op->op_flags & OPf_SPECIAL) {
1563 cxix = dopoptoloop(cxstack_ix);
1564 if (cxix < 0)
1565 DIE("Can't \"redo\" outside a block");
1566 }
1567 else {
1568 cxix = dopoptolabel(cPVOP->op_pv);
1569 if (cxix < 0)
1570 DIE("Label not found for \"redo %s\"", cPVOP->op_pv);
1571 }
1572 if (cxix < cxstack_ix)
1573 dounwind(cxix);
1574
1575 TOPBLOCK(cx);
1576 oldsave = scopestack[scopestack_ix - 1];
1577 LEAVE_SCOPE(oldsave);
1578 return cx->blk_loop.redo_op;
1579}
1580
1581static OP* lastgotoprobe;
1582
1583static OP *
1584dofindlabel(op,label,opstack)
1585OP *op;
1586char *label;
1587OP **opstack;
1588{
1589 OP *kid;
1590 OP **ops = opstack;
1591
1592 if (op->op_type == OP_LEAVE ||
1593 op->op_type == OP_SCOPE ||
1594 op->op_type == OP_LEAVELOOP ||
1595 op->op_type == OP_LEAVETRY)
1596 *ops++ = cUNOP->op_first;
1597 *ops = 0;
1598 if (op->op_flags & OPf_KIDS) {
1599 /* First try all the kids at this level, since that's likeliest. */
1600 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling) {
1601 if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
1602 kCOP->cop_label && strEQ(kCOP->cop_label, label))
1603 return kid;
1604 }
1605 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling) {
1606 if (kid == lastgotoprobe)
1607 continue;
1608 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
1609 if (ops > opstack &&
1610 (ops[-1]->op_type == OP_NEXTSTATE ||
1611 ops[-1]->op_type == OP_DBSTATE))
1612 *ops = kid;
1613 else
1614 *ops++ = kid;
1615 }
1616 if (op = dofindlabel(kid,label,ops))
1617 return op;
1618 }
1619 }
1620 *ops = 0;
1621 return 0;
1622}
1623
1624PP(pp_dump)
1625{
1626 return pp_goto(ARGS);
1627 /*NOTREACHED*/
1628}
1629
1630PP(pp_goto)
1631{
1632 dSP;
1633 OP *retop = 0;
1634 I32 ix;
1635 register CONTEXT *cx;
1636 OP *enterops[64];
1637 char *label;
1638 int do_dump = (op->op_type == OP_DUMP);
1639
1640 label = 0;
1641 if (op->op_flags & OPf_STACKED) {
1642 SV *sv = POPs;
1643
1644 /* This egregious kludge implements goto &subroutine */
1645 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
1646 I32 cxix;
1647 register CONTEXT *cx;
1648 CV* cv = (CV*)SvRV(sv);
1649 SV** mark;
1650 I32 items = 0;
1651 I32 oldsave;
1652
4aa0a1f7 1653 if (!CvROOT(cv) && !CvXSUB(cv)) {
1654 if (CvGV(cv)) {
1655 SV *tmpstr = sv_newmortal();
e5cf08de 1656 gv_efullname3(tmpstr, CvGV(cv), Nullch);
4aa0a1f7 1657 DIE("Goto undefined subroutine &%s",SvPVX(tmpstr));
1658 }
1659 DIE("Goto undefined subroutine");
1660 }
1661
a0d0e21e 1662 /* First do some returnish stuff. */
1663 cxix = dopoptosub(cxstack_ix);
1664 if (cxix < 0)
1665 DIE("Can't goto subroutine outside a subroutine");
1666 if (cxix < cxstack_ix)
1667 dounwind(cxix);
1668 TOPBLOCK(cx);
1669 mark = stack_sp;
1670 if (cx->blk_sub.hasargs) { /* put @_ back onto stack */
1671 AV* av = cx->blk_sub.argarray;
1672
1673 items = AvFILL(av) + 1;
1ce6579f 1674 stack_sp++;
1675 EXTEND(stack_sp, items); /* @_ could have been extended. */
1676 Copy(AvARRAY(av), stack_sp, items, SV*);
a0d0e21e 1677 stack_sp += items;
1678 GvAV(defgv) = cx->blk_sub.savearray;
a0d0e21e 1679 AvREAL_off(av);
4633a7c4 1680 av_clear(av);
a0d0e21e 1681 }
1682 if (!(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
1683 SvREFCNT_dec(cx->blk_sub.cv);
1684 oldsave = scopestack[scopestack_ix - 1];
1685 LEAVE_SCOPE(oldsave);
1686
1687 /* Now do some callish stuff. */
1688 SAVETMPS;
1689 if (CvXSUB(cv)) {
1690 if (CvOLDSTYLE(cv)) {
ecfc5424 1691 I32 (*fp3)_((int,int,int));
a0d0e21e 1692 while (sp > mark) {
1693 sp[1] = sp[0];
1694 sp--;
1695 }
ecfc5424 1696 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
1697 items = (*fp3)(CvXSUBANY(cv).any_i32,
1698 mark - stack_base + 1,
1699 items);
a0d0e21e 1700 sp = stack_base + items;
1701 }
1702 else {
1ce6579f 1703 stack_sp--; /* There is no cv arg. */
a0d0e21e 1704 (void)(*CvXSUB(cv))(cv);
1705 }
1706 LEAVE;
1707 return pop_return();
1708 }
1709 else {
1710 AV* padlist = CvPADLIST(cv);
1711 SV** svp = AvARRAY(padlist);
1712 cx->blk_sub.cv = cv;
1713 cx->blk_sub.olddepth = CvDEPTH(cv);
1714 CvDEPTH(cv)++;
1715 if (CvDEPTH(cv) < 2)
1716 (void)SvREFCNT_inc(cv);
1717 else { /* save temporaries on recursion? */
1718 if (CvDEPTH(cv) == 100 && dowarn)
1719 warn("Deep recursion on subroutine \"%s\"",
1720 GvENAME(CvGV(cv)));
1721 if (CvDEPTH(cv) > AvFILL(padlist)) {
1722 AV *newpad = newAV();
4aa0a1f7 1723 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
a0d0e21e 1724 I32 ix = AvFILL((AV*)svp[1]);
1725 svp = AvARRAY(svp[0]);
748a9306 1726 for ( ;ix > 0; ix--) {
a0d0e21e 1727 if (svp[ix] != &sv_undef) {
748a9306 1728 char *name = SvPVX(svp[ix]);
1729 if (SvFLAGS(svp[ix]) & SVf_FAKE) {
1730 /* outer lexical? */
1731 av_store(newpad, ix,
4aa0a1f7 1732 SvREFCNT_inc(oldpad[ix]) );
748a9306 1733 }
1734 else { /* our own lexical */
1735 if (*name == '@')
1736 av_store(newpad, ix, sv = (SV*)newAV());
1737 else if (*name == '%')
1738 av_store(newpad, ix, sv = (SV*)newHV());
1739 else
1740 av_store(newpad, ix, sv = NEWSV(0,0));
1741 SvPADMY_on(sv);
1742 }
a0d0e21e 1743 }
1744 else {
748a9306 1745 av_store(newpad, ix, sv = NEWSV(0,0));
a0d0e21e 1746 SvPADTMP_on(sv);
1747 }
1748 }
1749 if (cx->blk_sub.hasargs) {
1750 AV* av = newAV();
1751 av_extend(av, 0);
1752 av_store(newpad, 0, (SV*)av);
1753 AvFLAGS(av) = AVf_REIFY;
1754 }
1755 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
1756 AvFILL(padlist) = CvDEPTH(cv);
1757 svp = AvARRAY(padlist);
1758 }
1759 }
1760 SAVESPTR(curpad);
1761 curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
1762 if (cx->blk_sub.hasargs) {
1763 AV* av = (AV*)curpad[0];
1764 SV** ary;
1765
1766 cx->blk_sub.savearray = GvAV(defgv);
1767 cx->blk_sub.argarray = av;
1768 GvAV(defgv) = cx->blk_sub.argarray;
1769 ++mark;
1770
1771 if (items >= AvMAX(av) + 1) {
1772 ary = AvALLOC(av);
1773 if (AvARRAY(av) != ary) {
1774 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
1775 SvPVX(av) = (char*)ary;
1776 }
1777 if (items >= AvMAX(av) + 1) {
1778 AvMAX(av) = items - 1;
1779 Renew(ary,items+1,SV*);
1780 AvALLOC(av) = ary;
1781 SvPVX(av) = (char*)ary;
1782 }
1783 }
1784 Copy(mark,AvARRAY(av),items,SV*);
1785 AvFILL(av) = items - 1;
1786
1787 while (items--) {
1788 if (*mark)
1789 SvTEMP_off(*mark);
1790 mark++;
1791 }
1792 }
f967eb5f 1793 if (perldb && curstash != debstash) {
1794 /* &xsub is not copying @_ */
1ce6579f 1795 SV *sv = GvSV(DBsub);
1796 save_item(sv);
e5cf08de 1797 gv_efullname3(sv, CvGV(cv), Nullch);
f967eb5f 1798 /* We do not care about using sv to call CV,
1799 * just for info. */
1ce6579f 1800 }
a0d0e21e 1801 RETURNOP(CvSTART(cv));
1802 }
1803 }
1804 else
1805 label = SvPV(sv,na);
1806 }
1807 else if (op->op_flags & OPf_SPECIAL) {
1808 if (! do_dump)
1809 DIE("goto must have label");
1810 }
1811 else
1812 label = cPVOP->op_pv;
1813
1814 if (label && *label) {
1815 OP *gotoprobe = 0;
1816
1817 /* find label */
1818
1819 lastgotoprobe = 0;
1820 *enterops = 0;
1821 for (ix = cxstack_ix; ix >= 0; ix--) {
1822 cx = &cxstack[ix];
1823 switch (cx->cx_type) {
1824 case CXt_SUB:
1825 gotoprobe = CvROOT(cx->blk_sub.cv);
1826 break;
1827 case CXt_EVAL:
1828 gotoprobe = eval_root; /* XXX not good for nested eval */
1829 break;
1830 case CXt_LOOP:
1831 gotoprobe = cx->blk_oldcop->op_sibling;
1832 break;
1833 case CXt_SUBST:
1834 continue;
1835 case CXt_BLOCK:
1836 if (ix)
1837 gotoprobe = cx->blk_oldcop->op_sibling;
1838 else
1839 gotoprobe = main_root;
1840 break;
1841 default:
1842 if (ix)
1843 DIE("panic: goto");
1844 else
1845 gotoprobe = main_root;
1846 break;
1847 }
1848 retop = dofindlabel(gotoprobe, label, enterops);
1849 if (retop)
1850 break;
1851 lastgotoprobe = gotoprobe;
1852 }
1853 if (!retop)
1854 DIE("Can't find label %s", label);
1855
1856 /* pop unwanted frames */
1857
1858 if (ix < cxstack_ix) {
1859 I32 oldsave;
1860
1861 if (ix < 0)
1862 ix = 0;
1863 dounwind(ix);
1864 TOPBLOCK(cx);
1865 oldsave = scopestack[scopestack_ix];
1866 LEAVE_SCOPE(oldsave);
1867 }
1868
1869 /* push wanted frames */
1870
748a9306 1871 if (*enterops && enterops[1]) {
a0d0e21e 1872 OP *oldop = op;
748a9306 1873 for (ix = 1; enterops[ix]; ix++) {
a0d0e21e 1874 op = enterops[ix];
1875 (*op->op_ppaddr)();
1876 }
1877 op = oldop;
1878 }
1879 }
1880
1881 if (do_dump) {
a5f75d66 1882#ifdef VMS
1883 if (!retop) retop = main_start;
1884#endif
a0d0e21e 1885 restartop = retop;
1886 do_undump = TRUE;
1887
1888 my_unexec();
1889
1890 restartop = 0; /* hmm, must be GNU unexec().. */
1891 do_undump = FALSE;
1892 }
1893
1ce6579f 1894 if (curstack == signalstack) {
748a9306 1895 restartop = retop;
a5f75d66 1896 Siglongjmp(top_env, 3);
748a9306 1897 }
1898
a0d0e21e 1899 RETURNOP(retop);
1900}
1901
1902PP(pp_exit)
1903{
1904 dSP;
1905 I32 anum;
1906
1907 if (MAXARG < 1)
1908 anum = 0;
1909 else
1910 anum = SvIVx(POPs);
1911 my_exit(anum);
1912 PUSHs(&sv_undef);
1913 RETURN;
1914}
1915
1916#ifdef NOTYET
1917PP(pp_nswitch)
1918{
1919 dSP;
1920 double value = SvNVx(GvSV(cCOP->cop_gv));
1921 register I32 match = I_32(value);
1922
1923 if (value < 0.0) {
1924 if (((double)match) > value)
1925 --match; /* was fractional--truncate other way */
1926 }
1927 match -= cCOP->uop.scop.scop_offset;
1928 if (match < 0)
1929 match = 0;
1930 else if (match > cCOP->uop.scop.scop_max)
1931 match = cCOP->uop.scop.scop_max;
1932 op = cCOP->uop.scop.scop_next[match];
1933 RETURNOP(op);
1934}
1935
1936PP(pp_cswitch)
1937{
1938 dSP;
1939 register I32 match;
1940
1941 if (multiline)
1942 op = op->op_next; /* can't assume anything */
1943 else {
1944 match = *(SvPVx(GvSV(cCOP->cop_gv), na)) & 255;
1945 match -= cCOP->uop.scop.scop_offset;
1946 if (match < 0)
1947 match = 0;
1948 else if (match > cCOP->uop.scop.scop_max)
1949 match = cCOP->uop.scop.scop_max;
1950 op = cCOP->uop.scop.scop_next[match];
1951 }
1952 RETURNOP(op);
1953}
1954#endif
1955
1956/* Eval. */
1957
1958static void
1959save_lines(array, sv)
1960AV *array;
1961SV *sv;
1962{
1963 register char *s = SvPVX(sv);
1964 register char *send = SvPVX(sv) + SvCUR(sv);
1965 register char *t;
1966 register I32 line = 1;
1967
1968 while (s && s < send) {
1969 SV *tmpstr = NEWSV(85,0);
1970
1971 sv_upgrade(tmpstr, SVt_PVMG);
1972 t = strchr(s, '\n');
1973 if (t)
1974 t++;
1975 else
1976 t = send;
1977
1978 sv_setpvn(tmpstr, s, t - s);
1979 av_store(array, line++, tmpstr);
1980 s = t;
1981 }
1982}
1983
1984static OP *
1985doeval(gimme)
1986int gimme;
1987{
1988 dSP;
1989 OP *saveop = op;
1990 HV *newstash;
748a9306 1991 AV* comppadlist;
a0d0e21e 1992
1993 in_eval = 1;
1994
1ce6579f 1995 PUSHMARK(SP);
1996
a0d0e21e 1997 /* set up a scratch pad */
1998
1999 SAVEINT(padix);
2000 SAVESPTR(curpad);
2001 SAVESPTR(comppad);
2002 SAVESPTR(comppad_name);
2003 SAVEINT(comppad_name_fill);
2004 SAVEINT(min_intro_pending);
2005 SAVEINT(max_intro_pending);
748a9306 2006
2007 SAVESPTR(compcv);
2008 compcv = (CV*)NEWSV(1104,0);
2009 sv_upgrade((SV *)compcv, SVt_PVCV);
2010
a0d0e21e 2011 comppad = newAV();
2012 comppad_name = newAV();
2013 comppad_name_fill = 0;
2014 min_intro_pending = 0;
2015 av_push(comppad, Nullsv);
2016 curpad = AvARRAY(comppad);
2017 padix = 0;
2018
748a9306 2019 comppadlist = newAV();
2020 AvREAL_off(comppadlist);
8e07c86e 2021 av_store(comppadlist, 0, (SV*)comppad_name);
2022 av_store(comppadlist, 1, (SV*)comppad);
748a9306 2023 CvPADLIST(compcv) = comppadlist;
8e07c86e 2024 SAVEFREESV(compcv);
748a9306 2025
a0d0e21e 2026 /* make sure we compile in the right package */
2027
2028 newstash = curcop->cop_stash;
2029 if (curstash != newstash) {
2030 SAVESPTR(curstash);
2031 curstash = newstash;
2032 }
2033 SAVESPTR(beginav);
2034 beginav = newAV();
2035 SAVEFREESV(beginav);
2036
2037 /* try to compile it */
2038
2039 eval_root = Nullop;
2040 error_count = 0;
2041 curcop = &compiling;
2042 curcop->cop_arybase = 0;
c07a80fd 2043 SvREFCNT_dec(rs);
2044 rs = newSVpv("\n", 1);
1ce6579f 2045 if (saveop->op_flags & OPf_SPECIAL)
2046 in_eval |= 4;
2047 else
2048 sv_setpv(GvSV(errgv),"");
a0d0e21e 2049 if (yyparse() || error_count || !eval_root) {
2050 SV **newsp;
2051 I32 gimme;
2052 CONTEXT *cx;
2053 I32 optype;
2054
2055 op = saveop;
2056 if (eval_root) {
2057 op_free(eval_root);
2058 eval_root = Nullop;
2059 }
1ce6579f 2060 SP = stack_base + POPMARK; /* pop original mark */
a0d0e21e 2061 POPBLOCK(cx,curpm);
2062 POPEVAL(cx);
2063 pop_return();
2064 lex_end();
2065 LEAVE;
2066 if (optype == OP_REQUIRE)
4633a7c4 2067 DIE("%s", SvPVx(GvSV(errgv), na));
c07a80fd 2068 SvREFCNT_dec(rs);
2069 rs = SvREFCNT_inc(nrs);
a0d0e21e 2070 RETPUSHUNDEF;
2071 }
c07a80fd 2072 SvREFCNT_dec(rs);
2073 rs = SvREFCNT_inc(nrs);
a0d0e21e 2074 compiling.cop_line = 0;
a0d0e21e 2075 SAVEFREEOP(eval_root);
2076 if (gimme & G_ARRAY)
2077 list(eval_root);
2078 else
2079 scalar(eval_root);
2080
2081 DEBUG_x(dump_eval());
2082
2083 /* compiled okay, so do it */
2084
1ce6579f 2085 SP = stack_base + POPMARK; /* pop original mark */
a0d0e21e 2086 RETURNOP(eval_start);
2087}
2088
2089PP(pp_require)
2090{
2091 dSP;
2092 register CONTEXT *cx;
2093 SV *sv;
2094 char *name;
2095 char *tmpname;
2096 SV** svp;
2097 I32 gimme = G_SCALAR;
760ac839 2098 PerlIO *tryrsfp = 0;
a0d0e21e 2099
2100 sv = POPs;
4633a7c4 2101 if (SvNIOKp(sv) && !SvPOKp(sv)) {
a5f75d66 2102 if (atof(patchlevel) + 0.00000999 < SvNV(sv))
2103 DIE("Perl %s required--this is only version %s, stopped",
2104 SvPV(sv,na),patchlevel);
a0d0e21e 2105 RETPUSHYES;
2106 }
2107 name = SvPV(sv, na);
2108 if (!*name)
2109 DIE("Null filename used");
4633a7c4 2110 TAINT_PROPER("require");
a0d0e21e 2111 if (op->op_type == OP_REQUIRE &&
2112 (svp = hv_fetch(GvHVn(incgv), name, SvCUR(sv), 0)) &&
2113 *svp != &sv_undef)
2114 RETPUSHYES;
2115
2116 /* prepare to compile file */
2117
2118 tmpname = savepv(name);
2119 if (*tmpname == '/' ||
2120 (*tmpname == '.' &&
2121 (tmpname[1] == '/' ||
748a9306 2122 (tmpname[1] == '.' && tmpname[2] == '/')))
4633a7c4 2123#ifdef DOSISH
2124 || (tmpname[0] && tmpname[1] == ':')
2125#endif
748a9306 2126#ifdef VMS
4633a7c4 2127 || (strchr(tmpname,':') || ((*tmpname == '[' || *tmpname == '<') &&
2128 (tmpname[1] == '-' || tmpname[1] == ']' || tmpname[1] == '>')))
748a9306 2129#endif
2130 )
a0d0e21e 2131 {
760ac839 2132 tryrsfp = PerlIO_open(tmpname,"r");
a0d0e21e 2133 }
2134 else {
2135 AV *ar = GvAVn(incgv);
2136 I32 i;
2137
2138 for (i = 0; i <= AvFILL(ar); i++) {
748a9306 2139#ifdef VMS
2140 if (tounixpath_ts(SvPVx(*av_fetch(ar, i, TRUE), na),buf) == NULL)
4633a7c4 2141 continue;
2142 strcat(buf,name);
748a9306 2143#else
a0d0e21e 2144 (void)sprintf(buf, "%s/%s",
2145 SvPVx(*av_fetch(ar, i, TRUE), na), name);
748a9306 2146#endif
760ac839 2147 tryrsfp = PerlIO_open(buf, "r");
a0d0e21e 2148 if (tryrsfp) {
2149 char *s = buf;
2150
2151 if (*s == '.' && s[1] == '/')
2152 s += 2;
2153 Safefree(tmpname);
2154 tmpname = savepv(s);
2155 break;
2156 }
2157 }
2158 }
2159 SAVESPTR(compiling.cop_filegv);
2160 compiling.cop_filegv = gv_fetchfile(tmpname);
2161 Safefree(tmpname);
2162 tmpname = Nullch;
2163 if (!tryrsfp) {
2164 if (op->op_type == OP_REQUIRE) {
2165 sprintf(tokenbuf,"Can't locate %s in @INC", name);
2166 if (instr(tokenbuf,".h "))
2167 strcat(tokenbuf," (change .h to .ph maybe?)");
2168 if (instr(tokenbuf,".ph "))
2169 strcat(tokenbuf," (did you run h2ph?)");
2170 DIE("%s",tokenbuf);
2171 }
2172
2173 RETPUSHUNDEF;
2174 }
2175
2176 /* Assume success here to prevent recursive requirement. */
2177 (void)hv_store(GvHVn(incgv), name, strlen(name),
2178 newSVsv(GvSV(compiling.cop_filegv)), 0 );
2179
2180 ENTER;
2181 SAVETMPS;
2182 lex_start(sv_2mortal(newSVpv("",0)));
e50aee73 2183 if (rsfp_filters){
2184 save_aptr(&rsfp_filters);
2185 rsfp_filters = NULL;
2186 }
2187
a0d0e21e 2188 rsfp = tryrsfp;
2189 name = savepv(name);
2190 SAVEFREEPV(name);
2191 SAVEI32(hints);
2192 hints = 0;
2193
2194 /* switch to eval mode */
2195
2196 push_return(op->op_next);
2197 PUSHBLOCK(cx, CXt_EVAL, SP);
2198 PUSHEVAL(cx, name, compiling.cop_filegv);
2199
2200 compiling.cop_line = 0;
2201
2202 PUTBACK;
2203 return doeval(G_SCALAR);
2204}
2205
2206PP(pp_dofile)
2207{
2208 return pp_require(ARGS);
2209}
2210
2211PP(pp_entereval)
2212{
2213 dSP;
2214 register CONTEXT *cx;
2215 dPOPss;
2216 I32 gimme = GIMME;
2217 char tmpbuf[32];
2218 STRLEN len;
2219
2220 if (!SvPV(sv,len) || !len)
2221 RETPUSHUNDEF;
748a9306 2222 TAINT_PROPER("eval");
a0d0e21e 2223
2224 ENTER;
a0d0e21e 2225 lex_start(sv);
748a9306 2226 SAVETMPS;
a0d0e21e 2227
2228 /* switch to eval mode */
2229
748a9306 2230 SAVESPTR(compiling.cop_filegv);
a0d0e21e 2231 sprintf(tmpbuf, "_<(eval %d)", ++evalseq);
2232 compiling.cop_filegv = gv_fetchfile(tmpbuf+2);
2233 compiling.cop_line = 1;
2234 SAVEDELETE(defstash, savepv(tmpbuf), strlen(tmpbuf));
2235 SAVEI32(hints);
2236 hints = op->op_targ;
2237
2238 push_return(op->op_next);
2239 PUSHBLOCK(cx, CXt_EVAL, SP);
2240 PUSHEVAL(cx, 0, compiling.cop_filegv);
2241
2242 /* prepare to compile string */
2243
2244 if (perldb && curstash != debstash)
2245 save_lines(GvAV(compiling.cop_filegv), linestr);
2246 PUTBACK;
2247 return doeval(gimme);
2248}
2249
2250PP(pp_leaveeval)
2251{
2252 dSP;
2253 register SV **mark;
2254 SV **newsp;
2255 PMOP *newpm;
2256 I32 gimme;
2257 register CONTEXT *cx;
2258 OP *retop;
760ac839 2259 U8 save_flags = op -> op_flags;
a0d0e21e 2260 I32 optype;
2261
2262 POPBLOCK(cx,newpm);
2263 POPEVAL(cx);
2264 retop = pop_return();
2265
2266 if (gimme == G_SCALAR) {
2267 if (op->op_private & OPpLEAVE_VOID)
2268 MARK = newsp;
2269 else {
2270 MARK = newsp + 1;
2271 if (MARK <= SP) {
2272 if (SvFLAGS(TOPs) & SVs_TEMP)
2273 *MARK = TOPs;
2274 else
2275 *MARK = sv_mortalcopy(TOPs);
2276 }
2277 else {
2278 MEXTEND(mark,0);
2279 *MARK = &sv_undef;
2280 }
2281 }
2282 SP = MARK;
2283 }
2284 else {
2285 for (mark = newsp + 1; mark <= SP; mark++)
760ac839 2286 if (!(SvFLAGS(*mark) & SVs_TEMP))
a0d0e21e 2287 *mark = sv_mortalcopy(*mark);
2288 /* in case LEAVE wipes old return values */
2289 }
2290 curpm = newpm; /* Don't pop $1 et al till now */
2291
1ce6579f 2292 if (optype == OP_REQUIRE &&
2293 !(gimme == G_SCALAR ? SvTRUE(*sp) : sp > newsp)) {
a0d0e21e 2294 char *name = cx->blk_eval.old_name;
2295
1ce6579f 2296 /* Unassume the success we assumed earlier. */
2297 (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
2298 retop = die("%s did not return a true value", name);
a0d0e21e 2299 }
2300
2301 lex_end();
2302 LEAVE;
760ac839 2303 if (!(save_flags & OPf_SPECIAL))
1ce6579f 2304 sv_setpv(GvSV(errgv),"");
a0d0e21e 2305
2306 RETURNOP(retop);
2307}
2308
a0d0e21e 2309PP(pp_entertry)
2310{
2311 dSP;
2312 register CONTEXT *cx;
2313 I32 gimme = GIMME;
2314
2315 ENTER;
2316 SAVETMPS;
2317
2318 push_return(cLOGOP->op_other->op_next);
2319 PUSHBLOCK(cx, CXt_EVAL, SP);
2320 PUSHEVAL(cx, 0, 0);
2321 eval_root = op; /* Only needed so that goto works right. */
2322
2323 in_eval = 1;
4633a7c4 2324 sv_setpv(GvSV(errgv),"");
a0d0e21e 2325 RETURN;
2326}
2327
2328PP(pp_leavetry)
2329{
2330 dSP;
2331 register SV **mark;
2332 SV **newsp;
2333 PMOP *newpm;
2334 I32 gimme;
2335 register CONTEXT *cx;
2336 I32 optype;
2337
2338 POPBLOCK(cx,newpm);
2339 POPEVAL(cx);
2340 pop_return();
2341
2342 if (gimme == G_SCALAR) {
2343 if (op->op_private & OPpLEAVE_VOID)
2344 MARK = newsp;
2345 else {
2346 MARK = newsp + 1;
2347 if (MARK <= SP) {
2348 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
2349 *MARK = TOPs;
2350 else
2351 *MARK = sv_mortalcopy(TOPs);
2352 }
2353 else {
2354 MEXTEND(mark,0);
2355 *MARK = &sv_undef;
2356 }
2357 }
2358 SP = MARK;
2359 }
2360 else {
2361 for (mark = newsp + 1; mark <= SP; mark++)
760ac839 2362 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP)))
a0d0e21e 2363 *mark = sv_mortalcopy(*mark);
2364 /* in case LEAVE wipes old return values */
2365 }
2366 curpm = newpm; /* Don't pop $1 et al till now */
2367
2368 LEAVE;
4633a7c4 2369 sv_setpv(GvSV(errgv),"");
a0d0e21e 2370 RETURN;
2371}
2372
2373static void
2374doparseform(sv)
2375SV *sv;
2376{
2377 STRLEN len;
2378 register char *s = SvPV_force(sv, len);
2379 register char *send = s + len;
2380 register char *base;
2381 register I32 skipspaces = 0;
2382 bool noblank;
2383 bool repeat;
2384 bool postspace = FALSE;
2385 U16 *fops;
2386 register U16 *fpc;
2387 U16 *linepc;
2388 register I32 arg;
2389 bool ischop;
2390
2391 New(804, fops, (send - s)*3+2, U16); /* Almost certainly too long... */
2392 fpc = fops;
2393
2394 if (s < send) {
2395 linepc = fpc;
2396 *fpc++ = FF_LINEMARK;
2397 noblank = repeat = FALSE;
2398 base = s;
2399 }
2400
2401 while (s <= send) {
2402 switch (*s++) {
2403 default:
2404 skipspaces = 0;
2405 continue;
2406
2407 case '~':
2408 if (*s == '~') {
2409 repeat = TRUE;
2410 *s = ' ';
2411 }
2412 noblank = TRUE;
2413 s[-1] = ' ';
2414 /* FALL THROUGH */
2415 case ' ': case '\t':
2416 skipspaces++;
2417 continue;
2418
2419 case '\n': case 0:
2420 arg = s - base;
2421 skipspaces++;
2422 arg -= skipspaces;
2423 if (arg) {
2424 if (postspace) {
2425 *fpc++ = FF_SPACE;
2426 postspace = FALSE;
2427 }
2428 *fpc++ = FF_LITERAL;
2429 *fpc++ = arg;
2430 }
2431 if (s <= send)
2432 skipspaces--;
2433 if (skipspaces) {
2434 *fpc++ = FF_SKIP;
2435 *fpc++ = skipspaces;
2436 }
2437 skipspaces = 0;
2438 if (s <= send)
2439 *fpc++ = FF_NEWLINE;
2440 if (noblank) {
2441 *fpc++ = FF_BLANK;
2442 if (repeat)
2443 arg = fpc - linepc + 1;
2444 else
2445 arg = 0;
2446 *fpc++ = arg;
2447 }
2448 if (s < send) {
2449 linepc = fpc;
2450 *fpc++ = FF_LINEMARK;
2451 noblank = repeat = FALSE;
2452 base = s;
2453 }
2454 else
2455 s++;
2456 continue;
2457
2458 case '@':
2459 case '^':
2460 ischop = s[-1] == '^';
2461
2462 if (postspace) {
2463 *fpc++ = FF_SPACE;
2464 postspace = FALSE;
2465 }
2466 arg = (s - base) - 1;
2467 if (arg) {
2468 *fpc++ = FF_LITERAL;
2469 *fpc++ = arg;
2470 }
2471
2472 base = s - 1;
2473 *fpc++ = FF_FETCH;
2474 if (*s == '*') {
2475 s++;
2476 *fpc++ = 0;
2477 *fpc++ = FF_LINEGLOB;
2478 }
2479 else if (*s == '#' || (*s == '.' && s[1] == '#')) {
2480 arg = ischop ? 512 : 0;
2481 base = s - 1;
2482 while (*s == '#')
2483 s++;
2484 if (*s == '.') {
2485 char *f;
2486 s++;
2487 f = s;
2488 while (*s == '#')
2489 s++;
2490 arg |= 256 + (s - f);
2491 }
2492 *fpc++ = s - base; /* fieldsize for FETCH */
2493 *fpc++ = FF_DECIMAL;
2494 *fpc++ = arg;
2495 }
2496 else {
2497 I32 prespace = 0;
2498 bool ismore = FALSE;
2499
2500 if (*s == '>') {
2501 while (*++s == '>') ;
2502 prespace = FF_SPACE;
2503 }
2504 else if (*s == '|') {
2505 while (*++s == '|') ;
2506 prespace = FF_HALFSPACE;
2507 postspace = TRUE;
2508 }
2509 else {
2510 if (*s == '<')
2511 while (*++s == '<') ;
2512 postspace = TRUE;
2513 }
2514 if (*s == '.' && s[1] == '.' && s[2] == '.') {
2515 s += 3;
2516 ismore = TRUE;
2517 }
2518 *fpc++ = s - base; /* fieldsize for FETCH */
2519
2520 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
2521
2522 if (prespace)
2523 *fpc++ = prespace;
2524 *fpc++ = FF_ITEM;
2525 if (ismore)
2526 *fpc++ = FF_MORE;
2527 if (ischop)
2528 *fpc++ = FF_CHOP;
2529 }
2530 base = s;
2531 skipspaces = 0;
2532 continue;
2533 }
2534 }
2535 *fpc++ = FF_END;
2536
2537 arg = fpc - fops;
2538 { /* need to jump to the next word */
2539 int z;
2540 z = WORD_ALIGN - SvCUR(sv) % WORD_ALIGN;
2541 SvGROW(sv, SvCUR(sv) + z + arg * sizeof(U16) + 4);
2542 s = SvPVX(sv) + SvCUR(sv) + z;
2543 }
2544 Copy(fops, s, arg, U16);
2545 Safefree(fops);
2546 SvCOMPILED_on(sv);
2547}