This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Remove bad advice from perllocale.pod
[perl5.git] / pp_ctl.c
CommitLineData
a0d0e21e
LW
1/* pp_ctl.c
2 *
3 * Copyright (c) 1991-1994, Larry Wall
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
8 */
9
10/*
11 * Now far ahead the Road has gone,
12 * And I must follow, if I can,
13 * Pursuing it with eager feet,
14 * Until it joins some larger way
15 * Where many paths and errands meet.
16 * And whither then? I cannot say.
17 */
18
19#include "EXTERN.h"
20#include "perl.h"
21
22#ifndef WORD_ALIGN
23#define WORD_ALIGN sizeof(U16)
24#endif
25
26static OP *doeval _((int gimme));
27static OP *dofindlabel _((OP *op, char *label, OP **opstack));
28static void doparseform _((SV *sv));
29static I32 dopoptoeval _((I32 startingblock));
30static I32 dopoptolabel _((char *label));
31static I32 dopoptoloop _((I32 startingblock));
32static I32 dopoptosub _((I32 startingblock));
33static void save_lines _((AV *array, SV *sv));
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
LW
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
LW
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
LW
80 pm->op_pmflags = pm->op_pmpermflags; /* reset case sensitivity */
81 pm->op_pmregexp = pregcomp(t, t + len, pm);
82 }
a0d0e21e
LW
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
LW
90 pm->op_pmflags &= ~PMf_RUNTIME; /* no point compiling again */
91 hoistmust(pm);
92 cLOGOP->op_first->op_next = op->op_next;
a0d0e21e
LW
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
LW
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
LW
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
LW
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
LW
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
LW
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
LW
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
LW
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
LW
179 double value;
180 bool gotsome;
181 STRLEN len;
182
55497cff 183 if (!SvMAGICAL(form) || !SvCOMPILED(form)) {
a0d0e21e
LW
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
LW
224 } )
225 switch (*fpc++) {
226 case FF_LINEMARK:
227 linemark = t;
a0d0e21e
LW
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
LW
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
LW
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. */
9f8d30d5
CS
615 SvTEMP_off(*up);
616 if (!sortcop && !SvPOK(*up))
a0d0e21e 617 (void)sv_2pv(*up, &na);
a0d0e21e
LW
618 up++;
619 }
620 }
621 max = --up - myorigmark;
622 if (sortcop) {
623 if (max > 1) {
624 AV *oldstack;
625 CONTEXT *cx;
626 SV** newsp;
627
628 SAVETMPS;
629 SAVESPTR(op);
630
1ce6579f 631 oldstack = curstack;
a0d0e21e
LW
632 if (!sortstack) {
633 sortstack = newAV();
634 AvREAL_off(sortstack);
635 av_extend(sortstack, 32);
636 }
1ce6579f 637 SWITCHSTACK(curstack, sortstack);
a0d0e21e
LW
638 if (sortstash != stash) {
639 firstgv = gv_fetchpv("a", TRUE, SVt_PV);
640 secondgv = gv_fetchpv("b", TRUE, SVt_PV);
641 sortstash = stash;
642 }
643
644 SAVESPTR(GvSV(firstgv));
645 SAVESPTR(GvSV(secondgv));
646 PUSHBLOCK(cx, CXt_LOOP, stack_base);
647 sortcxix = cxstack_ix;
648
649 qsort((char*)(myorigmark+1), max, sizeof(SV*), sortcv);
650
651 POPBLOCK(cx,curpm);
652 SWITCHSTACK(sortstack, oldstack);
653 }
654 LEAVE;
655 }
656 else {
657 if (max > 1) {
658 MEXTEND(SP, 20); /* Can't afford stack realloc on signal. */
bbce6d69 659 qsort((char*)(ORIGMARK+1), max, sizeof(SV*),
660 (op->op_private & OPpLOCALE) ? sortcmp_locale : sortcmp);
a0d0e21e
LW
661 }
662 }
663 stack_sp = ORIGMARK + max;
664 return nextop;
665}
666
667/* Range stuff. */
668
669PP(pp_range)
670{
671 if (GIMME == G_ARRAY)
672 return cCONDOP->op_true;
673 return SvTRUEx(PAD_SV(op->op_targ)) ? cCONDOP->op_false : cCONDOP->op_true;
674}
675
676PP(pp_flip)
677{
678 dSP;
679
680 if (GIMME == G_ARRAY) {
681 RETURNOP(((CONDOP*)cUNOP->op_first)->op_false);
682 }
683 else {
684 dTOPss;
685 SV *targ = PAD_SV(op->op_targ);
686
687 if ((op->op_private & OPpFLIP_LINENUM)
688 ? last_in_gv && SvIV(sv) == IoLINES(GvIOp(last_in_gv))
689 : SvTRUE(sv) ) {
690 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
691 if (op->op_flags & OPf_SPECIAL) {
692 sv_setiv(targ, 1);
693 RETURN;
694 }
695 else {
696 sv_setiv(targ, 0);
697 sp--;
698 RETURNOP(((CONDOP*)cUNOP->op_first)->op_false);
699 }
700 }
701 sv_setpv(TARG, "");
702 SETs(targ);
703 RETURN;
704 }
705}
706
707PP(pp_flop)
708{
709 dSP;
710
711 if (GIMME == G_ARRAY) {
712 dPOPPOPssrl;
713 register I32 i;
714 register SV *sv;
715 I32 max;
716
4633a7c4 717 if (SvNIOKp(left) || !SvPOKp(left) ||
bbce6d69 718 (looks_like_number(left) && *SvPVX(left) != '0') )
719 {
a0d0e21e
LW
720 i = SvIV(left);
721 max = SvIV(right);
bbce6d69 722 if (max >= i) {
723 EXTEND_MORTAL(max - i + 1);
a0d0e21e 724 EXTEND(SP, max - i + 1);
bbce6d69 725 }
a0d0e21e 726 while (i <= max) {
bbce6d69 727 sv = sv_2mortal(newSViv(i++));
a0d0e21e
LW
728 PUSHs(sv);
729 }
730 }
731 else {
732 SV *final = sv_mortalcopy(right);
733 STRLEN len;
734 char *tmps = SvPV(final, len);
735
736 sv = sv_mortalcopy(left);
4633a7c4 737 while (!SvNIOKp(sv) && SvCUR(sv) <= len &&
a0d0e21e
LW
738 strNE(SvPVX(sv),tmps) ) {
739 XPUSHs(sv);
740 sv = sv_2mortal(newSVsv(sv));
741 sv_inc(sv);
742 }
743 if (strEQ(SvPVX(sv),tmps))
744 XPUSHs(sv);
745 }
746 }
747 else {
748 dTOPss;
749 SV *targ = PAD_SV(cUNOP->op_first->op_targ);
750 sv_inc(targ);
751 if ((op->op_private & OPpFLIP_LINENUM)
752 ? last_in_gv && SvIV(sv) == IoLINES(GvIOp(last_in_gv))
753 : SvTRUE(sv) ) {
754 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
755 sv_catpv(targ, "E0");
756 }
757 SETs(targ);
758 }
759
760 RETURN;
761}
762
763/* Control. */
764
765static I32
766dopoptolabel(label)
767char *label;
768{
769 register I32 i;
770 register CONTEXT *cx;
771
772 for (i = cxstack_ix; i >= 0; i--) {
773 cx = &cxstack[i];
774 switch (cx->cx_type) {
775 case CXt_SUBST:
776 if (dowarn)
777 warn("Exiting substitution via %s", op_name[op->op_type]);
778 break;
779 case CXt_SUB:
780 if (dowarn)
781 warn("Exiting subroutine via %s", op_name[op->op_type]);
782 break;
783 case CXt_EVAL:
784 if (dowarn)
785 warn("Exiting eval via %s", op_name[op->op_type]);
786 break;
787 case CXt_LOOP:
788 if (!cx->blk_loop.label ||
789 strNE(label, cx->blk_loop.label) ) {
790 DEBUG_l(deb("(Skipping label #%d %s)\n",
791 i, cx->blk_loop.label));
792 continue;
793 }
794 DEBUG_l( deb("(Found label #%d %s)\n", i, label));
795 return i;
796 }
797 }
798 return i;
799}
800
e50aee73
AD
801I32
802dowantarray()
803{
804 I32 cxix;
805
806 cxix = dopoptosub(cxstack_ix);
807 if (cxix < 0)
808 return G_SCALAR;
809
810 if (cxstack[cxix].blk_gimme == G_ARRAY)
811 return G_ARRAY;
812 else
813 return G_SCALAR;
814}
815
a0d0e21e
LW
816static I32
817dopoptosub(startingblock)
818I32 startingblock;
819{
820 I32 i;
821 register CONTEXT *cx;
822 for (i = startingblock; i >= 0; i--) {
823 cx = &cxstack[i];
824 switch (cx->cx_type) {
825 default:
826 continue;
827 case CXt_EVAL:
828 case CXt_SUB:
829 DEBUG_l( deb("(Found sub #%d)\n", i));
830 return i;
831 }
832 }
833 return i;
834}
835
836static I32
837dopoptoeval(startingblock)
838I32 startingblock;
839{
840 I32 i;
841 register CONTEXT *cx;
842 for (i = startingblock; i >= 0; i--) {
843 cx = &cxstack[i];
844 switch (cx->cx_type) {
845 default:
846 continue;
847 case CXt_EVAL:
848 DEBUG_l( deb("(Found eval #%d)\n", i));
849 return i;
850 }
851 }
852 return i;
853}
854
855static I32
856dopoptoloop(startingblock)
857I32 startingblock;
858{
859 I32 i;
860 register CONTEXT *cx;
861 for (i = startingblock; i >= 0; i--) {
862 cx = &cxstack[i];
863 switch (cx->cx_type) {
864 case CXt_SUBST:
865 if (dowarn)
5f05dabc 866 warn("Exiting substitution via %s", op_name[op->op_type]);
a0d0e21e
LW
867 break;
868 case CXt_SUB:
869 if (dowarn)
870 warn("Exiting subroutine via %s", op_name[op->op_type]);
871 break;
872 case CXt_EVAL:
873 if (dowarn)
874 warn("Exiting eval via %s", op_name[op->op_type]);
875 break;
876 case CXt_LOOP:
877 DEBUG_l( deb("(Found loop #%d)\n", i));
878 return i;
879 }
880 }
881 return i;
882}
883
884void
885dounwind(cxix)
886I32 cxix;
887{
888 register CONTEXT *cx;
889 SV **newsp;
890 I32 optype;
891
892 while (cxstack_ix > cxix) {
893 cx = &cxstack[cxstack_ix--];
760ac839 894 DEBUG_l(PerlIO_printf(Perl_debug_log, "Unwinding block %ld, type %s\n", (long) cxstack_ix+1,
a0d0e21e
LW
895 block_type[cx->cx_type]));
896 /* Note: we don't need to restore the base context info till the end. */
897 switch (cx->cx_type) {
898 case CXt_SUB:
899 POPSUB(cx);
900 break;
901 case CXt_EVAL:
902 POPEVAL(cx);
903 break;
904 case CXt_LOOP:
905 POPLOOP(cx);
906 break;
907 case CXt_SUBST:
908 break;
909 }
910 }
911}
912
a0d0e21e
LW
913OP *
914die_where(message)
915char *message;
916{
917 if (in_eval) {
918 I32 cxix;
919 register CONTEXT *cx;
920 I32 gimme;
921 SV **newsp;
922
4633a7c4
LW
923 if (in_eval & 4) {
924 SV **svp;
925 STRLEN klen = strlen(message);
926
927 svp = hv_fetch(GvHV(errgv), message, klen, TRUE);
928 if (svp) {
929 if (!SvIOK(*svp)) {
930 static char prefix[] = "\t(in cleanup) ";
931 sv_upgrade(*svp, SVt_IV);
932 (void)SvIOK_only(*svp);
933 SvGROW(GvSV(errgv), SvCUR(GvSV(errgv))+sizeof(prefix)+klen);
934 sv_catpvn(GvSV(errgv), prefix, sizeof(prefix)-1);
935 sv_catpvn(GvSV(errgv), message, klen);
936 }
937 sv_inc(*svp);
938 }
939 }
940 else
c07a80fd 941 sv_setpv(GvSV(errgv), message);
4633a7c4 942
a0d0e21e
LW
943 cxix = dopoptoeval(cxstack_ix);
944 if (cxix >= 0) {
945 I32 optype;
946
947 if (cxix < cxstack_ix)
948 dounwind(cxix);
949
950 POPBLOCK(cx,curpm);
951 if (cx->cx_type != CXt_EVAL) {
760ac839 952 PerlIO_printf(PerlIO_stderr(), "panic: die %s", message);
a0d0e21e
LW
953 my_exit(1);
954 }
955 POPEVAL(cx);
956
957 if (gimme == G_SCALAR)
958 *++newsp = &sv_undef;
959 stack_sp = newsp;
960
961 LEAVE;
748a9306 962
a0d0e21e 963 if (optype == OP_REQUIRE)
4633a7c4 964 DIE("%s", SvPVx(GvSV(errgv), na));
a0d0e21e
LW
965 return pop_return();
966 }
967 }
760ac839
LW
968 PerlIO_printf(PerlIO_stderr(), "%s",message);
969 PerlIO_flush(PerlIO_stderr());
a6c477ed 970 if (e_tmpname) {
971 if (e_fp) {
760ac839 972 PerlIO_close(e_fp);
a6c477ed 973 e_fp = Nullfp;
974 }
a0d0e21e 975 (void)UNLINK(e_tmpname);
a6c477ed 976 Safefree(e_tmpname);
977 e_tmpname = Nullch;
e632a5f6 978 }
748a9306
LW
979 statusvalue = SHIFTSTATUS(statusvalue);
980#ifdef VMS
981 my_exit((U32)vaxc$errno?vaxc$errno:errno?errno:statusvalue?statusvalue:SS$_ABORT);
982#else
a0d0e21e 983 my_exit((I32)((errno&255)?errno:((statusvalue&255)?statusvalue:255)));
748a9306 984#endif
a0d0e21e
LW
985 return 0;
986}
987
988PP(pp_xor)
989{
990 dSP; dPOPTOPssrl;
991 if (SvTRUE(left) != SvTRUE(right))
992 RETSETYES;
993 else
994 RETSETNO;
995}
996
997PP(pp_andassign)
998{
999 dSP;
1000 if (!SvTRUE(TOPs))
1001 RETURN;
1002 else
1003 RETURNOP(cLOGOP->op_other);
1004}
1005
1006PP(pp_orassign)
1007{
1008 dSP;
1009 if (SvTRUE(TOPs))
1010 RETURN;
1011 else
1012 RETURNOP(cLOGOP->op_other);
1013}
1014
1015#ifdef DEPRECATED
1016PP(pp_entersubr)
1017{
1018 dSP;
1019 SV** mark = (stack_base + *markstack_ptr + 1);
1020 SV* cv = *mark;
1021 while (mark < sp) { /* emulate old interface */
1022 *mark = mark[1];
1023 mark++;
1024 }
1025 *sp = cv;
1026 return pp_entersub();
1027}
1028#endif
1029
1030PP(pp_caller)
1031{
1032 dSP;
1033 register I32 cxix = dopoptosub(cxstack_ix);
1034 register CONTEXT *cx;
1035 I32 dbcxix;
1036 SV *sv;
1037 I32 count = 0;
1038
1039 if (MAXARG)
1040 count = POPi;
1041 EXTEND(SP, 6);
1042 for (;;) {
1043 if (cxix < 0) {
1044 if (GIMME != G_ARRAY)
1045 RETPUSHUNDEF;
1046 RETURN;
1047 }
1048 if (DBsub && cxix >= 0 &&
1049 cxstack[cxix].blk_sub.cv == GvCV(DBsub))
1050 count++;
1051 if (!count--)
1052 break;
1053 cxix = dopoptosub(cxix - 1);
1054 }
1055 cx = &cxstack[cxix];
06a5b730 1056 if (cxstack[cxix].cx_type == CXt_SUB) {
1057 dbcxix = dopoptosub(cxix - 1);
1058 /* We expect that cxstack[dbcxix] is CXt_SUB, anyway, the
1059 field below is defined for any cx. */
1060 if (DBsub && dbcxix >= 0 && cxstack[dbcxix].blk_sub.cv == GvCV(DBsub))
1061 cx = &cxstack[dbcxix];
1062 }
1063
a0d0e21e
LW
1064 if (GIMME != G_ARRAY) {
1065 dTARGET;
1066
1067 sv_setpv(TARG, HvNAME(cx->blk_oldcop->cop_stash));
1068 PUSHs(TARG);
1069 RETURN;
1070 }
a0d0e21e
LW
1071
1072 PUSHs(sv_2mortal(newSVpv(HvNAME(cx->blk_oldcop->cop_stash), 0)));
1073 PUSHs(sv_2mortal(newSVpv(SvPVX(GvSV(cx->blk_oldcop->cop_filegv)), 0)));
1074 PUSHs(sv_2mortal(newSViv((I32)cx->blk_oldcop->cop_line)));
1075 if (!MAXARG)
1076 RETURN;
06a5b730 1077 if (cx->cx_type == CXt_SUB) { /* So is cxstack[dbcxix]. */
a0d0e21e 1078 sv = NEWSV(49, 0);
e5cf08de 1079 gv_efullname3(sv, CvGV(cxstack[cxix].blk_sub.cv), Nullch);
a0d0e21e
LW
1080 PUSHs(sv_2mortal(sv));
1081 PUSHs(sv_2mortal(newSViv((I32)cx->blk_sub.hasargs)));
1082 }
1083 else {
1084 PUSHs(sv_2mortal(newSVpv("(eval)",0)));
1085 PUSHs(sv_2mortal(newSViv(0)));
1086 }
1087 PUSHs(sv_2mortal(newSViv((I32)cx->blk_gimme)));
4633a7c4 1088 if (cx->cx_type == CXt_EVAL) {
06a5b730 1089 if (cx->blk_eval.old_op_type == OP_ENTEREVAL) {
4633a7c4 1090 PUSHs(cx->blk_eval.cur_text);
06a5b730 1091 PUSHs(&sv_no);
1092 }
1093 else if (cx->blk_eval.old_name) { /* Try blocks have old_name == 0. */
1094 /* Require, put the name. */
1095 PUSHs(sv_2mortal(newSVpv(cx->blk_eval.old_name, 0)));
1096 PUSHs(&sv_yes);
1097 }
4633a7c4
LW
1098 }
1099 else if (cx->cx_type == CXt_SUB &&
1100 cx->blk_sub.hasargs &&
1101 curcop->cop_stash == debstash)
1102 {
a0d0e21e
LW
1103 AV *ary = cx->blk_sub.argarray;
1104 int off = AvARRAY(ary) - AvALLOC(ary);
1105
1106 if (!dbargs) {
1107 GV* tmpgv;
1108 dbargs = GvAV(gv_AVadd(tmpgv = gv_fetchpv("DB::args", TRUE,
1109 SVt_PVAV)));
a5f75d66 1110 GvMULTI_on(tmpgv);
a0d0e21e
LW
1111 AvREAL_off(dbargs); /* XXX Should be REIFY */
1112 }
1113
1114 if (AvMAX(dbargs) < AvFILL(ary) + off)
1115 av_extend(dbargs, AvFILL(ary) + off);
1116 Copy(AvALLOC(ary), AvARRAY(dbargs), AvFILL(ary) + 1 + off, SV*);
1117 AvFILL(dbargs) = AvFILL(ary) + off;
1118 }
1119 RETURN;
1120}
1121
1122static int
1123sortcv(a, b)
1124const void *a;
1125const void *b;
1126{
1127 SV **str1 = (SV **) a;
1128 SV **str2 = (SV **) b;
748a9306 1129 I32 oldsaveix = savestack_ix;
a0d0e21e
LW
1130 I32 oldscopeix = scopestack_ix;
1131 I32 result;
1132 GvSV(firstgv) = *str1;
1133 GvSV(secondgv) = *str2;
1134 stack_sp = stack_base;
1135 op = sortcop;
a6c477ed 1136 runops();
a0d0e21e
LW
1137 if (stack_sp != stack_base + 1)
1138 croak("Sort subroutine didn't return single value");
748a9306 1139 if (!SvNIOKp(*stack_sp))
a0d0e21e
LW
1140 croak("Sort subroutine didn't return a numeric value");
1141 result = SvIV(*stack_sp);
1142 while (scopestack_ix > oldscopeix) {
1143 LEAVE;
1144 }
748a9306 1145 leave_scope(oldsaveix);
a0d0e21e
LW
1146 return result;
1147}
1148
1149static int
1150sortcmp(a, b)
1151const void *a;
1152const void *b;
1153{
bbce6d69 1154 return sv_cmp(*(SV **)a, *(SV **)b);
1155}
e5cf08de 1156
bbce6d69 1157static int
1158sortcmp_locale(a, b)
1159const void *a;
1160const void *b;
1161{
1162 return sv_cmp_locale(*(SV **)a, *(SV **)b);
a0d0e21e
LW
1163}
1164
1165PP(pp_reset)
1166{
1167 dSP;
1168 char *tmps;
1169
1170 if (MAXARG < 1)
1171 tmps = "";
1172 else
1173 tmps = POPp;
1174 sv_reset(tmps, curcop->cop_stash);
1175 PUSHs(&sv_yes);
1176 RETURN;
1177}
1178
1179PP(pp_lineseq)
1180{
1181 return NORMAL;
1182}
1183
1184PP(pp_dbstate)
1185{
1186 curcop = (COP*)op;
1187 TAINT_NOT; /* Each statement is presumed innocent */
1188 stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
1189 FREETMPS;
1190
1191 if (op->op_private || SvIV(DBsingle) || SvIV(DBsignal) || SvIV(DBtrace))
1192 {
1193 SV **sp;
1194 register CV *cv;
1195 register CONTEXT *cx;
748a9306 1196 I32 gimme = G_ARRAY;
a0d0e21e
LW
1197 I32 hasargs;
1198 GV *gv;
1199
a0d0e21e
LW
1200 gv = DBgv;
1201 cv = GvCV(gv);
a0d0e21e
LW
1202 if (!cv)
1203 DIE("No DB::DB routine defined");
1204
06a5b730 1205 if (CvDEPTH(cv) >= 1 && !(debug & (1<<30))) /* don't do recursive DB::DB call */
a0d0e21e 1206 return NORMAL;
748a9306 1207
4633a7c4
LW
1208 ENTER;
1209 SAVETMPS;
1210
748a9306 1211 SAVEI32(debug);
55497cff 1212 SAVESTACK_POS();
748a9306
LW
1213 debug = 0;
1214 hasargs = 0;
1215 sp = stack_sp;
1216
a0d0e21e 1217 push_return(op->op_next);
748a9306 1218 PUSHBLOCK(cx, CXt_SUB, sp);
a0d0e21e
LW
1219 PUSHSUB(cx);
1220 CvDEPTH(cv)++;
1221 (void)SvREFCNT_inc(cv);
1222 SAVESPTR(curpad);
1223 curpad = AvARRAY((AV*)*av_fetch(CvPADLIST(cv),1,FALSE));
1224 RETURNOP(CvSTART(cv));
1225 }
1226 else
1227 return NORMAL;
1228}
1229
1230PP(pp_scope)
1231{
1232 return NORMAL;
1233}
1234
1235PP(pp_enteriter)
1236{
1237 dSP; dMARK;
1238 register CONTEXT *cx;
1239 I32 gimme = GIMME;
1240 SV **svp;
1241
4633a7c4
LW
1242 ENTER;
1243 SAVETMPS;
1244
a0d0e21e
LW
1245 if (op->op_targ)
1246 svp = &curpad[op->op_targ]; /* "my" variable */
1247 else
1248 svp = &GvSV((GV*)POPs); /* symbol table variable */
1249
4633a7c4
LW
1250 SAVESPTR(*svp);
1251
a0d0e21e
LW
1252 ENTER;
1253
1254 PUSHBLOCK(cx, CXt_LOOP, SP);
1255 PUSHLOOP(cx, svp, MARK);
44a8e56a 1256 if (op->op_flags & OPf_STACKED)
1257 cx->blk_loop.iterary = (AV*)SvREFCNT_inc(POPs);
4633a7c4 1258 else {
1ce6579f 1259 cx->blk_loop.iterary = curstack;
1260 AvFILL(curstack) = sp - stack_base;
4633a7c4
LW
1261 cx->blk_loop.iterix = MARK - stack_base;
1262 }
a0d0e21e
LW
1263
1264 RETURN;
1265}
1266
1267PP(pp_enterloop)
1268{
1269 dSP;
1270 register CONTEXT *cx;
1271 I32 gimme = GIMME;
1272
1273 ENTER;
1274 SAVETMPS;
1275 ENTER;
1276
1277 PUSHBLOCK(cx, CXt_LOOP, SP);
1278 PUSHLOOP(cx, 0, SP);
1279
1280 RETURN;
1281}
1282
1283PP(pp_leaveloop)
1284{
1285 dSP;
1286 register CONTEXT *cx;
1287 I32 gimme;
1288 SV **newsp;
1289 PMOP *newpm;
1290 SV **mark;
1291
1292 POPBLOCK(cx,newpm);
1293 mark = newsp;
1294 POPLOOP(cx);
1295 if (gimme == G_SCALAR) {
1296 if (op->op_private & OPpLEAVE_VOID)
1297 ;
1298 else {
1299 if (mark < SP)
1300 *++newsp = sv_mortalcopy(*SP);
1301 else
1302 *++newsp = &sv_undef;
1303 }
1304 }
1305 else {
1306 while (mark < SP)
1307 *++newsp = sv_mortalcopy(*++mark);
1308 }
1309 curpm = newpm; /* Don't pop $1 et al till now */
1310 sp = newsp;
1311 LEAVE;
1312 LEAVE;
1313
1314 RETURN;
1315}
1316
1317PP(pp_return)
1318{
1319 dSP; dMARK;
1320 I32 cxix;
1321 register CONTEXT *cx;
1322 I32 gimme;
1323 SV **newsp;
1324 PMOP *newpm;
1325 I32 optype = 0;
1326
1ce6579f 1327 if (curstack == sortstack) {
a0d0e21e 1328 if (cxstack_ix == sortcxix || dopoptosub(cxstack_ix) < sortcxix) {
16d20bd9
AD
1329 if (cxstack_ix > sortcxix)
1330 dounwind(sortcxix);
1ce6579f 1331 AvARRAY(curstack)[1] = *SP;
a0d0e21e
LW
1332 stack_sp = stack_base + 1;
1333 return 0;
1334 }
1335 }
1336
1337 cxix = dopoptosub(cxstack_ix);
1338 if (cxix < 0)
1339 DIE("Can't return outside a subroutine");
1340 if (cxix < cxstack_ix)
1341 dounwind(cxix);
1342
1343 POPBLOCK(cx,newpm);
1344 switch (cx->cx_type) {
1345 case CXt_SUB:
1346 POPSUB(cx);
1347 break;
1348 case CXt_EVAL:
1349 POPEVAL(cx);
748a9306
LW
1350 if (optype == OP_REQUIRE &&
1351 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
1352 {
1353 char *name = cx->blk_eval.old_name;
1354 (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
1355 DIE("%s did not return a true value", name);
1356 }
a0d0e21e
LW
1357 break;
1358 default:
1359 DIE("panic: return");
1360 break;
1361 }
1362
1363 if (gimme == G_SCALAR) {
1364 if (MARK < SP)
1365 *++newsp = sv_mortalcopy(*SP);
1366 else
1367 *++newsp = &sv_undef;
a0d0e21e
LW
1368 }
1369 else {
a0d0e21e
LW
1370 while (MARK < SP)
1371 *++newsp = sv_mortalcopy(*++MARK);
1372 }
1373 curpm = newpm; /* Don't pop $1 et al till now */
1374 stack_sp = newsp;
1375
1376 LEAVE;
1377 return pop_return();
1378}
1379
1380PP(pp_last)
1381{
1382 dSP;
1383 I32 cxix;
1384 register CONTEXT *cx;
1385 I32 gimme;
1386 I32 optype;
1387 OP *nextop;
1388 SV **newsp;
1389 PMOP *newpm;
1390 SV **mark = stack_base + cxstack[cxstack_ix].blk_oldsp;
a0d0e21e
LW
1391
1392 if (op->op_flags & OPf_SPECIAL) {
1393 cxix = dopoptoloop(cxstack_ix);
1394 if (cxix < 0)
1395 DIE("Can't \"last\" outside a block");
1396 }
1397 else {
1398 cxix = dopoptolabel(cPVOP->op_pv);
1399 if (cxix < 0)
1400 DIE("Label not found for \"last %s\"", cPVOP->op_pv);
1401 }
1402 if (cxix < cxstack_ix)
1403 dounwind(cxix);
1404
1405 POPBLOCK(cx,newpm);
1406 switch (cx->cx_type) {
1407 case CXt_LOOP:
1408 POPLOOP(cx);
1409 nextop = cx->blk_loop.last_op->op_next;
1410 LEAVE;
1411 break;
1412 case CXt_EVAL:
1413 POPEVAL(cx);
1414 nextop = pop_return();
1415 break;
1416 case CXt_SUB:
1417 POPSUB(cx);
1418 nextop = pop_return();
1419 break;
1420 default:
1421 DIE("panic: last");
1422 break;
1423 }
1424
1425 if (gimme == G_SCALAR) {
1426 if (mark < SP)
1427 *++newsp = sv_mortalcopy(*SP);
1428 else
1429 *++newsp = &sv_undef;
1430 }
1431 else {
1432 while (mark < SP)
1433 *++newsp = sv_mortalcopy(*++mark);
1434 }
1435 curpm = newpm; /* Don't pop $1 et al till now */
1436 sp = newsp;
1437
1438 LEAVE;
1439 RETURNOP(nextop);
1440}
1441
1442PP(pp_next)
1443{
1444 I32 cxix;
1445 register CONTEXT *cx;
1446 I32 oldsave;
1447
1448 if (op->op_flags & OPf_SPECIAL) {
1449 cxix = dopoptoloop(cxstack_ix);
1450 if (cxix < 0)
1451 DIE("Can't \"next\" outside a block");
1452 }
1453 else {
1454 cxix = dopoptolabel(cPVOP->op_pv);
1455 if (cxix < 0)
1456 DIE("Label not found for \"next %s\"", cPVOP->op_pv);
1457 }
1458 if (cxix < cxstack_ix)
1459 dounwind(cxix);
1460
1461 TOPBLOCK(cx);
1462 oldsave = scopestack[scopestack_ix - 1];
1463 LEAVE_SCOPE(oldsave);
1464 return cx->blk_loop.next_op;
1465}
1466
1467PP(pp_redo)
1468{
1469 I32 cxix;
1470 register CONTEXT *cx;
1471 I32 oldsave;
1472
1473 if (op->op_flags & OPf_SPECIAL) {
1474 cxix = dopoptoloop(cxstack_ix);
1475 if (cxix < 0)
1476 DIE("Can't \"redo\" outside a block");
1477 }
1478 else {
1479 cxix = dopoptolabel(cPVOP->op_pv);
1480 if (cxix < 0)
1481 DIE("Label not found for \"redo %s\"", cPVOP->op_pv);
1482 }
1483 if (cxix < cxstack_ix)
1484 dounwind(cxix);
1485
1486 TOPBLOCK(cx);
1487 oldsave = scopestack[scopestack_ix - 1];
1488 LEAVE_SCOPE(oldsave);
1489 return cx->blk_loop.redo_op;
1490}
1491
1492static OP* lastgotoprobe;
1493
1494static OP *
1495dofindlabel(op,label,opstack)
1496OP *op;
1497char *label;
1498OP **opstack;
1499{
1500 OP *kid;
1501 OP **ops = opstack;
1502
1503 if (op->op_type == OP_LEAVE ||
1504 op->op_type == OP_SCOPE ||
1505 op->op_type == OP_LEAVELOOP ||
1506 op->op_type == OP_LEAVETRY)
1507 *ops++ = cUNOP->op_first;
1508 *ops = 0;
1509 if (op->op_flags & OPf_KIDS) {
1510 /* First try all the kids at this level, since that's likeliest. */
1511 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling) {
1512 if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
1513 kCOP->cop_label && strEQ(kCOP->cop_label, label))
1514 return kid;
1515 }
1516 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling) {
1517 if (kid == lastgotoprobe)
1518 continue;
1519 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
1520 if (ops > opstack &&
1521 (ops[-1]->op_type == OP_NEXTSTATE ||
1522 ops[-1]->op_type == OP_DBSTATE))
1523 *ops = kid;
1524 else
1525 *ops++ = kid;
1526 }
1527 if (op = dofindlabel(kid,label,ops))
1528 return op;
1529 }
1530 }
1531 *ops = 0;
1532 return 0;
1533}
1534
1535PP(pp_dump)
1536{
1537 return pp_goto(ARGS);
1538 /*NOTREACHED*/
1539}
1540
1541PP(pp_goto)
1542{
1543 dSP;
1544 OP *retop = 0;
1545 I32 ix;
1546 register CONTEXT *cx;
1547 OP *enterops[64];
1548 char *label;
1549 int do_dump = (op->op_type == OP_DUMP);
1550
1551 label = 0;
1552 if (op->op_flags & OPf_STACKED) {
1553 SV *sv = POPs;
1554
1555 /* This egregious kludge implements goto &subroutine */
1556 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
1557 I32 cxix;
1558 register CONTEXT *cx;
1559 CV* cv = (CV*)SvRV(sv);
1560 SV** mark;
1561 I32 items = 0;
1562 I32 oldsave;
1563
4aa0a1f7
AD
1564 if (!CvROOT(cv) && !CvXSUB(cv)) {
1565 if (CvGV(cv)) {
1566 SV *tmpstr = sv_newmortal();
e5cf08de 1567 gv_efullname3(tmpstr, CvGV(cv), Nullch);
4aa0a1f7
AD
1568 DIE("Goto undefined subroutine &%s",SvPVX(tmpstr));
1569 }
1570 DIE("Goto undefined subroutine");
1571 }
1572
a0d0e21e
LW
1573 /* First do some returnish stuff. */
1574 cxix = dopoptosub(cxstack_ix);
1575 if (cxix < 0)
1576 DIE("Can't goto subroutine outside a subroutine");
1577 if (cxix < cxstack_ix)
1578 dounwind(cxix);
1579 TOPBLOCK(cx);
1580 mark = stack_sp;
1581 if (cx->blk_sub.hasargs) { /* put @_ back onto stack */
1582 AV* av = cx->blk_sub.argarray;
1583
1584 items = AvFILL(av) + 1;
1ce6579f 1585 stack_sp++;
1586 EXTEND(stack_sp, items); /* @_ could have been extended. */
1587 Copy(AvARRAY(av), stack_sp, items, SV*);
a0d0e21e 1588 stack_sp += items;
2c05e328 1589 SvREFCNT_dec(GvAV(defgv));
a0d0e21e 1590 GvAV(defgv) = cx->blk_sub.savearray;
a0d0e21e 1591 AvREAL_off(av);
4633a7c4 1592 av_clear(av);
a0d0e21e
LW
1593 }
1594 if (!(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
1595 SvREFCNT_dec(cx->blk_sub.cv);
1596 oldsave = scopestack[scopestack_ix - 1];
1597 LEAVE_SCOPE(oldsave);
1598
1599 /* Now do some callish stuff. */
1600 SAVETMPS;
1601 if (CvXSUB(cv)) {
1602 if (CvOLDSTYLE(cv)) {
ecfc5424 1603 I32 (*fp3)_((int,int,int));
a0d0e21e
LW
1604 while (sp > mark) {
1605 sp[1] = sp[0];
1606 sp--;
1607 }
ecfc5424
AD
1608 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
1609 items = (*fp3)(CvXSUBANY(cv).any_i32,
1610 mark - stack_base + 1,
1611 items);
a0d0e21e
LW
1612 sp = stack_base + items;
1613 }
1614 else {
1ce6579f 1615 stack_sp--; /* There is no cv arg. */
a0d0e21e
LW
1616 (void)(*CvXSUB(cv))(cv);
1617 }
1618 LEAVE;
1619 return pop_return();
1620 }
1621 else {
1622 AV* padlist = CvPADLIST(cv);
1623 SV** svp = AvARRAY(padlist);
1624 cx->blk_sub.cv = cv;
1625 cx->blk_sub.olddepth = CvDEPTH(cv);
1626 CvDEPTH(cv)++;
1627 if (CvDEPTH(cv) < 2)
1628 (void)SvREFCNT_inc(cv);
1629 else { /* save temporaries on recursion? */
1630 if (CvDEPTH(cv) == 100 && dowarn)
44a8e56a 1631 sub_crush_depth(cv);
a0d0e21e
LW
1632 if (CvDEPTH(cv) > AvFILL(padlist)) {
1633 AV *newpad = newAV();
4aa0a1f7 1634 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
a0d0e21e
LW
1635 I32 ix = AvFILL((AV*)svp[1]);
1636 svp = AvARRAY(svp[0]);
748a9306 1637 for ( ;ix > 0; ix--) {
a0d0e21e 1638 if (svp[ix] != &sv_undef) {
748a9306 1639 char *name = SvPVX(svp[ix]);
5f05dabc 1640 if ((SvFLAGS(svp[ix]) & SVf_FAKE)
1641 || *name == '&')
1642 {
1643 /* outer lexical or anon code */
748a9306 1644 av_store(newpad, ix,
4aa0a1f7 1645 SvREFCNT_inc(oldpad[ix]) );
748a9306
LW
1646 }
1647 else { /* our own lexical */
1648 if (*name == '@')
1649 av_store(newpad, ix, sv = (SV*)newAV());
1650 else if (*name == '%')
1651 av_store(newpad, ix, sv = (SV*)newHV());
1652 else
1653 av_store(newpad, ix, sv = NEWSV(0,0));
1654 SvPADMY_on(sv);
1655 }
a0d0e21e
LW
1656 }
1657 else {
748a9306 1658 av_store(newpad, ix, sv = NEWSV(0,0));
a0d0e21e
LW
1659 SvPADTMP_on(sv);
1660 }
1661 }
1662 if (cx->blk_sub.hasargs) {
1663 AV* av = newAV();
1664 av_extend(av, 0);
1665 av_store(newpad, 0, (SV*)av);
1666 AvFLAGS(av) = AVf_REIFY;
1667 }
1668 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
1669 AvFILL(padlist) = CvDEPTH(cv);
1670 svp = AvARRAY(padlist);
1671 }
1672 }
1673 SAVESPTR(curpad);
1674 curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
1675 if (cx->blk_sub.hasargs) {
1676 AV* av = (AV*)curpad[0];
1677 SV** ary;
1678
1679 cx->blk_sub.savearray = GvAV(defgv);
1680 cx->blk_sub.argarray = av;
2c05e328 1681 GvAV(defgv) = (AV*)SvREFCNT_inc(av);
a0d0e21e
LW
1682 ++mark;
1683
1684 if (items >= AvMAX(av) + 1) {
1685 ary = AvALLOC(av);
1686 if (AvARRAY(av) != ary) {
1687 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
1688 SvPVX(av) = (char*)ary;
1689 }
1690 if (items >= AvMAX(av) + 1) {
1691 AvMAX(av) = items - 1;
1692 Renew(ary,items+1,SV*);
1693 AvALLOC(av) = ary;
1694 SvPVX(av) = (char*)ary;
1695 }
1696 }
1697 Copy(mark,AvARRAY(av),items,SV*);
1698 AvFILL(av) = items - 1;
1699
1700 while (items--) {
1701 if (*mark)
1702 SvTEMP_off(*mark);
1703 mark++;
1704 }
1705 }
f967eb5f 1706 if (perldb && curstash != debstash) {
44a8e56a 1707 /*
1708 * We do not care about using sv to call CV;
1709 * it's for informational purposes only.
1710 */
1ce6579f 1711 SV *sv = GvSV(DBsub);
1712 save_item(sv);
e5cf08de 1713 gv_efullname3(sv, CvGV(cv), Nullch);
1ce6579f 1714 }
a0d0e21e
LW
1715 RETURNOP(CvSTART(cv));
1716 }
1717 }
1718 else
1719 label = SvPV(sv,na);
1720 }
1721 else if (op->op_flags & OPf_SPECIAL) {
1722 if (! do_dump)
1723 DIE("goto must have label");
1724 }
1725 else
1726 label = cPVOP->op_pv;
1727
1728 if (label && *label) {
1729 OP *gotoprobe = 0;
1730
1731 /* find label */
1732
1733 lastgotoprobe = 0;
1734 *enterops = 0;
1735 for (ix = cxstack_ix; ix >= 0; ix--) {
1736 cx = &cxstack[ix];
1737 switch (cx->cx_type) {
1738 case CXt_SUB:
1739 gotoprobe = CvROOT(cx->blk_sub.cv);
1740 break;
1741 case CXt_EVAL:
1742 gotoprobe = eval_root; /* XXX not good for nested eval */
1743 break;
1744 case CXt_LOOP:
1745 gotoprobe = cx->blk_oldcop->op_sibling;
1746 break;
1747 case CXt_SUBST:
1748 continue;
1749 case CXt_BLOCK:
1750 if (ix)
1751 gotoprobe = cx->blk_oldcop->op_sibling;
1752 else
1753 gotoprobe = main_root;
1754 break;
1755 default:
1756 if (ix)
1757 DIE("panic: goto");
1758 else
1759 gotoprobe = main_root;
1760 break;
1761 }
1762 retop = dofindlabel(gotoprobe, label, enterops);
1763 if (retop)
1764 break;
1765 lastgotoprobe = gotoprobe;
1766 }
1767 if (!retop)
1768 DIE("Can't find label %s", label);
1769
1770 /* pop unwanted frames */
1771
1772 if (ix < cxstack_ix) {
1773 I32 oldsave;
1774
1775 if (ix < 0)
1776 ix = 0;
1777 dounwind(ix);
1778 TOPBLOCK(cx);
1779 oldsave = scopestack[scopestack_ix];
1780 LEAVE_SCOPE(oldsave);
1781 }
1782
1783 /* push wanted frames */
1784
748a9306 1785 if (*enterops && enterops[1]) {
a0d0e21e 1786 OP *oldop = op;
748a9306 1787 for (ix = 1; enterops[ix]; ix++) {
a0d0e21e
LW
1788 op = enterops[ix];
1789 (*op->op_ppaddr)();
1790 }
1791 op = oldop;
1792 }
1793 }
1794
1795 if (do_dump) {
a5f75d66
AD
1796#ifdef VMS
1797 if (!retop) retop = main_start;
1798#endif
a0d0e21e
LW
1799 restartop = retop;
1800 do_undump = TRUE;
1801
1802 my_unexec();
1803
1804 restartop = 0; /* hmm, must be GNU unexec().. */
1805 do_undump = FALSE;
1806 }
1807
1ce6579f 1808 if (curstack == signalstack) {
748a9306 1809 restartop = retop;
a5f75d66 1810 Siglongjmp(top_env, 3);
748a9306
LW
1811 }
1812
a0d0e21e
LW
1813 RETURNOP(retop);
1814}
1815
1816PP(pp_exit)
1817{
1818 dSP;
1819 I32 anum;
1820
1821 if (MAXARG < 1)
1822 anum = 0;
1823 else
1824 anum = SvIVx(POPs);
1825 my_exit(anum);
1826 PUSHs(&sv_undef);
1827 RETURN;
1828}
1829
1830#ifdef NOTYET
1831PP(pp_nswitch)
1832{
1833 dSP;
1834 double value = SvNVx(GvSV(cCOP->cop_gv));
1835 register I32 match = I_32(value);
1836
1837 if (value < 0.0) {
1838 if (((double)match) > value)
1839 --match; /* was fractional--truncate other way */
1840 }
1841 match -= cCOP->uop.scop.scop_offset;
1842 if (match < 0)
1843 match = 0;
1844 else if (match > cCOP->uop.scop.scop_max)
1845 match = cCOP->uop.scop.scop_max;
1846 op = cCOP->uop.scop.scop_next[match];
1847 RETURNOP(op);
1848}
1849
1850PP(pp_cswitch)
1851{
1852 dSP;
1853 register I32 match;
1854
1855 if (multiline)
1856 op = op->op_next; /* can't assume anything */
1857 else {
1858 match = *(SvPVx(GvSV(cCOP->cop_gv), na)) & 255;
1859 match -= cCOP->uop.scop.scop_offset;
1860 if (match < 0)
1861 match = 0;
1862 else if (match > cCOP->uop.scop.scop_max)
1863 match = cCOP->uop.scop.scop_max;
1864 op = cCOP->uop.scop.scop_next[match];
1865 }
1866 RETURNOP(op);
1867}
1868#endif
1869
1870/* Eval. */
1871
1872static void
1873save_lines(array, sv)
1874AV *array;
1875SV *sv;
1876{
1877 register char *s = SvPVX(sv);
1878 register char *send = SvPVX(sv) + SvCUR(sv);
1879 register char *t;
1880 register I32 line = 1;
1881
1882 while (s && s < send) {
1883 SV *tmpstr = NEWSV(85,0);
1884
1885 sv_upgrade(tmpstr, SVt_PVMG);
1886 t = strchr(s, '\n');
1887 if (t)
1888 t++;
1889 else
1890 t = send;
1891
1892 sv_setpvn(tmpstr, s, t - s);
1893 av_store(array, line++, tmpstr);
1894 s = t;
1895 }
1896}
1897
1898static OP *
1899doeval(gimme)
1900int gimme;
1901{
1902 dSP;
1903 OP *saveop = op;
1904 HV *newstash;
ff3ff8d1 1905 CV *caller;
748a9306 1906 AV* comppadlist;
a0d0e21e
LW
1907
1908 in_eval = 1;
1909
1ce6579f 1910 PUSHMARK(SP);
1911
a0d0e21e
LW
1912 /* set up a scratch pad */
1913
55497cff 1914 SAVEI32(padix);
a0d0e21e
LW
1915 SAVESPTR(curpad);
1916 SAVESPTR(comppad);
1917 SAVESPTR(comppad_name);
55497cff 1918 SAVEI32(comppad_name_fill);
1919 SAVEI32(min_intro_pending);
1920 SAVEI32(max_intro_pending);
748a9306 1921
ff3ff8d1 1922 caller = compcv;
748a9306
LW
1923 SAVESPTR(compcv);
1924 compcv = (CV*)NEWSV(1104,0);
1925 sv_upgrade((SV *)compcv, SVt_PVCV);
07055b4c 1926 CvUNIQUE_on(compcv);
748a9306 1927
a0d0e21e
LW
1928 comppad = newAV();
1929 comppad_name = newAV();
1930 comppad_name_fill = 0;
1931 min_intro_pending = 0;
1932 av_push(comppad, Nullsv);
1933 curpad = AvARRAY(comppad);
1934 padix = 0;
1935
748a9306
LW
1936 comppadlist = newAV();
1937 AvREAL_off(comppadlist);
8e07c86e
AD
1938 av_store(comppadlist, 0, (SV*)comppad_name);
1939 av_store(comppadlist, 1, (SV*)comppad);
748a9306 1940 CvPADLIST(compcv) = comppadlist;
2c05e328
CS
1941
1942 if (saveop->op_type != OP_REQUIRE)
1943 CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc(caller);
07055b4c 1944
8e07c86e 1945 SAVEFREESV(compcv);
748a9306 1946
a0d0e21e
LW
1947 /* make sure we compile in the right package */
1948
1949 newstash = curcop->cop_stash;
1950 if (curstash != newstash) {
1951 SAVESPTR(curstash);
1952 curstash = newstash;
1953 }
1954 SAVESPTR(beginav);
1955 beginav = newAV();
1956 SAVEFREESV(beginav);
1957
1958 /* try to compile it */
1959
1960 eval_root = Nullop;
1961 error_count = 0;
1962 curcop = &compiling;
1963 curcop->cop_arybase = 0;
c07a80fd 1964 SvREFCNT_dec(rs);
1965 rs = newSVpv("\n", 1);
1ce6579f 1966 if (saveop->op_flags & OPf_SPECIAL)
1967 in_eval |= 4;
1968 else
1969 sv_setpv(GvSV(errgv),"");
a0d0e21e
LW
1970 if (yyparse() || error_count || !eval_root) {
1971 SV **newsp;
1972 I32 gimme;
1973 CONTEXT *cx;
1974 I32 optype;
1975
1976 op = saveop;
1977 if (eval_root) {
1978 op_free(eval_root);
1979 eval_root = Nullop;
1980 }
1ce6579f 1981 SP = stack_base + POPMARK; /* pop original mark */
a0d0e21e
LW
1982 POPBLOCK(cx,curpm);
1983 POPEVAL(cx);
1984 pop_return();
1985 lex_end();
1986 LEAVE;
1987 if (optype == OP_REQUIRE)
4633a7c4 1988 DIE("%s", SvPVx(GvSV(errgv), na));
c07a80fd 1989 SvREFCNT_dec(rs);
1990 rs = SvREFCNT_inc(nrs);
a0d0e21e
LW
1991 RETPUSHUNDEF;
1992 }
c07a80fd 1993 SvREFCNT_dec(rs);
1994 rs = SvREFCNT_inc(nrs);
a0d0e21e 1995 compiling.cop_line = 0;
a0d0e21e
LW
1996 SAVEFREEOP(eval_root);
1997 if (gimme & G_ARRAY)
1998 list(eval_root);
1999 else
2000 scalar(eval_root);
2001
2002 DEBUG_x(dump_eval());
2003
55497cff 2004 /* Register with debugger: */
2005
2006 if (perldb && saveop->op_type == OP_REQUIRE) {
2007 CV *cv = perl_get_cv("DB::postponed", FALSE);
2008
2009 if (cv) {
2010 dSP;
2011 PUSHMARK(sp);
2012 XPUSHs((SV*)compiling.cop_filegv);
2013 PUTBACK;
2014 perl_call_sv((SV*)cv, G_DISCARD);
2015 }
2016 }
2017
a0d0e21e
LW
2018 /* compiled okay, so do it */
2019
1ce6579f 2020 SP = stack_base + POPMARK; /* pop original mark */
a0d0e21e
LW
2021 RETURNOP(eval_start);
2022}
2023
2024PP(pp_require)
2025{
2026 dSP;
2027 register CONTEXT *cx;
2028 SV *sv;
2029 char *name;
2030 char *tmpname;
2031 SV** svp;
2032 I32 gimme = G_SCALAR;
760ac839 2033 PerlIO *tryrsfp = 0;
a0d0e21e
LW
2034
2035 sv = POPs;
4633a7c4 2036 if (SvNIOKp(sv) && !SvPOKp(sv)) {
36477c24 2037 SET_NUMERIC_STANDARD();
a5f75d66
AD
2038 if (atof(patchlevel) + 0.00000999 < SvNV(sv))
2039 DIE("Perl %s required--this is only version %s, stopped",
2040 SvPV(sv,na),patchlevel);
a0d0e21e
LW
2041 RETPUSHYES;
2042 }
2043 name = SvPV(sv, na);
2044 if (!*name)
2045 DIE("Null filename used");
4633a7c4 2046 TAINT_PROPER("require");
a0d0e21e
LW
2047 if (op->op_type == OP_REQUIRE &&
2048 (svp = hv_fetch(GvHVn(incgv), name, SvCUR(sv), 0)) &&
2049 *svp != &sv_undef)
2050 RETPUSHYES;
2051
2052 /* prepare to compile file */
2053
2054 tmpname = savepv(name);
2055 if (*tmpname == '/' ||
2056 (*tmpname == '.' &&
2057 (tmpname[1] == '/' ||
748a9306 2058 (tmpname[1] == '.' && tmpname[2] == '/')))
4633a7c4
LW
2059#ifdef DOSISH
2060 || (tmpname[0] && tmpname[1] == ':')
2061#endif
748a9306 2062#ifdef VMS
bbce6d69 2063 || (strchr(tmpname,':') || ((*tmpname == '[' || *tmpname == '<') &&
2064 (isALNUM(tmpname[1]) || strchr("$-_]>",tmpname[1]))))
748a9306
LW
2065#endif
2066 )
a0d0e21e 2067 {
760ac839 2068 tryrsfp = PerlIO_open(tmpname,"r");
a0d0e21e
LW
2069 }
2070 else {
2071 AV *ar = GvAVn(incgv);
2072 I32 i;
748a9306 2073#ifdef VMS
bbce6d69 2074 char unixified[256];
2075 if (tounixspec_ts(tmpname,unixified) != NULL)
2076 for (i = 0; i <= AvFILL(ar); i++) {
748a9306 2077 if (tounixpath_ts(SvPVx(*av_fetch(ar, i, TRUE), na),buf) == NULL)
4633a7c4 2078 continue;
bbce6d69 2079 strcat(buf,unixified);
748a9306 2080#else
bbce6d69 2081 for (i = 0; i <= AvFILL(ar); i++) {
a0d0e21e
LW
2082 (void)sprintf(buf, "%s/%s",
2083 SvPVx(*av_fetch(ar, i, TRUE), na), name);
748a9306 2084#endif
760ac839 2085 tryrsfp = PerlIO_open(buf, "r");
a0d0e21e
LW
2086 if (tryrsfp) {
2087 char *s = buf;
2088
2089 if (*s == '.' && s[1] == '/')
2090 s += 2;
2091 Safefree(tmpname);
2092 tmpname = savepv(s);
2093 break;
2094 }
2095 }
2096 }
2097 SAVESPTR(compiling.cop_filegv);
2098 compiling.cop_filegv = gv_fetchfile(tmpname);
2099 Safefree(tmpname);
2100 tmpname = Nullch;
2101 if (!tryrsfp) {
2102 if (op->op_type == OP_REQUIRE) {
2103 sprintf(tokenbuf,"Can't locate %s in @INC", name);
2104 if (instr(tokenbuf,".h "))
2105 strcat(tokenbuf," (change .h to .ph maybe?)");
2106 if (instr(tokenbuf,".ph "))
2107 strcat(tokenbuf," (did you run h2ph?)");
2108 DIE("%s",tokenbuf);
2109 }
2110
2111 RETPUSHUNDEF;
2112 }
2113
2114 /* Assume success here to prevent recursive requirement. */
2115 (void)hv_store(GvHVn(incgv), name, strlen(name),
2116 newSVsv(GvSV(compiling.cop_filegv)), 0 );
2117
2118 ENTER;
2119 SAVETMPS;
2120 lex_start(sv_2mortal(newSVpv("",0)));
e50aee73
AD
2121 if (rsfp_filters){
2122 save_aptr(&rsfp_filters);
2123 rsfp_filters = NULL;
2124 }
2125
a0d0e21e
LW
2126 rsfp = tryrsfp;
2127 name = savepv(name);
2128 SAVEFREEPV(name);
2129 SAVEI32(hints);
2130 hints = 0;
2131
2132 /* switch to eval mode */
2133
2134 push_return(op->op_next);
2135 PUSHBLOCK(cx, CXt_EVAL, SP);
2136 PUSHEVAL(cx, name, compiling.cop_filegv);
2137
2138 compiling.cop_line = 0;
2139
2140 PUTBACK;
2141 return doeval(G_SCALAR);
2142}
2143
2144PP(pp_dofile)
2145{
2146 return pp_require(ARGS);
2147}
2148
2149PP(pp_entereval)
2150{
2151 dSP;
2152 register CONTEXT *cx;
2153 dPOPss;
55497cff 2154 I32 gimme = GIMME, was = sub_generation;
2155 char tmpbuf[32], *safestr;
a0d0e21e 2156 STRLEN len;
55497cff 2157 OP *ret;
a0d0e21e
LW
2158
2159 if (!SvPV(sv,len) || !len)
2160 RETPUSHUNDEF;
748a9306 2161 TAINT_PROPER("eval");
a0d0e21e
LW
2162
2163 ENTER;
a0d0e21e 2164 lex_start(sv);
748a9306 2165 SAVETMPS;
a0d0e21e
LW
2166
2167 /* switch to eval mode */
2168
748a9306 2169 SAVESPTR(compiling.cop_filegv);
a0d0e21e
LW
2170 sprintf(tmpbuf, "_<(eval %d)", ++evalseq);
2171 compiling.cop_filegv = gv_fetchfile(tmpbuf+2);
2172 compiling.cop_line = 1;
55497cff 2173 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
2174 deleting the eval's FILEGV from the stash before gv_check() runs
2175 (i.e. before run-time proper). To work around the coredump that
2176 ensues, we always turn GvMULTI_on for any globals that were
2177 introduced within evals. See force_ident(). GSAR 96-10-12 */
2178 safestr = savepv(tmpbuf);
2179 SAVEDELETE(defstash, safestr, strlen(safestr));
a0d0e21e
LW
2180 SAVEI32(hints);
2181 hints = op->op_targ;
2182
2183 push_return(op->op_next);
2184 PUSHBLOCK(cx, CXt_EVAL, SP);
2185 PUSHEVAL(cx, 0, compiling.cop_filegv);
2186
2187 /* prepare to compile string */
2188
2189 if (perldb && curstash != debstash)
2190 save_lines(GvAV(compiling.cop_filegv), linestr);
2191 PUTBACK;
55497cff 2192 ret = doeval(gimme);
2193 if (perldb && was != sub_generation) { /* Some subs defined here. */
2194 strcpy(safestr, "_<(eval )"); /* Anything fake and short. */
2195 }
2196 return ret;
a0d0e21e
LW
2197}
2198
2199PP(pp_leaveeval)
2200{
2201 dSP;
2202 register SV **mark;
2203 SV **newsp;
2204 PMOP *newpm;
2205 I32 gimme;
2206 register CONTEXT *cx;
2207 OP *retop;
760ac839 2208 U8 save_flags = op -> op_flags;
a0d0e21e
LW
2209 I32 optype;
2210
2211 POPBLOCK(cx,newpm);
2212 POPEVAL(cx);
2213 retop = pop_return();
2214
2215 if (gimme == G_SCALAR) {
2216 if (op->op_private & OPpLEAVE_VOID)
2217 MARK = newsp;
2218 else {
2219 MARK = newsp + 1;
2220 if (MARK <= SP) {
2221 if (SvFLAGS(TOPs) & SVs_TEMP)
2222 *MARK = TOPs;
2223 else
2224 *MARK = sv_mortalcopy(TOPs);
2225 }
2226 else {
2227 MEXTEND(mark,0);
2228 *MARK = &sv_undef;
2229 }
2230 }
2231 SP = MARK;
2232 }
2233 else {
2234 for (mark = newsp + 1; mark <= SP; mark++)
760ac839 2235 if (!(SvFLAGS(*mark) & SVs_TEMP))
a0d0e21e
LW
2236 *mark = sv_mortalcopy(*mark);
2237 /* in case LEAVE wipes old return values */
2238 }
2239 curpm = newpm; /* Don't pop $1 et al till now */
2240
1ce6579f 2241 if (optype == OP_REQUIRE &&
2242 !(gimme == G_SCALAR ? SvTRUE(*sp) : sp > newsp)) {
a0d0e21e
LW
2243 char *name = cx->blk_eval.old_name;
2244
1ce6579f 2245 /* Unassume the success we assumed earlier. */
2246 (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
2247 retop = die("%s did not return a true value", name);
a0d0e21e
LW
2248 }
2249
2250 lex_end();
2251 LEAVE;
760ac839 2252 if (!(save_flags & OPf_SPECIAL))
1ce6579f 2253 sv_setpv(GvSV(errgv),"");
a0d0e21e
LW
2254
2255 RETURNOP(retop);
2256}
2257
a0d0e21e
LW
2258PP(pp_entertry)
2259{
2260 dSP;
2261 register CONTEXT *cx;
2262 I32 gimme = GIMME;
2263
2264 ENTER;
2265 SAVETMPS;
2266
2267 push_return(cLOGOP->op_other->op_next);
2268 PUSHBLOCK(cx, CXt_EVAL, SP);
2269 PUSHEVAL(cx, 0, 0);
2270 eval_root = op; /* Only needed so that goto works right. */
2271
2272 in_eval = 1;
4633a7c4 2273 sv_setpv(GvSV(errgv),"");
a0d0e21e
LW
2274 RETURN;
2275}
2276
2277PP(pp_leavetry)
2278{
2279 dSP;
2280 register SV **mark;
2281 SV **newsp;
2282 PMOP *newpm;
2283 I32 gimme;
2284 register CONTEXT *cx;
2285 I32 optype;
2286
2287 POPBLOCK(cx,newpm);
2288 POPEVAL(cx);
2289 pop_return();
2290
2291 if (gimme == G_SCALAR) {
2292 if (op->op_private & OPpLEAVE_VOID)
2293 MARK = newsp;
2294 else {
2295 MARK = newsp + 1;
2296 if (MARK <= SP) {
2297 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
2298 *MARK = TOPs;
2299 else
2300 *MARK = sv_mortalcopy(TOPs);
2301 }
2302 else {
2303 MEXTEND(mark,0);
2304 *MARK = &sv_undef;
2305 }
2306 }
2307 SP = MARK;
2308 }
2309 else {
2310 for (mark = newsp + 1; mark <= SP; mark++)
760ac839 2311 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP)))
a0d0e21e
LW
2312 *mark = sv_mortalcopy(*mark);
2313 /* in case LEAVE wipes old return values */
2314 }
2315 curpm = newpm; /* Don't pop $1 et al till now */
2316
2317 LEAVE;
4633a7c4 2318 sv_setpv(GvSV(errgv),"");
a0d0e21e
LW
2319 RETURN;
2320}
2321
2322static void
2323doparseform(sv)
2324SV *sv;
2325{
2326 STRLEN len;
2327 register char *s = SvPV_force(sv, len);
2328 register char *send = s + len;
2329 register char *base;
2330 register I32 skipspaces = 0;
2331 bool noblank;
2332 bool repeat;
2333 bool postspace = FALSE;
2334 U16 *fops;
2335 register U16 *fpc;
2336 U16 *linepc;
2337 register I32 arg;
2338 bool ischop;
2339
55497cff 2340 if (len == 0)
bbce6d69 2341 croak("Null picture in formline");
55497cff 2342
2343 New(804, fops, (send - s)*3+10, U16); /* Almost certainly too long... */
a0d0e21e
LW
2344 fpc = fops;
2345
2346 if (s < send) {
2347 linepc = fpc;
2348 *fpc++ = FF_LINEMARK;
2349 noblank = repeat = FALSE;
2350 base = s;
2351 }
2352
2353 while (s <= send) {
2354 switch (*s++) {
2355 default:
2356 skipspaces = 0;
2357 continue;
2358
2359 case '~':
2360 if (*s == '~') {
2361 repeat = TRUE;
2362 *s = ' ';
2363 }
2364 noblank = TRUE;
2365 s[-1] = ' ';
2366 /* FALL THROUGH */
2367 case ' ': case '\t':
2368 skipspaces++;
2369 continue;
2370
2371 case '\n': case 0:
2372 arg = s - base;
2373 skipspaces++;
2374 arg -= skipspaces;
2375 if (arg) {
5f05dabc 2376 if (postspace)
a0d0e21e 2377 *fpc++ = FF_SPACE;
a0d0e21e
LW
2378 *fpc++ = FF_LITERAL;
2379 *fpc++ = arg;
2380 }
5f05dabc 2381 postspace = FALSE;
a0d0e21e
LW
2382 if (s <= send)
2383 skipspaces--;
2384 if (skipspaces) {
2385 *fpc++ = FF_SKIP;
2386 *fpc++ = skipspaces;
2387 }
2388 skipspaces = 0;
2389 if (s <= send)
2390 *fpc++ = FF_NEWLINE;
2391 if (noblank) {
2392 *fpc++ = FF_BLANK;
2393 if (repeat)
2394 arg = fpc - linepc + 1;
2395 else
2396 arg = 0;
2397 *fpc++ = arg;
2398 }
2399 if (s < send) {
2400 linepc = fpc;
2401 *fpc++ = FF_LINEMARK;
2402 noblank = repeat = FALSE;
2403 base = s;
2404 }
2405 else
2406 s++;
2407 continue;
2408
2409 case '@':
2410 case '^':
2411 ischop = s[-1] == '^';
2412
2413 if (postspace) {
2414 *fpc++ = FF_SPACE;
2415 postspace = FALSE;
2416 }
2417 arg = (s - base) - 1;
2418 if (arg) {
2419 *fpc++ = FF_LITERAL;
2420 *fpc++ = arg;
2421 }
2422
2423 base = s - 1;
2424 *fpc++ = FF_FETCH;
2425 if (*s == '*') {
2426 s++;
2427 *fpc++ = 0;
2428 *fpc++ = FF_LINEGLOB;
2429 }
2430 else if (*s == '#' || (*s == '.' && s[1] == '#')) {
2431 arg = ischop ? 512 : 0;
2432 base = s - 1;
2433 while (*s == '#')
2434 s++;
2435 if (*s == '.') {
2436 char *f;
2437 s++;
2438 f = s;
2439 while (*s == '#')
2440 s++;
2441 arg |= 256 + (s - f);
2442 }
2443 *fpc++ = s - base; /* fieldsize for FETCH */
2444 *fpc++ = FF_DECIMAL;
2445 *fpc++ = arg;
2446 }
2447 else {
2448 I32 prespace = 0;
2449 bool ismore = FALSE;
2450
2451 if (*s == '>') {
2452 while (*++s == '>') ;
2453 prespace = FF_SPACE;
2454 }
2455 else if (*s == '|') {
2456 while (*++s == '|') ;
2457 prespace = FF_HALFSPACE;
2458 postspace = TRUE;
2459 }
2460 else {
2461 if (*s == '<')
2462 while (*++s == '<') ;
2463 postspace = TRUE;
2464 }
2465 if (*s == '.' && s[1] == '.' && s[2] == '.') {
2466 s += 3;
2467 ismore = TRUE;
2468 }
2469 *fpc++ = s - base; /* fieldsize for FETCH */
2470
2471 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
2472
2473 if (prespace)
2474 *fpc++ = prespace;
2475 *fpc++ = FF_ITEM;
2476 if (ismore)
2477 *fpc++ = FF_MORE;
2478 if (ischop)
2479 *fpc++ = FF_CHOP;
2480 }
2481 base = s;
2482 skipspaces = 0;
2483 continue;
2484 }
2485 }
2486 *fpc++ = FF_END;
2487
2488 arg = fpc - fops;
2489 { /* need to jump to the next word */
2490 int z;
2491 z = WORD_ALIGN - SvCUR(sv) % WORD_ALIGN;
2492 SvGROW(sv, SvCUR(sv) + z + arg * sizeof(U16) + 4);
2493 s = SvPVX(sv) + SvCUR(sv) + z;
2494 }
2495 Copy(fops, s, arg, U16);
2496 Safefree(fops);
55497cff 2497 sv_magic(sv, Nullsv, 'f', Nullch, 0);
a0d0e21e
LW
2498 SvCOMPILED_on(sv);
2499}