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