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