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