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