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