This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
integrate changes#2978,2979 from mainline
[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
76e3520e 28#ifdef PERL_OBJECT
4c2891ed 29#define CALLOP this->*PL_op
76e3520e 30#else
533c011a 31#define CALLOP *PL_op
1e422769 32static OP *docatch _((OP *o));
5dc0d613 33static OP *dofindlabel _((OP *o, char *label, OP **opstack, OP **oplimit));
a0d0e21e
LW
34static void doparseform _((SV *sv));
35static I32 dopoptoeval _((I32 startingblock));
36static I32 dopoptolabel _((char *label));
37static I32 dopoptoloop _((I32 startingblock));
38static I32 dopoptosub _((I32 startingblock));
2c375eb9 39static I32 dopoptosub_at _((PERL_CONTEXT *cxstk, I32 startingblock));
a0d0e21e 40static void save_lines _((AV *array, SV *sv));
745d3a65
HM
41static I32 sortcv _((SV *a, SV *b));
42static void qsortsv _((SV **array, size_t num_elts, I32 (*fun)(SV *a, SV *b)));
c277df42 43static OP *doeval _((int gimme, OP** startop));
76e3520e 44#endif
a0d0e21e 45
a0d0e21e
LW
46PP(pp_wantarray)
47{
4e35701f 48 djSP;
a0d0e21e
LW
49 I32 cxix;
50 EXTEND(SP, 1);
51
52 cxix = dopoptosub(cxstack_ix);
53 if (cxix < 0)
54 RETPUSHUNDEF;
55
54310121 56 switch (cxstack[cxix].blk_gimme) {
57 case G_ARRAY:
a0d0e21e 58 RETPUSHYES;
54310121 59 case G_SCALAR:
a0d0e21e 60 RETPUSHNO;
54310121 61 default:
62 RETPUSHUNDEF;
63 }
a0d0e21e
LW
64}
65
66PP(pp_regcmaybe)
67{
68 return NORMAL;
69}
70
2cd61cdb
IZ
71PP(pp_regcreset)
72{
73 /* XXXX Should store the old value to allow for tie/overload - and
74 restore in regcomp, where marked with XXXX. */
3280af22 75 PL_reginterp_cnt = 0;
2cd61cdb
IZ
76 return NORMAL;
77}
78
b3eb6a9b
GS
79PP(pp_regcomp)
80{
4e35701f 81 djSP;
a0d0e21e
LW
82 register PMOP *pm = (PMOP*)cLOGOP->op_other;
83 register char *t;
84 SV *tmpstr;
85 STRLEN len;
c277df42 86 MAGIC *mg = Null(MAGIC*);
a0d0e21e
LW
87
88 tmpstr = POPs;
b3eb6a9b 89 if (SvROK(tmpstr)) {
227a8b4b 90 SV *sv = SvRV(tmpstr);
c277df42
IZ
91 if(SvMAGICAL(sv))
92 mg = mg_find(sv, 'r');
93 }
b3eb6a9b 94 if (mg) {
c277df42
IZ
95 regexp *re = (regexp *)mg->mg_obj;
96 ReREFCNT_dec(pm->op_pmregexp);
97 pm->op_pmregexp = ReREFCNT_inc(re);
98 }
99 else {
100 t = SvPV(tmpstr, len);
101
20408e3c 102 /* Check against the last compiled regexp. */
85aff577 103 if (!pm->op_pmregexp || !pm->op_pmregexp->precomp ||
20408e3c
GS
104 pm->op_pmregexp->prelen != len ||
105 memNE(pm->op_pmregexp->precomp, t, len))
85aff577 106 {
c277df42
IZ
107 if (pm->op_pmregexp) {
108 ReREFCNT_dec(pm->op_pmregexp);
109 pm->op_pmregexp = Null(REGEXP*); /* crucial if regcomp aborts */
110 }
533c011a 111 if (PL_op->op_flags & OPf_SPECIAL)
3280af22 112 PL_reginterp_cnt = I32_MAX; /* Mark as safe. */
a0d0e21e 113
c277df42 114 pm->op_pmflags = pm->op_pmpermflags; /* reset case sensitivity */
15e52e56 115 pm->op_pmregexp = CALLREGCOMP(t, t + len, pm);
3280af22 116 PL_reginterp_cnt = 0; /* XXXX Be extra paranoid - needed
2cd61cdb 117 inside tie/overload accessors. */
c277df42 118 }
4633a7c4 119 }
a0d0e21e 120
72311751 121#ifndef INCOMPLETE_TAINTS
3280af22
NIS
122 if (PL_tainting) {
123 if (PL_tainted)
72311751
GS
124 pm->op_pmdynflags |= PMdf_TAINTED;
125 else
126 pm->op_pmdynflags &= ~PMdf_TAINTED;
127 }
128#endif
129
3280af22
NIS
130 if (!pm->op_pmregexp->prelen && PL_curpm)
131 pm = PL_curpm;
a0d0e21e
LW
132 else if (strEQ("\\s+", pm->op_pmregexp->precomp))
133 pm->op_pmflags |= PMf_WHITE;
134
135 if (pm->op_pmflags & PMf_KEEP) {
c90c0ff4 136 pm->op_private &= ~OPpRUNTIME; /* no point compiling again */
533c011a 137 cLOGOP->op_first->op_next = PL_op->op_next;
a0d0e21e
LW
138 }
139 RETURN;
140}
141
142PP(pp_substcont)
143{
4e35701f 144 djSP;
a0d0e21e 145 register PMOP *pm = (PMOP*) cLOGOP->op_other;
c09156bb 146 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
a0d0e21e
LW
147 register SV *dstr = cx->sb_dstr;
148 register char *s = cx->sb_s;
149 register char *m = cx->sb_m;
150 char *orig = cx->sb_orig;
d9f97599 151 register REGEXP *rx = cx->sb_rx;
a0d0e21e 152
d9f97599 153 rxres_restore(&cx->sb_rxres, rx);
c90c0ff4 154
a0d0e21e
LW
155 if (cx->sb_iters++) {
156 if (cx->sb_iters > cx->sb_maxiters)
157 DIE("Substitution loop");
158
48c036b1
GS
159 if (!(cx->sb_rxtainted & 2) && SvTAINTED(TOPs))
160 cx->sb_rxtainted |= 2;
a0d0e21e 161 sv_catsv(dstr, POPs);
a0d0e21e
LW
162
163 /* Are we done */
15e52e56 164 if (cx->sb_once || !CALLREGEXEC(rx, s, cx->sb_strend, orig,
c277df42
IZ
165 s == m, Nullsv, NULL,
166 cx->sb_safebase ? 0 : REXEC_COPY_STR))
a0d0e21e
LW
167 {
168 SV *targ = cx->sb_targ;
169 sv_catpvn(dstr, s, cx->sb_strend - s);
748a9306 170
48c036b1 171 cx->sb_rxtainted |= RX_MATCH_TAINTED(rx);
9212bbba 172
4633a7c4 173 (void)SvOOK_off(targ);
cb0b1708 174 Safefree(SvPVX(targ));
748a9306
LW
175 SvPVX(targ) = SvPVX(dstr);
176 SvCUR_set(targ, SvCUR(dstr));
177 SvLEN_set(targ, SvLEN(dstr));
178 SvPVX(dstr) = 0;
179 sv_free(dstr);
48c036b1
GS
180
181 TAINT_IF(cx->sb_rxtainted & 1);
182 PUSHs(sv_2mortal(newSViv((I32)cx->sb_iters - 1)));
183
a0d0e21e 184 (void)SvPOK_only(targ);
48c036b1 185 TAINT_IF(cx->sb_rxtainted);
a0d0e21e 186 SvSETMAGIC(targ);
9212bbba 187 SvTAINT(targ);
5cd24f17 188
4633a7c4 189 LEAVE_SCOPE(cx->sb_oldsave);
a0d0e21e
LW
190 POPSUBST(cx);
191 RETURNOP(pm->op_next);
192 }
193 }
d9f97599 194 if (rx->subbase && rx->subbase != orig) {
a0d0e21e
LW
195 m = s;
196 s = orig;
d9f97599 197 cx->sb_orig = orig = rx->subbase;
a0d0e21e
LW
198 s = orig + (m - s);
199 cx->sb_strend = s + (cx->sb_strend - m);
200 }
d9f97599 201 cx->sb_m = m = rx->startp[0];
a0d0e21e 202 sv_catpvn(dstr, s, m-s);
d9f97599
GS
203 cx->sb_s = rx->endp[0];
204 cx->sb_rxtainted |= RX_MATCH_TAINTED(rx);
205 rxres_save(&cx->sb_rxres, rx);
a0d0e21e
LW
206 RETURNOP(pm->op_pmreplstart);
207}
208
c90c0ff4 209void
d9f97599 210rxres_save(void **rsp, REGEXP *rx)
c90c0ff4 211{
212 UV *p = (UV*)*rsp;
213 U32 i;
214
d9f97599
GS
215 if (!p || p[1] < rx->nparens) {
216 i = 6 + rx->nparens * 2;
c90c0ff4 217 if (!p)
218 New(501, p, i, UV);
219 else
220 Renew(p, i, UV);
221 *rsp = (void*)p;
222 }
223
d9f97599
GS
224 *p++ = (UV)rx->subbase;
225 rx->subbase = Nullch;
c90c0ff4 226
d9f97599 227 *p++ = rx->nparens;
c90c0ff4 228
d9f97599
GS
229 *p++ = (UV)rx->subbeg;
230 *p++ = (UV)rx->subend;
231 for (i = 0; i <= rx->nparens; ++i) {
232 *p++ = (UV)rx->startp[i];
233 *p++ = (UV)rx->endp[i];
c90c0ff4 234 }
235}
236
237void
d9f97599 238rxres_restore(void **rsp, REGEXP *rx)
c90c0ff4 239{
240 UV *p = (UV*)*rsp;
241 U32 i;
242
d9f97599
GS
243 Safefree(rx->subbase);
244 rx->subbase = (char*)(*p);
c90c0ff4 245 *p++ = 0;
246
d9f97599 247 rx->nparens = *p++;
c90c0ff4 248
d9f97599
GS
249 rx->subbeg = (char*)(*p++);
250 rx->subend = (char*)(*p++);
251 for (i = 0; i <= rx->nparens; ++i) {
252 rx->startp[i] = (char*)(*p++);
253 rx->endp[i] = (char*)(*p++);
c90c0ff4 254 }
255}
256
257void
8ac85365 258rxres_free(void **rsp)
c90c0ff4 259{
260 UV *p = (UV*)*rsp;
261
262 if (p) {
263 Safefree((char*)(*p));
264 Safefree(p);
265 *rsp = Null(void*);
266 }
267}
268
a0d0e21e
LW
269PP(pp_formline)
270{
4e35701f 271 djSP; dMARK; dORIGMARK;
76e3520e 272 register SV *tmpForm = *++MARK;
a0d0e21e
LW
273 register U16 *fpc;
274 register char *t;
275 register char *f;
276 register char *s;
277 register char *send;
278 register I32 arg;
279 register SV *sv;
280 char *item;
281 I32 itemsize;
282 I32 fieldsize;
283 I32 lines = 0;
3280af22 284 bool chopspace = (strchr(PL_chopset, ' ') != Nullch);
a0d0e21e
LW
285 char *chophere;
286 char *linemark;
a0d0e21e
LW
287 double value;
288 bool gotsome;
289 STRLEN len;
290
76e3520e
GS
291 if (!SvMAGICAL(tmpForm) || !SvCOMPILED(tmpForm)) {
292 SvREADONLY_off(tmpForm);
293 doparseform(tmpForm);
a0d0e21e
LW
294 }
295
3280af22
NIS
296 SvPV_force(PL_formtarget, len);
297 t = SvGROW(PL_formtarget, len + SvCUR(tmpForm) + 1); /* XXX SvCUR bad */
a0d0e21e 298 t += len;
76e3520e 299 f = SvPV(tmpForm, len);
a0d0e21e 300 /* need to jump to the next word */
76e3520e 301 s = f + len + WORD_ALIGN - SvCUR(tmpForm) % WORD_ALIGN;
a0d0e21e
LW
302
303 fpc = (U16*)s;
304
305 for (;;) {
306 DEBUG_f( {
307 char *name = "???";
308 arg = -1;
309 switch (*fpc) {
310 case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
311 case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
312 case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
313 case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
314 case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
315
316 case FF_CHECKNL: name = "CHECKNL"; break;
317 case FF_CHECKCHOP: name = "CHECKCHOP"; break;
318 case FF_SPACE: name = "SPACE"; break;
319 case FF_HALFSPACE: name = "HALFSPACE"; break;
320 case FF_ITEM: name = "ITEM"; break;
321 case FF_CHOP: name = "CHOP"; break;
322 case FF_LINEGLOB: name = "LINEGLOB"; break;
323 case FF_NEWLINE: name = "NEWLINE"; break;
324 case FF_MORE: name = "MORE"; break;
325 case FF_LINEMARK: name = "LINEMARK"; break;
326 case FF_END: name = "END"; break;
327 }
328 if (arg >= 0)
760ac839 329 PerlIO_printf(PerlIO_stderr(), "%-16s%ld\n", name, (long) arg);
a0d0e21e 330 else
760ac839 331 PerlIO_printf(PerlIO_stderr(), "%-16s\n", name);
a0d0e21e
LW
332 } )
333 switch (*fpc++) {
334 case FF_LINEMARK:
335 linemark = t;
a0d0e21e
LW
336 lines++;
337 gotsome = FALSE;
338 break;
339
340 case FF_LITERAL:
341 arg = *fpc++;
342 while (arg--)
343 *t++ = *f++;
344 break;
345
346 case FF_SKIP:
347 f += *fpc++;
348 break;
349
350 case FF_FETCH:
351 arg = *fpc++;
352 f += arg;
353 fieldsize = arg;
354
355 if (MARK < SP)
356 sv = *++MARK;
357 else {
3280af22
NIS
358 sv = &PL_sv_no;
359 if (PL_dowarn)
a0d0e21e
LW
360 warn("Not enough format arguments");
361 }
362 break;
363
364 case FF_CHECKNL:
365 item = s = SvPV(sv, len);
366 itemsize = len;
367 if (itemsize > fieldsize)
368 itemsize = fieldsize;
369 send = chophere = s + itemsize;
370 while (s < send) {
371 if (*s & ~31)
372 gotsome = TRUE;
373 else if (*s == '\n')
374 break;
375 s++;
376 }
377 itemsize = s - item;
378 break;
379
380 case FF_CHECKCHOP:
381 item = s = SvPV(sv, len);
382 itemsize = len;
383 if (itemsize <= fieldsize) {
384 send = chophere = s + itemsize;
385 while (s < send) {
386 if (*s == '\r') {
387 itemsize = s - item;
388 break;
389 }
390 if (*s++ & ~31)
391 gotsome = TRUE;
392 }
393 }
394 else {
395 itemsize = fieldsize;
396 send = chophere = s + itemsize;
397 while (s < send || (s == send && isSPACE(*s))) {
398 if (isSPACE(*s)) {
399 if (chopspace)
400 chophere = s;
401 if (*s == '\r')
402 break;
403 }
404 else {
405 if (*s & ~31)
406 gotsome = TRUE;
3280af22 407 if (strchr(PL_chopset, *s))
a0d0e21e
LW
408 chophere = s + 1;
409 }
410 s++;
411 }
412 itemsize = chophere - item;
413 }
414 break;
415
416 case FF_SPACE:
417 arg = fieldsize - itemsize;
418 if (arg) {
419 fieldsize -= arg;
420 while (arg-- > 0)
421 *t++ = ' ';
422 }
423 break;
424
425 case FF_HALFSPACE:
426 arg = fieldsize - itemsize;
427 if (arg) {
428 arg /= 2;
429 fieldsize -= arg;
430 while (arg-- > 0)
431 *t++ = ' ';
432 }
433 break;
434
435 case FF_ITEM:
436 arg = itemsize;
437 s = item;
438 while (arg--) {
9d116dd7 439#ifdef EBCDIC
a0d0e21e 440 int ch = *t++ = *s++;
9d116dd7 441 if (iscntrl(ch))
a0d0e21e
LW
442#else
443 if ( !((*t++ = *s++) & ~31) )
a0d0e21e 444#endif
9d116dd7 445 t[-1] = ' ';
a0d0e21e
LW
446 }
447 break;
448
449 case FF_CHOP:
450 s = chophere;
451 if (chopspace) {
452 while (*s && isSPACE(*s))
453 s++;
454 }
455 sv_chop(sv,s);
456 break;
457
458 case FF_LINEGLOB:
459 item = s = SvPV(sv, len);
460 itemsize = len;
461 if (itemsize) {
462 gotsome = TRUE;
463 send = s + itemsize;
464 while (s < send) {
465 if (*s++ == '\n') {
466 if (s == send)
467 itemsize--;
468 else
469 lines++;
470 }
471 }
3280af22
NIS
472 SvCUR_set(PL_formtarget, t - SvPVX(PL_formtarget));
473 sv_catpvn(PL_formtarget, item, itemsize);
474 SvGROW(PL_formtarget, SvCUR(PL_formtarget) + SvCUR(tmpForm) + 1);
475 t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget);
a0d0e21e
LW
476 }
477 break;
478
479 case FF_DECIMAL:
480 /* If the field is marked with ^ and the value is undefined,
481 blank it out. */
482 arg = *fpc++;
483 if ((arg & 512) && !SvOK(sv)) {
484 arg = fieldsize;
485 while (arg--)
486 *t++ = ' ';
487 break;
488 }
489 gotsome = TRUE;
490 value = SvNV(sv);
bbce6d69 491 /* Formats aren't yet marked for locales, so assume "yes". */
36477c24 492 SET_NUMERIC_LOCAL();
a0d0e21e
LW
493 if (arg & 256) {
494 sprintf(t, "%#*.*f", (int) fieldsize, (int) arg & 255, value);
495 } else {
496 sprintf(t, "%*.0f", (int) fieldsize, value);
497 }
498 t += fieldsize;
499 break;
500
501 case FF_NEWLINE:
502 f++;
503 while (t-- > linemark && *t == ' ') ;
504 t++;
505 *t++ = '\n';
506 break;
507
508 case FF_BLANK:
509 arg = *fpc++;
510 if (gotsome) {
511 if (arg) { /* repeat until fields exhausted? */
512 *t = '\0';
3280af22
NIS
513 SvCUR_set(PL_formtarget, t - SvPVX(PL_formtarget));
514 lines += FmLINES(PL_formtarget);
a0d0e21e
LW
515 if (lines == 200) {
516 arg = t - linemark;
517 if (strnEQ(linemark, linemark - arg, arg))
518 DIE("Runaway format");
519 }
3280af22 520 FmLINES(PL_formtarget) = lines;
a0d0e21e
LW
521 SP = ORIGMARK;
522 RETURNOP(cLISTOP->op_first);
523 }
524 }
525 else {
526 t = linemark;
527 lines--;
528 }
529 break;
530
531 case FF_MORE:
9f208677
GB
532 s = chophere;
533 send = item + len;
534 if (chopspace) {
535 while (*s && isSPACE(*s) && s < send)
536 s++;
537 }
538 if (s < send) {
a0d0e21e
LW
539 arg = fieldsize - itemsize;
540 if (arg) {
541 fieldsize -= arg;
542 while (arg-- > 0)
543 *t++ = ' ';
544 }
545 s = t - 3;
546 if (strnEQ(s," ",3)) {
3280af22 547 while (s > SvPVX(PL_formtarget) && isSPACE(s[-1]))
a0d0e21e
LW
548 s--;
549 }
550 *s++ = '.';
551 *s++ = '.';
552 *s++ = '.';
553 }
554 break;
555
556 case FF_END:
557 *t = '\0';
3280af22
NIS
558 SvCUR_set(PL_formtarget, t - SvPVX(PL_formtarget));
559 FmLINES(PL_formtarget) += lines;
a0d0e21e
LW
560 SP = ORIGMARK;
561 RETPUSHYES;
562 }
563 }
564}
565
566PP(pp_grepstart)
567{
4e35701f 568 djSP;
a0d0e21e
LW
569 SV *src;
570
3280af22 571 if (PL_stack_base + *PL_markstack_ptr == SP) {
a0d0e21e 572 (void)POPMARK;
54310121 573 if (GIMME_V == G_SCALAR)
3280af22 574 XPUSHs(&PL_sv_no);
533c011a 575 RETURNOP(PL_op->op_next->op_next);
a0d0e21e 576 }
3280af22 577 PL_stack_sp = PL_stack_base + *PL_markstack_ptr + 1;
11343788
MB
578 pp_pushmark(ARGS); /* push dst */
579 pp_pushmark(ARGS); /* push src */
a0d0e21e
LW
580 ENTER; /* enter outer scope */
581
582 SAVETMPS;
fb54173c
MB
583#ifdef USE_THREADS
584 /* SAVE_DEFSV does *not* suffice here */
940cb80d 585 save_sptr(&THREADSV(0));
fb54173c 586#else
3280af22 587 SAVESPTR(GvSV(PL_defgv));
fb54173c 588#endif /* USE_THREADS */
a0d0e21e 589 ENTER; /* enter inner scope */
3280af22 590 SAVESPTR(PL_curpm);
a0d0e21e 591
3280af22 592 src = PL_stack_base[*PL_markstack_ptr];
a0d0e21e 593 SvTEMP_off(src);
54b9620d 594 DEFSV = src;
a0d0e21e
LW
595
596 PUTBACK;
533c011a 597 if (PL_op->op_type == OP_MAPSTART)
11343788 598 pp_pushmark(ARGS); /* push top */
533c011a 599 return ((LOGOP*)PL_op->op_next)->op_other;
a0d0e21e
LW
600}
601
602PP(pp_mapstart)
603{
604 DIE("panic: mapstart"); /* uses grepstart */
605}
606
607PP(pp_mapwhile)
608{
4e35701f 609 djSP;
3280af22 610 I32 diff = (SP - PL_stack_base) - *PL_markstack_ptr;
a0d0e21e
LW
611 I32 count;
612 I32 shift;
613 SV** src;
614 SV** dst;
615
3280af22 616 ++PL_markstack_ptr[-1];
a0d0e21e 617 if (diff) {
3280af22
NIS
618 if (diff > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) {
619 shift = diff - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]);
620 count = (SP - PL_stack_base) - PL_markstack_ptr[-1] + 2;
a0d0e21e 621
924508f0
GS
622 EXTEND(SP,shift);
623 src = SP;
624 dst = (SP += shift);
3280af22
NIS
625 PL_markstack_ptr[-1] += shift;
626 *PL_markstack_ptr += shift;
a0d0e21e
LW
627 while (--count)
628 *dst-- = *src--;
629 }
3280af22 630 dst = PL_stack_base + (PL_markstack_ptr[-2] += diff) - 1;
a0d0e21e
LW
631 ++diff;
632 while (--diff)
633 *dst-- = SvTEMP(TOPs) ? POPs : sv_mortalcopy(POPs);
634 }
635 LEAVE; /* exit inner scope */
636
637 /* All done yet? */
3280af22 638 if (PL_markstack_ptr[-1] > *PL_markstack_ptr) {
a0d0e21e 639 I32 items;
54310121 640 I32 gimme = GIMME_V;
a0d0e21e
LW
641
642 (void)POPMARK; /* pop top */
643 LEAVE; /* exit outer scope */
644 (void)POPMARK; /* pop src */
3280af22 645 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
a0d0e21e 646 (void)POPMARK; /* pop dst */
3280af22 647 SP = PL_stack_base + POPMARK; /* pop original mark */
54310121 648 if (gimme == G_SCALAR) {
a0d0e21e
LW
649 dTARGET;
650 XPUSHi(items);
a0d0e21e 651 }
54310121 652 else if (gimme == G_ARRAY)
653 SP += items;
a0d0e21e
LW
654 RETURN;
655 }
656 else {
657 SV *src;
658
659 ENTER; /* enter inner scope */
3280af22 660 SAVESPTR(PL_curpm);
a0d0e21e 661
3280af22 662 src = PL_stack_base[PL_markstack_ptr[-1]];
a0d0e21e 663 SvTEMP_off(src);
54b9620d 664 DEFSV = src;
a0d0e21e
LW
665
666 RETURNOP(cLOGOP->op_other);
667 }
668}
669
0f208e93
IZ
670#define tryCALL_AMAGICbin(left,right,meth,svp) STMT_START { \
671 *svp = Nullsv; \
672 if (PL_amagic_generation) { \
673 if (SvAMAGIC(left)||SvAMAGIC(right))\
674 *svp = amagic_call(left, \
675 right, \
676 CAT2(meth,_amg), \
677 0); \
678 } \
679 } STMT_END
680
681STATIC I32
682amagic_cmp(register SV *str1, register SV *str2)
683{
684 SV *tmpsv;
685 tryCALL_AMAGICbin(str1,str2,scmp,&tmpsv);
686 if (tmpsv) {
687 double d;
688
689 if (SvIOK(tmpsv)) {
690 I32 i = SvIVX(tmpsv);
691 if (i > 0)
692 return 1;
693 return i? -1 : 0;
694 }
695 d = SvNV(tmpsv);
696 if (d > 0)
697 return 1;
698 return d? -1 : 0;
699 }
700 return sv_cmp(str1, str2);
701}
702
703STATIC I32
704amagic_cmp_locale(register SV *str1, register SV *str2)
705{
706 SV *tmpsv;
707 tryCALL_AMAGICbin(str1,str2,scmp,&tmpsv);
708 if (tmpsv) {
709 double d;
710
711 if (SvIOK(tmpsv)) {
712 I32 i = SvIVX(tmpsv);
713 if (i > 0)
714 return 1;
715 return i? -1 : 0;
716 }
717 d = SvNV(tmpsv);
718 if (d > 0)
719 return 1;
720 return d? -1 : 0;
721 }
722 return sv_cmp_locale(str1, str2);
723}
724
a0d0e21e
LW
725PP(pp_sort)
726{
4e35701f 727 djSP; dMARK; dORIGMARK;
a0d0e21e
LW
728 register SV **up;
729 SV **myorigmark = ORIGMARK;
730 register I32 max;
731 HV *stash;
732 GV *gv;
733 CV *cv;
734 I32 gimme = GIMME;
533c011a 735 OP* nextop = PL_op->op_next;
0f208e93 736 I32 overloading = 0;
a0d0e21e
LW
737
738 if (gimme != G_ARRAY) {
739 SP = MARK;
740 RETPUSHUNDEF;
741 }
742
d0abe6c5 743 ENTER;
3280af22 744 SAVEPPTR(PL_sortcop);
533c011a
NIS
745 if (PL_op->op_flags & OPf_STACKED) {
746 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e
LW
747 OP *kid = cLISTOP->op_first->op_sibling; /* pass pushmark */
748 kid = kUNOP->op_first; /* pass rv2gv */
749 kid = kUNOP->op_first; /* pass leave */
3280af22
NIS
750 PL_sortcop = kid->op_next;
751 stash = PL_curcop->cop_stash;
a0d0e21e
LW
752 }
753 else {
754 cv = sv_2cv(*++MARK, &stash, &gv, 0);
755 if (!(cv && CvROOT(cv))) {
756 if (gv) {
757 SV *tmpstr = sv_newmortal();
e5cf08de 758 gv_efullname3(tmpstr, gv, Nullch);
a0d0e21e
LW
759 if (cv && CvXSUB(cv))
760 DIE("Xsub \"%s\" called in sort", SvPVX(tmpstr));
761 DIE("Undefined sort subroutine \"%s\" called",
762 SvPVX(tmpstr));
763 }
764 if (cv) {
765 if (CvXSUB(cv))
766 DIE("Xsub called in sort");
767 DIE("Undefined subroutine in sort");
768 }
769 DIE("Not a CODE reference in sort");
770 }
3280af22 771 PL_sortcop = CvSTART(cv);
a0d0e21e
LW
772 SAVESPTR(CvROOT(cv)->op_ppaddr);
773 CvROOT(cv)->op_ppaddr = ppaddr[OP_NULL];
b3933176 774
3280af22
NIS
775 SAVESPTR(PL_curpad);
776 PL_curpad = AvARRAY((AV*)AvARRAY(CvPADLIST(cv))[1]);
a0d0e21e
LW
777 }
778 }
779 else {
3280af22
NIS
780 PL_sortcop = Nullop;
781 stash = PL_curcop->cop_stash;
a0d0e21e
LW
782 }
783
784 up = myorigmark + 1;
785 while (MARK < SP) { /* This may or may not shift down one here. */
786 /*SUPPRESS 560*/
787 if (*up = *++MARK) { /* Weed out nulls. */
9f8d30d5 788 SvTEMP_off(*up);
0f208e93
IZ
789 if (!PL_sortcop && !SvPOK(*up)) {
790 if (SvAMAGIC(*up))
791 overloading = 1;
36596149
GB
792 else {
793 STRLEN n_a;
794 (void)sv_2pv(*up, &n_a);
795 }
0f208e93 796 }
a0d0e21e
LW
797 up++;
798 }
799 }
800 max = --up - myorigmark;
3280af22 801 if (PL_sortcop) {
a0d0e21e 802 if (max > 1) {
c09156bb 803 PERL_CONTEXT *cx;
a0d0e21e 804 SV** newsp;
54310121 805 bool oldcatch = CATCH_GET;
a0d0e21e
LW
806
807 SAVETMPS;
462e5cf6 808 SAVEOP();
a0d0e21e 809
54310121 810 CATCH_SET(TRUE);
e788e7d3 811 PUSHSTACKi(PERLSI_SORT);
3280af22
NIS
812 if (PL_sortstash != stash) {
813 PL_firstgv = gv_fetchpv("a", TRUE, SVt_PV);
814 PL_secondgv = gv_fetchpv("b", TRUE, SVt_PV);
815 PL_sortstash = stash;
a0d0e21e
LW
816 }
817
3280af22
NIS
818 SAVESPTR(GvSV(PL_firstgv));
819 SAVESPTR(GvSV(PL_secondgv));
b3933176 820
3280af22 821 PUSHBLOCK(cx, CXt_NULL, PL_stack_base);
533c011a 822 if (!(PL_op->op_flags & OPf_SPECIAL)) {
b3933176
CS
823 bool hasargs = FALSE;
824 cx->cx_type = CXt_SUB;
825 cx->blk_gimme = G_SCALAR;
826 PUSHSUB(cx);
827 if (!CvDEPTH(cv))
3e3baf6d 828 (void)SvREFCNT_inc(cv); /* in preparation for POPSUB */
b3933176 829 }
3280af22 830 PL_sortcxix = cxstack_ix;
ac4c12e7 831 qsortsv((myorigmark+1), max, FUNC_NAME_TO_PTR(sortcv));
a0d0e21e 832
3280af22 833 POPBLOCK(cx,PL_curpm);
d3acc0f7 834 POPSTACK;
54310121 835 CATCH_SET(oldcatch);
a0d0e21e 836 }
a0d0e21e
LW
837 }
838 else {
839 if (max > 1) {
840 MEXTEND(SP, 20); /* Can't afford stack realloc on signal. */
745d3a65 841 qsortsv(ORIGMARK+1, max,
533c011a 842 (PL_op->op_private & OPpLOCALE)
0f208e93
IZ
843 ? ( overloading
844 ? FUNC_NAME_TO_PTR(amagic_cmp_locale)
845 : FUNC_NAME_TO_PTR(sv_cmp_locale))
846 : ( overloading
847 ? FUNC_NAME_TO_PTR(amagic_cmp)
848 : FUNC_NAME_TO_PTR(sv_cmp) ));
a0d0e21e
LW
849 }
850 }
d0abe6c5 851 LEAVE;
3280af22 852 PL_stack_sp = ORIGMARK + max;
a0d0e21e
LW
853 return nextop;
854}
855
856/* Range stuff. */
857
858PP(pp_range)
859{
860 if (GIMME == G_ARRAY)
861 return cCONDOP->op_true;
533c011a 862 return SvTRUEx(PAD_SV(PL_op->op_targ)) ? cCONDOP->op_false : cCONDOP->op_true;
a0d0e21e
LW
863}
864
865PP(pp_flip)
866{
4e35701f 867 djSP;
a0d0e21e
LW
868
869 if (GIMME == G_ARRAY) {
870 RETURNOP(((CONDOP*)cUNOP->op_first)->op_false);
871 }
872 else {
873 dTOPss;
533c011a 874 SV *targ = PAD_SV(PL_op->op_targ);
a0d0e21e 875
533c011a 876 if ((PL_op->op_private & OPpFLIP_LINENUM)
3280af22 877 ? (PL_last_in_gv && SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv)))
a0d0e21e
LW
878 : SvTRUE(sv) ) {
879 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
533c011a 880 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e 881 sv_setiv(targ, 1);
3e3baf6d 882 SETs(targ);
a0d0e21e
LW
883 RETURN;
884 }
885 else {
886 sv_setiv(targ, 0);
924508f0 887 SP--;
a0d0e21e
LW
888 RETURNOP(((CONDOP*)cUNOP->op_first)->op_false);
889 }
890 }
891 sv_setpv(TARG, "");
892 SETs(targ);
893 RETURN;
894 }
895}
896
897PP(pp_flop)
898{
4e35701f 899 djSP;
a0d0e21e
LW
900
901 if (GIMME == G_ARRAY) {
902 dPOPPOPssrl;
aa04a7cf 903 register I32 i, j;
a0d0e21e
LW
904 register SV *sv;
905 I32 max;
906
4633a7c4 907 if (SvNIOKp(left) || !SvPOKp(left) ||
bbce6d69 908 (looks_like_number(left) && *SvPVX(left) != '0') )
909 {
aa04a7cf 910 if (SvNV(left) < IV_MIN || SvNV(right) > IV_MAX)
89ea2908 911 croak("Range iterator outside integer range");
a0d0e21e
LW
912 i = SvIV(left);
913 max = SvIV(right);
bbce6d69 914 if (max >= i) {
aa04a7cf
GS
915 j = max - i + 1;
916 EXTEND_MORTAL(j);
917 EXTEND(SP, j);
bbce6d69 918 }
aa04a7cf
GS
919 else
920 j = 0;
921 while (j--) {
bbce6d69 922 sv = sv_2mortal(newSViv(i++));
a0d0e21e
LW
923 PUSHs(sv);
924 }
925 }
926 else {
927 SV *final = sv_mortalcopy(right);
928 STRLEN len;
36596149 929 STRLEN n_a;
a0d0e21e
LW
930 char *tmps = SvPV(final, len);
931
932 sv = sv_mortalcopy(left);
36596149 933 SvPV_force(sv,n_a);
89ea2908 934 while (!SvNIOKp(sv) && SvCUR(sv) <= len) {
a0d0e21e 935 XPUSHs(sv);
89ea2908
GA
936 if (strEQ(SvPVX(sv),tmps))
937 break;
a0d0e21e
LW
938 sv = sv_2mortal(newSVsv(sv));
939 sv_inc(sv);
940 }
a0d0e21e
LW
941 }
942 }
943 else {
944 dTOPss;
945 SV *targ = PAD_SV(cUNOP->op_first->op_targ);
946 sv_inc(targ);
533c011a 947 if ((PL_op->op_private & OPpFLIP_LINENUM)
3280af22 948 ? (PL_last_in_gv && SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv)))
a0d0e21e
LW
949 : SvTRUE(sv) ) {
950 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
951 sv_catpv(targ, "E0");
952 }
953 SETs(targ);
954 }
955
956 RETURN;
957}
958
959/* Control. */
960
76e3520e 961STATIC I32
8ac85365 962dopoptolabel(char *label)
a0d0e21e 963{
11343788 964 dTHR;
a0d0e21e 965 register I32 i;
c09156bb 966 register PERL_CONTEXT *cx;
a0d0e21e
LW
967
968 for (i = cxstack_ix; i >= 0; i--) {
969 cx = &cxstack[i];
4e43f39e 970 switch (CxTYPE(cx)) {
a0d0e21e 971 case CXt_SUBST:
3280af22 972 if (PL_dowarn)
533c011a 973 warn("Exiting substitution via %s", op_name[PL_op->op_type]);
a0d0e21e
LW
974 break;
975 case CXt_SUB:
3280af22 976 if (PL_dowarn)
533c011a 977 warn("Exiting subroutine via %s", op_name[PL_op->op_type]);
a0d0e21e
LW
978 break;
979 case CXt_EVAL:
3280af22 980 if (PL_dowarn)
533c011a 981 warn("Exiting eval via %s", op_name[PL_op->op_type]);
a0d0e21e 982 break;
0a753a76 983 case CXt_NULL:
3280af22 984 if (PL_dowarn)
533c011a 985 warn("Exiting pseudo-block via %s", op_name[PL_op->op_type]);
0a753a76 986 return -1;
a0d0e21e
LW
987 case CXt_LOOP:
988 if (!cx->blk_loop.label ||
989 strNE(label, cx->blk_loop.label) ) {
68dc0745 990 DEBUG_l(deb("(Skipping label #%ld %s)\n",
991 (long)i, cx->blk_loop.label));
a0d0e21e
LW
992 continue;
993 }
68dc0745 994 DEBUG_l( deb("(Found label #%ld %s)\n", (long)i, label));
a0d0e21e
LW
995 return i;
996 }
997 }
998 return i;
999}
1000
e50aee73 1001I32
8ac85365 1002dowantarray(void)
e50aee73 1003{
54310121 1004 I32 gimme = block_gimme();
1005 return (gimme == G_VOID) ? G_SCALAR : gimme;
1006}
1007
1008I32
8ac85365 1009block_gimme(void)
54310121 1010{
11343788 1011 dTHR;
e50aee73
AD
1012 I32 cxix;
1013
1014 cxix = dopoptosub(cxstack_ix);
1015 if (cxix < 0)
46fc3d4c 1016 return G_VOID;
e50aee73 1017
54310121 1018 switch (cxstack[cxix].blk_gimme) {
d2719217
GS
1019 case G_VOID:
1020 return G_VOID;
54310121 1021 case G_SCALAR:
e50aee73 1022 return G_SCALAR;
54310121 1023 case G_ARRAY:
1024 return G_ARRAY;
1025 default:
1026 croak("panic: bad gimme: %d\n", cxstack[cxix].blk_gimme);
d2719217
GS
1027 /* NOTREACHED */
1028 return 0;
54310121 1029 }
e50aee73
AD
1030}
1031
76e3520e 1032STATIC I32
8ac85365 1033dopoptosub(I32 startingblock)
a0d0e21e 1034{
11343788 1035 dTHR;
2c375eb9
GS
1036 return dopoptosub_at(cxstack, startingblock);
1037}
1038
1039STATIC I32
1040dopoptosub_at(PERL_CONTEXT *cxstk, I32 startingblock)
1041{
1042 dTHR;
a0d0e21e 1043 I32 i;
c09156bb 1044 register PERL_CONTEXT *cx;
a0d0e21e 1045 for (i = startingblock; i >= 0; i--) {
2c375eb9 1046 cx = &cxstk[i];
4e43f39e 1047 switch (CxTYPE(cx)) {
a0d0e21e
LW
1048 default:
1049 continue;
1050 case CXt_EVAL:
1051 case CXt_SUB:
68dc0745 1052 DEBUG_l( deb("(Found sub #%ld)\n", (long)i));
a0d0e21e
LW
1053 return i;
1054 }
1055 }
1056 return i;
1057}
1058
76e3520e 1059STATIC I32
8ac85365 1060dopoptoeval(I32 startingblock)
a0d0e21e 1061{
11343788 1062 dTHR;
a0d0e21e 1063 I32 i;
c09156bb 1064 register PERL_CONTEXT *cx;
a0d0e21e
LW
1065 for (i = startingblock; i >= 0; i--) {
1066 cx = &cxstack[i];
4e43f39e 1067 switch (CxTYPE(cx)) {
a0d0e21e
LW
1068 default:
1069 continue;
1070 case CXt_EVAL:
68dc0745 1071 DEBUG_l( deb("(Found eval #%ld)\n", (long)i));
a0d0e21e
LW
1072 return i;
1073 }
1074 }
1075 return i;
1076}
1077
76e3520e 1078STATIC I32
8ac85365 1079dopoptoloop(I32 startingblock)
a0d0e21e 1080{
11343788 1081 dTHR;
a0d0e21e 1082 I32 i;
c09156bb 1083 register PERL_CONTEXT *cx;
a0d0e21e
LW
1084 for (i = startingblock; i >= 0; i--) {
1085 cx = &cxstack[i];
4e43f39e 1086 switch (CxTYPE(cx)) {
a0d0e21e 1087 case CXt_SUBST:
3280af22 1088 if (PL_dowarn)
533c011a 1089 warn("Exiting substitution via %s", op_name[PL_op->op_type]);
a0d0e21e
LW
1090 break;
1091 case CXt_SUB:
3280af22 1092 if (PL_dowarn)
533c011a 1093 warn("Exiting subroutine via %s", op_name[PL_op->op_type]);
a0d0e21e
LW
1094 break;
1095 case CXt_EVAL:
3280af22 1096 if (PL_dowarn)
533c011a 1097 warn("Exiting eval via %s", op_name[PL_op->op_type]);
a0d0e21e 1098 break;
0a753a76 1099 case CXt_NULL:
3280af22 1100 if (PL_dowarn)
533c011a 1101 warn("Exiting pseudo-block via %s", op_name[PL_op->op_type]);
0a753a76 1102 return -1;
a0d0e21e 1103 case CXt_LOOP:
68dc0745 1104 DEBUG_l( deb("(Found loop #%ld)\n", (long)i));
a0d0e21e
LW
1105 return i;
1106 }
1107 }
1108 return i;
1109}
1110
1111void
8ac85365 1112dounwind(I32 cxix)
a0d0e21e 1113{
11343788 1114 dTHR;
c09156bb 1115 register PERL_CONTEXT *cx;
a0d0e21e
LW
1116 SV **newsp;
1117 I32 optype;
1118
1119 while (cxstack_ix > cxix) {
c90c0ff4 1120 cx = &cxstack[cxstack_ix];
1121 DEBUG_l(PerlIO_printf(Perl_debug_log, "Unwinding block %ld, type %s\n",
4e43f39e 1122 (long) cxstack_ix, block_type[CxTYPE(cx)]));
a0d0e21e 1123 /* Note: we don't need to restore the base context info till the end. */
4e43f39e 1124 switch (CxTYPE(cx)) {
c90c0ff4 1125 case CXt_SUBST:
1126 POPSUBST(cx);
1127 continue; /* not break */
a0d0e21e
LW
1128 case CXt_SUB:
1129 POPSUB(cx);
1130 break;
1131 case CXt_EVAL:
1132 POPEVAL(cx);
1133 break;
1134 case CXt_LOOP:
1135 POPLOOP(cx);
1136 break;
0a753a76 1137 case CXt_NULL:
a0d0e21e
LW
1138 break;
1139 }
c90c0ff4 1140 cxstack_ix--;
a0d0e21e
LW
1141 }
1142}
1143
a0d0e21e 1144OP *
8ac85365 1145die_where(char *message)
a0d0e21e 1146{
e336de0d 1147 dSP;
36596149 1148 STRLEN n_a;
3280af22 1149 if (PL_in_eval) {
a0d0e21e 1150 I32 cxix;
c09156bb 1151 register PERL_CONTEXT *cx;
a0d0e21e
LW
1152 I32 gimme;
1153 SV **newsp;
1154
4e6ea2c3 1155 if (message) {
3280af22 1156 if (PL_in_eval & 4) {
4e6ea2c3
GS
1157 SV **svp;
1158 STRLEN klen = strlen(message);
1159
1160 svp = hv_fetch(ERRHV, message, klen, TRUE);
1161 if (svp) {
1162 if (!SvIOK(*svp)) {
1163 static char prefix[] = "\t(in cleanup) ";
1164 SV *err = ERRSV;
1165 sv_upgrade(*svp, SVt_IV);
1166 (void)SvIOK_only(*svp);
1167 if (!SvPOK(err))
1168 sv_setpv(err,"");
1169 SvGROW(err, SvCUR(err)+sizeof(prefix)+klen);
1170 sv_catpvn(err, prefix, sizeof(prefix)-1);
1171 sv_catpvn(err, message, klen);
1172 }
1173 sv_inc(*svp);
4633a7c4 1174 }
4633a7c4 1175 }
4e6ea2c3
GS
1176 else
1177 sv_setpv(ERRSV, message);
4633a7c4
LW
1178 }
1179 else
36596149 1180 message = SvPVx(ERRSV, n_a);
4e6ea2c3 1181
3280af22 1182 while ((cxix = dopoptoeval(cxstack_ix)) < 0 && PL_curstackinfo->si_prev) {
bac4b2ad 1183 dounwind(-1);
d3acc0f7 1184 POPSTACK;
bac4b2ad 1185 }
e336de0d 1186
a0d0e21e
LW
1187 if (cxix >= 0) {
1188 I32 optype;
1189
1190 if (cxix < cxstack_ix)
1191 dounwind(cxix);
1192
3280af22 1193 POPBLOCK(cx,PL_curpm);
4e43f39e 1194 if (CxTYPE(cx) != CXt_EVAL) {
760ac839 1195 PerlIO_printf(PerlIO_stderr(), "panic: die %s", message);
a0d0e21e
LW
1196 my_exit(1);
1197 }
1198 POPEVAL(cx);
1199
1200 if (gimme == G_SCALAR)
3280af22
NIS
1201 *++newsp = &PL_sv_undef;
1202 PL_stack_sp = newsp;
a0d0e21e
LW
1203
1204 LEAVE;
748a9306 1205
7a2e2cd6 1206 if (optype == OP_REQUIRE) {
36596149 1207 char* msg = SvPVx(ERRSV, n_a);
7a2e2cd6 1208 DIE("%s", *msg ? msg : "Compilation failed in require");
1209 }
a0d0e21e
LW
1210 return pop_return();
1211 }
1212 }
8e399e6d 1213 if(!message)
36596149 1214 message = SvPVx(ERRSV, n_a);
760ac839
LW
1215 PerlIO_printf(PerlIO_stderr(), "%s",message);
1216 PerlIO_flush(PerlIO_stderr());
f86702cc 1217 my_failure_exit();
1218 /* NOTREACHED */
a0d0e21e
LW
1219 return 0;
1220}
1221
1222PP(pp_xor)
1223{
4e35701f 1224 djSP; dPOPTOPssrl;
a0d0e21e
LW
1225 if (SvTRUE(left) != SvTRUE(right))
1226 RETSETYES;
1227 else
1228 RETSETNO;
1229}
1230
1231PP(pp_andassign)
1232{
4e35701f 1233 djSP;
a0d0e21e
LW
1234 if (!SvTRUE(TOPs))
1235 RETURN;
1236 else
1237 RETURNOP(cLOGOP->op_other);
1238}
1239
1240PP(pp_orassign)
1241{
4e35701f 1242 djSP;
a0d0e21e
LW
1243 if (SvTRUE(TOPs))
1244 RETURN;
1245 else
1246 RETURNOP(cLOGOP->op_other);
1247}
1248
a0d0e21e
LW
1249PP(pp_caller)
1250{
4e35701f 1251 djSP;
a0d0e21e 1252 register I32 cxix = dopoptosub(cxstack_ix);
c09156bb 1253 register PERL_CONTEXT *cx;
2c375eb9 1254 register PERL_CONTEXT *ccstack = cxstack;
3280af22 1255 PERL_SI *top_si = PL_curstackinfo;
a0d0e21e 1256 I32 dbcxix;
54310121 1257 I32 gimme;
49d8d3a1 1258 HV *hv;
a0d0e21e
LW
1259 SV *sv;
1260 I32 count = 0;
1261
1262 if (MAXARG)
1263 count = POPi;
1264 EXTEND(SP, 6);
1265 for (;;) {
2c375eb9
GS
1266 /* we may be in a higher stacklevel, so dig down deeper */
1267 while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
1268 top_si = top_si->si_prev;
1269 ccstack = top_si->si_cxstack;
1270 cxix = dopoptosub_at(ccstack, top_si->si_cxix);
1271 }
a0d0e21e
LW
1272 if (cxix < 0) {
1273 if (GIMME != G_ARRAY)
1274 RETPUSHUNDEF;
1275 RETURN;
1276 }
3280af22
NIS
1277 if (PL_DBsub && cxix >= 0 &&
1278 ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
a0d0e21e
LW
1279 count++;
1280 if (!count--)
1281 break;
2c375eb9 1282 cxix = dopoptosub_at(ccstack, cxix - 1);
a0d0e21e 1283 }
2c375eb9
GS
1284
1285 cx = &ccstack[cxix];
4e43f39e 1286 if (CxTYPE(cx) == CXt_SUB) {
2c375eb9
GS
1287 dbcxix = dopoptosub_at(ccstack, cxix - 1);
1288 /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the
06a5b730 1289 field below is defined for any cx. */
3280af22 1290 if (PL_DBsub && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub))
2c375eb9 1291 cx = &ccstack[dbcxix];
06a5b730 1292 }
1293
a0d0e21e 1294 if (GIMME != G_ARRAY) {
49d8d3a1
MB
1295 hv = cx->blk_oldcop->cop_stash;
1296 if (!hv)
3280af22 1297 PUSHs(&PL_sv_undef);
49d8d3a1
MB
1298 else {
1299 dTARGET;
1300 sv_setpv(TARG, HvNAME(hv));
1301 PUSHs(TARG);
1302 }
a0d0e21e
LW
1303 RETURN;
1304 }
a0d0e21e 1305
49d8d3a1
MB
1306 hv = cx->blk_oldcop->cop_stash;
1307 if (!hv)
3280af22 1308 PUSHs(&PL_sv_undef);
49d8d3a1
MB
1309 else
1310 PUSHs(sv_2mortal(newSVpv(HvNAME(hv), 0)));
a0d0e21e
LW
1311 PUSHs(sv_2mortal(newSVpv(SvPVX(GvSV(cx->blk_oldcop->cop_filegv)), 0)));
1312 PUSHs(sv_2mortal(newSViv((I32)cx->blk_oldcop->cop_line)));
1313 if (!MAXARG)
1314 RETURN;
4e43f39e 1315 if (CxTYPE(cx) == CXt_SUB) { /* So is ccstack[dbcxix]. */
a0d0e21e 1316 sv = NEWSV(49, 0);
2c375eb9 1317 gv_efullname3(sv, CvGV(ccstack[cxix].blk_sub.cv), Nullch);
a0d0e21e
LW
1318 PUSHs(sv_2mortal(sv));
1319 PUSHs(sv_2mortal(newSViv((I32)cx->blk_sub.hasargs)));
1320 }
1321 else {
1322 PUSHs(sv_2mortal(newSVpv("(eval)",0)));
1323 PUSHs(sv_2mortal(newSViv(0)));
1324 }
54310121 1325 gimme = (I32)cx->blk_gimme;
1326 if (gimme == G_VOID)
3280af22 1327 PUSHs(&PL_sv_undef);
54310121 1328 else
1329 PUSHs(sv_2mortal(newSViv(gimme & G_ARRAY)));
4e43f39e 1330 if (CxTYPE(cx) == CXt_EVAL) {
06a5b730 1331 if (cx->blk_eval.old_op_type == OP_ENTEREVAL) {
4633a7c4 1332 PUSHs(cx->blk_eval.cur_text);
3280af22 1333 PUSHs(&PL_sv_no);
06a5b730 1334 }
1335 else if (cx->blk_eval.old_name) { /* Try blocks have old_name == 0. */
1336 /* Require, put the name. */
1337 PUSHs(sv_2mortal(newSVpv(cx->blk_eval.old_name, 0)));
3280af22 1338 PUSHs(&PL_sv_yes);
06a5b730 1339 }
4633a7c4 1340 }
4e43f39e 1341 else if (CxTYPE(cx) == CXt_SUB &&
4633a7c4 1342 cx->blk_sub.hasargs &&
3280af22 1343 PL_curcop->cop_stash == PL_debstash)
4633a7c4 1344 {
a0d0e21e
LW
1345 AV *ary = cx->blk_sub.argarray;
1346 int off = AvARRAY(ary) - AvALLOC(ary);
1347
3280af22 1348 if (!PL_dbargs) {
a0d0e21e 1349 GV* tmpgv;
3280af22 1350 PL_dbargs = GvAV(gv_AVadd(tmpgv = gv_fetchpv("DB::args", TRUE,
a0d0e21e 1351 SVt_PVAV)));
a5f75d66 1352 GvMULTI_on(tmpgv);
3280af22 1353 AvREAL_off(PL_dbargs); /* XXX Should be REIFY */
a0d0e21e
LW
1354 }
1355
3280af22
NIS
1356 if (AvMAX(PL_dbargs) < AvFILLp(ary) + off)
1357 av_extend(PL_dbargs, AvFILLp(ary) + off);
1358 Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*);
1359 AvFILLp(PL_dbargs) = AvFILLp(ary) + off;
a0d0e21e
LW
1360 }
1361 RETURN;
1362}
1363
6b6eec5b 1364STATIC I32
745d3a65 1365sortcv(SV *a, SV *b)
a0d0e21e 1366{
11343788 1367 dTHR;
3280af22
NIS
1368 I32 oldsaveix = PL_savestack_ix;
1369 I32 oldscopeix = PL_scopestack_ix;
a0d0e21e 1370 I32 result;
3280af22
NIS
1371 GvSV(PL_firstgv) = a;
1372 GvSV(PL_secondgv) = b;
1373 PL_stack_sp = PL_stack_base;
533c011a 1374 PL_op = PL_sortcop;
76e3520e 1375 CALLRUNOPS();
3280af22 1376 if (PL_stack_sp != PL_stack_base + 1)
a0d0e21e 1377 croak("Sort subroutine didn't return single value");
3280af22 1378 if (!SvNIOKp(*PL_stack_sp))
a0d0e21e 1379 croak("Sort subroutine didn't return a numeric value");
3280af22
NIS
1380 result = SvIV(*PL_stack_sp);
1381 while (PL_scopestack_ix > oldscopeix) {
a0d0e21e
LW
1382 LEAVE;
1383 }
748a9306 1384 leave_scope(oldsaveix);
a0d0e21e
LW
1385 return result;
1386}
1387
a0d0e21e
LW
1388PP(pp_reset)
1389{
4e35701f 1390 djSP;
a0d0e21e 1391 char *tmps;
36596149 1392 STRLEN n_a;
a0d0e21e
LW
1393
1394 if (MAXARG < 1)
1395 tmps = "";
1396 else
36596149 1397 tmps = POPpx;
3280af22
NIS
1398 sv_reset(tmps, PL_curcop->cop_stash);
1399 PUSHs(&PL_sv_yes);
a0d0e21e
LW
1400 RETURN;
1401}
1402
1403PP(pp_lineseq)
1404{
1405 return NORMAL;
1406}
1407
1408PP(pp_dbstate)
1409{
533c011a 1410 PL_curcop = (COP*)PL_op;
a0d0e21e 1411 TAINT_NOT; /* Each statement is presumed innocent */
3280af22 1412 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
a0d0e21e
LW
1413 FREETMPS;
1414
533c011a 1415 if (PL_op->op_private || SvIV(PL_DBsingle) || SvIV(PL_DBsignal) || SvIV(PL_DBtrace))
a0d0e21e 1416 {
924508f0 1417 djSP;
a0d0e21e 1418 register CV *cv;
c09156bb 1419 register PERL_CONTEXT *cx;
748a9306 1420 I32 gimme = G_ARRAY;
a0d0e21e
LW
1421 I32 hasargs;
1422 GV *gv;
1423
3280af22 1424 gv = PL_DBgv;
a0d0e21e 1425 cv = GvCV(gv);
a0d0e21e
LW
1426 if (!cv)
1427 DIE("No DB::DB routine defined");
1428
3280af22 1429 if (CvDEPTH(cv) >= 1 && !(PL_debug & (1<<30))) /* don't do recursive DB::DB call */
a0d0e21e 1430 return NORMAL;
748a9306 1431
4633a7c4
LW
1432 ENTER;
1433 SAVETMPS;
1434
3280af22 1435 SAVEI32(PL_debug);
55497cff 1436 SAVESTACK_POS();
3280af22 1437 PL_debug = 0;
748a9306 1438 hasargs = 0;
924508f0 1439 SPAGAIN;
748a9306 1440
533c011a 1441 push_return(PL_op->op_next);
924508f0 1442 PUSHBLOCK(cx, CXt_SUB, SP);
a0d0e21e
LW
1443 PUSHSUB(cx);
1444 CvDEPTH(cv)++;
1445 (void)SvREFCNT_inc(cv);
3280af22
NIS
1446 SAVESPTR(PL_curpad);
1447 PL_curpad = AvARRAY((AV*)*av_fetch(CvPADLIST(cv),1,FALSE));
a0d0e21e
LW
1448 RETURNOP(CvSTART(cv));
1449 }
1450 else
1451 return NORMAL;
1452}
1453
1454PP(pp_scope)
1455{
1456 return NORMAL;
1457}
1458
1459PP(pp_enteriter)
1460{
4e35701f 1461 djSP; dMARK;
c09156bb 1462 register PERL_CONTEXT *cx;
54310121 1463 I32 gimme = GIMME_V;
a0d0e21e
LW
1464 SV **svp;
1465
4633a7c4
LW
1466 ENTER;
1467 SAVETMPS;
1468
54b9620d 1469#ifdef USE_THREADS
8bed047e
GS
1470 if (PL_op->op_flags & OPf_SPECIAL) {
1471 dTHR;
1472 svp = &THREADSV(PL_op->op_targ); /* per-thread variable */
1473 SAVEGENERICSV(*svp);
1474 *svp = NEWSV(0,0);
1475 }
a0d0e21e 1476 else
54b9620d 1477#endif /* USE_THREADS */
533c011a
NIS
1478 if (PL_op->op_targ) {
1479 svp = &PL_curpad[PL_op->op_targ]; /* "my" variable */
54b9620d
MB
1480 SAVESPTR(*svp);
1481 }
1482 else {
8bed047e
GS
1483 svp = &GvSV((GV*)POPs); /* symbol table variable */
1484 SAVEGENERICSV(*svp);
1485 *svp = NEWSV(0,0);
54b9620d 1486 }
4633a7c4 1487
a0d0e21e
LW
1488 ENTER;
1489
1490 PUSHBLOCK(cx, CXt_LOOP, SP);
1491 PUSHLOOP(cx, svp, MARK);
533c011a 1492 if (PL_op->op_flags & OPf_STACKED) {
44a8e56a 1493 cx->blk_loop.iterary = (AV*)SvREFCNT_inc(POPs);
89ea2908
GA
1494 if (SvTYPE(cx->blk_loop.iterary) != SVt_PVAV) {
1495 dPOPss;
1496 if (SvNIOKp(sv) || !SvPOKp(sv) ||
1497 (looks_like_number(sv) && *SvPVX(sv) != '0')) {
1498 if (SvNV(sv) < IV_MIN ||
1499 SvNV((SV*)cx->blk_loop.iterary) >= IV_MAX)
1500 croak("Range iterator outside integer range");
1501 cx->blk_loop.iterix = SvIV(sv);
1502 cx->blk_loop.itermax = SvIV((SV*)cx->blk_loop.iterary);
1503 }
1504 else
1505 cx->blk_loop.iterlval = newSVsv(sv);
1506 }
1507 }
4633a7c4 1508 else {
3280af22
NIS
1509 cx->blk_loop.iterary = PL_curstack;
1510 AvFILLp(PL_curstack) = SP - PL_stack_base;
1511 cx->blk_loop.iterix = MARK - PL_stack_base;
4633a7c4 1512 }
a0d0e21e
LW
1513
1514 RETURN;
1515}
1516
1517PP(pp_enterloop)
1518{
4e35701f 1519 djSP;
c09156bb 1520 register PERL_CONTEXT *cx;
54310121 1521 I32 gimme = GIMME_V;
a0d0e21e
LW
1522
1523 ENTER;
1524 SAVETMPS;
1525 ENTER;
1526
1527 PUSHBLOCK(cx, CXt_LOOP, SP);
1528 PUSHLOOP(cx, 0, SP);
1529
1530 RETURN;
1531}
1532
1533PP(pp_leaveloop)
1534{
4e35701f 1535 djSP;
c09156bb 1536 register PERL_CONTEXT *cx;
f86702cc 1537 struct block_loop cxloop;
a0d0e21e
LW
1538 I32 gimme;
1539 SV **newsp;
1540 PMOP *newpm;
1541 SV **mark;
1542
1543 POPBLOCK(cx,newpm);
4fdae800 1544 mark = newsp;
f86702cc 1545 POPLOOP1(cx); /* Delay POPLOOP2 until stack values are safe */
1546
a1f49e72 1547 TAINT_NOT;
54310121 1548 if (gimme == G_VOID)
1549 ; /* do nothing */
1550 else if (gimme == G_SCALAR) {
1551 if (mark < SP)
1552 *++newsp = sv_mortalcopy(*SP);
1553 else
3280af22 1554 *++newsp = &PL_sv_undef;
a0d0e21e
LW
1555 }
1556 else {
a1f49e72 1557 while (mark < SP) {
a0d0e21e 1558 *++newsp = sv_mortalcopy(*++mark);
a1f49e72
CS
1559 TAINT_NOT; /* Each item is independent */
1560 }
a0d0e21e 1561 }
f86702cc 1562 SP = newsp;
1563 PUTBACK;
1564
1565 POPLOOP2(); /* Stack values are safe: release loop vars ... */
3280af22 1566 PL_curpm = newpm; /* ... and pop $1 et al */
f86702cc 1567
a0d0e21e
LW
1568 LEAVE;
1569 LEAVE;
1570
f86702cc 1571 return NORMAL;
a0d0e21e
LW
1572}
1573
1574PP(pp_return)
1575{
4e35701f 1576 djSP; dMARK;
a0d0e21e 1577 I32 cxix;
c09156bb 1578 register PERL_CONTEXT *cx;
f86702cc 1579 struct block_sub cxsub;
1580 bool popsub2 = FALSE;
a0d0e21e
LW
1581 I32 gimme;
1582 SV **newsp;
1583 PMOP *newpm;
1584 I32 optype = 0;
1585
3280af22
NIS
1586 if (PL_curstackinfo->si_type == PERLSI_SORT) {
1587 if (cxstack_ix == PL_sortcxix || dopoptosub(cxstack_ix) <= PL_sortcxix) {
1588 if (cxstack_ix > PL_sortcxix)
1589 dounwind(PL_sortcxix);
1590 AvARRAY(PL_curstack)[1] = *SP;
1591 PL_stack_sp = PL_stack_base + 1;
a0d0e21e
LW
1592 return 0;
1593 }
1594 }
1595
1596 cxix = dopoptosub(cxstack_ix);
1597 if (cxix < 0)
1598 DIE("Can't return outside a subroutine");
1599 if (cxix < cxstack_ix)
1600 dounwind(cxix);
1601
1602 POPBLOCK(cx,newpm);
4e43f39e 1603 switch (CxTYPE(cx)) {
a0d0e21e 1604 case CXt_SUB:
f86702cc 1605 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1606 popsub2 = TRUE;
a0d0e21e
LW
1607 break;
1608 case CXt_EVAL:
1609 POPEVAL(cx);
748a9306
LW
1610 if (optype == OP_REQUIRE &&
1611 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
1612 {
54310121 1613 /* Unassume the success we assumed earlier. */
748a9306 1614 char *name = cx->blk_eval.old_name;
3280af22 1615 (void)hv_delete(GvHVn(PL_incgv), name, strlen(name), G_DISCARD);
748a9306
LW
1616 DIE("%s did not return a true value", name);
1617 }
a0d0e21e
LW
1618 break;
1619 default:
1620 DIE("panic: return");
a0d0e21e
LW
1621 }
1622
a1f49e72 1623 TAINT_NOT;
a0d0e21e 1624 if (gimme == G_SCALAR) {
a29cdaf0
IZ
1625 if (MARK < SP) {
1626 if (popsub2) {
1627 if (cxsub.cv && CvDEPTH(cxsub.cv) > 1) {
1628 if (SvTEMP(TOPs)) {
1629 *++newsp = SvREFCNT_inc(*SP);
1630 FREETMPS;
1631 sv_2mortal(*newsp);
1632 } else {
1633 FREETMPS;
1634 *++newsp = sv_mortalcopy(*SP);
1635 }
1636 } else
1637 *++newsp = (SvTEMP(*SP)) ? *SP : sv_mortalcopy(*SP);
1638 } else
1639 *++newsp = sv_mortalcopy(*SP);
1640 } else
3280af22 1641 *++newsp = &PL_sv_undef;
a0d0e21e 1642 }
54310121 1643 else if (gimme == G_ARRAY) {
a1f49e72 1644 while (++MARK <= SP) {
f86702cc 1645 *++newsp = (popsub2 && SvTEMP(*MARK))
1646 ? *MARK : sv_mortalcopy(*MARK);
a1f49e72
CS
1647 TAINT_NOT; /* Each item is independent */
1648 }
a0d0e21e 1649 }
3280af22 1650 PL_stack_sp = newsp;
a0d0e21e 1651
f86702cc 1652 /* Stack values are safe: */
1653 if (popsub2) {
1654 POPSUB2(); /* release CV and @_ ... */
1655 }
3280af22 1656 PL_curpm = newpm; /* ... and pop $1 et al */
f86702cc 1657
a0d0e21e
LW
1658 LEAVE;
1659 return pop_return();
1660}
1661
1662PP(pp_last)
1663{
4e35701f 1664 djSP;
a0d0e21e 1665 I32 cxix;
c09156bb 1666 register PERL_CONTEXT *cx;
f86702cc 1667 struct block_loop cxloop;
1668 struct block_sub cxsub;
1669 I32 pop2 = 0;
a0d0e21e
LW
1670 I32 gimme;
1671 I32 optype;
1672 OP *nextop;
1673 SV **newsp;
1674 PMOP *newpm;
3280af22 1675 SV **mark = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
a0d0e21e 1676
533c011a 1677 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e
LW
1678 cxix = dopoptoloop(cxstack_ix);
1679 if (cxix < 0)
1680 DIE("Can't \"last\" outside a block");
1681 }
1682 else {
1683 cxix = dopoptolabel(cPVOP->op_pv);
1684 if (cxix < 0)
1685 DIE("Label not found for \"last %s\"", cPVOP->op_pv);
1686 }
1687 if (cxix < cxstack_ix)
1688 dounwind(cxix);
1689
1690 POPBLOCK(cx,newpm);
4e43f39e 1691 switch (CxTYPE(cx)) {
a0d0e21e 1692 case CXt_LOOP:
f86702cc 1693 POPLOOP1(cx); /* Delay POPLOOP2 until stack values are safe */
1694 pop2 = CXt_LOOP;
4fdae800 1695 nextop = cxloop.last_op->op_next;
a0d0e21e 1696 break;
f86702cc 1697 case CXt_SUB:
1698 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1699 pop2 = CXt_SUB;
a0d0e21e
LW
1700 nextop = pop_return();
1701 break;
f86702cc 1702 case CXt_EVAL:
1703 POPEVAL(cx);
a0d0e21e
LW
1704 nextop = pop_return();
1705 break;
1706 default:
1707 DIE("panic: last");
a0d0e21e
LW
1708 }
1709
a1f49e72 1710 TAINT_NOT;
a0d0e21e 1711 if (gimme == G_SCALAR) {
f86702cc 1712 if (MARK < SP)
1713 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
1714 ? *SP : sv_mortalcopy(*SP);
a0d0e21e 1715 else
3280af22 1716 *++newsp = &PL_sv_undef;
a0d0e21e 1717 }
54310121 1718 else if (gimme == G_ARRAY) {
a1f49e72 1719 while (++MARK <= SP) {
f86702cc 1720 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
1721 ? *MARK : sv_mortalcopy(*MARK);
a1f49e72
CS
1722 TAINT_NOT; /* Each item is independent */
1723 }
f86702cc 1724 }
1725 SP = newsp;
1726 PUTBACK;
1727
1728 /* Stack values are safe: */
1729 switch (pop2) {
1730 case CXt_LOOP:
1731 POPLOOP2(); /* release loop vars ... */
4fdae800 1732 LEAVE;
f86702cc 1733 break;
1734 case CXt_SUB:
1735 POPSUB2(); /* release CV and @_ ... */
1736 break;
a0d0e21e 1737 }
3280af22 1738 PL_curpm = newpm; /* ... and pop $1 et al */
a0d0e21e
LW
1739
1740 LEAVE;
f86702cc 1741 return nextop;
a0d0e21e
LW
1742}
1743
1744PP(pp_next)
1745{
1746 I32 cxix;
c09156bb 1747 register PERL_CONTEXT *cx;
a0d0e21e
LW
1748 I32 oldsave;
1749
533c011a 1750 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e
LW
1751 cxix = dopoptoloop(cxstack_ix);
1752 if (cxix < 0)
1753 DIE("Can't \"next\" outside a block");
1754 }
1755 else {
1756 cxix = dopoptolabel(cPVOP->op_pv);
1757 if (cxix < 0)
1758 DIE("Label not found for \"next %s\"", cPVOP->op_pv);
1759 }
1760 if (cxix < cxstack_ix)
1761 dounwind(cxix);
1762
1763 TOPBLOCK(cx);
3280af22 1764 oldsave = PL_scopestack[PL_scopestack_ix - 1];
a0d0e21e
LW
1765 LEAVE_SCOPE(oldsave);
1766 return cx->blk_loop.next_op;
1767}
1768
1769PP(pp_redo)
1770{
1771 I32 cxix;
c09156bb 1772 register PERL_CONTEXT *cx;
a0d0e21e
LW
1773 I32 oldsave;
1774
533c011a 1775 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e
LW
1776 cxix = dopoptoloop(cxstack_ix);
1777 if (cxix < 0)
1778 DIE("Can't \"redo\" outside a block");
1779 }
1780 else {
1781 cxix = dopoptolabel(cPVOP->op_pv);
1782 if (cxix < 0)
1783 DIE("Label not found for \"redo %s\"", cPVOP->op_pv);
1784 }
1785 if (cxix < cxstack_ix)
1786 dounwind(cxix);
1787
1788 TOPBLOCK(cx);
3280af22 1789 oldsave = PL_scopestack[PL_scopestack_ix - 1];
a0d0e21e
LW
1790 LEAVE_SCOPE(oldsave);
1791 return cx->blk_loop.redo_op;
1792}
1793
0824fdcb 1794STATIC OP *
8ac85365 1795dofindlabel(OP *o, char *label, OP **opstack, OP **oplimit)
a0d0e21e
LW
1796{
1797 OP *kid;
1798 OP **ops = opstack;
fc36a67e 1799 static char too_deep[] = "Target of goto is too deeply nested";
a0d0e21e 1800
fc36a67e 1801 if (ops >= oplimit)
1802 croak(too_deep);
11343788
MB
1803 if (o->op_type == OP_LEAVE ||
1804 o->op_type == OP_SCOPE ||
1805 o->op_type == OP_LEAVELOOP ||
1806 o->op_type == OP_LEAVETRY)
fc36a67e 1807 {
5dc0d613 1808 *ops++ = cUNOPo->op_first;
fc36a67e 1809 if (ops >= oplimit)
1810 croak(too_deep);
1811 }
a0d0e21e 1812 *ops = 0;
11343788 1813 if (o->op_flags & OPf_KIDS) {
5c0ca799 1814 dTHR;
a0d0e21e 1815 /* First try all the kids at this level, since that's likeliest. */
11343788 1816 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
a0d0e21e
LW
1817 if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
1818 kCOP->cop_label && strEQ(kCOP->cop_label, label))
1819 return kid;
1820 }
11343788 1821 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
3280af22 1822 if (kid == PL_lastgotoprobe)
a0d0e21e 1823 continue;
fc36a67e 1824 if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
1825 (ops == opstack ||
1826 (ops[-1]->op_type != OP_NEXTSTATE &&
1827 ops[-1]->op_type != OP_DBSTATE)))
1828 *ops++ = kid;
5dc0d613 1829 if (o = dofindlabel(kid, label, ops, oplimit))
11343788 1830 return o;
a0d0e21e
LW
1831 }
1832 }
1833 *ops = 0;
1834 return 0;
1835}
1836
1837PP(pp_dump)
1838{
1839 return pp_goto(ARGS);
1840 /*NOTREACHED*/
1841}
1842
1843PP(pp_goto)
1844{
4e35701f 1845 djSP;
a0d0e21e
LW
1846 OP *retop = 0;
1847 I32 ix;
c09156bb 1848 register PERL_CONTEXT *cx;
fc36a67e 1849#define GOTO_DEPTH 64
1850 OP *enterops[GOTO_DEPTH];
a0d0e21e 1851 char *label;
533c011a 1852 int do_dump = (PL_op->op_type == OP_DUMP);
a0d0e21e
LW
1853
1854 label = 0;
533c011a 1855 if (PL_op->op_flags & OPf_STACKED) {
a0d0e21e 1856 SV *sv = POPs;
36596149 1857 STRLEN n_a;
a0d0e21e
LW
1858
1859 /* This egregious kludge implements goto &subroutine */
1860 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
1861 I32 cxix;
c09156bb 1862 register PERL_CONTEXT *cx;
a0d0e21e
LW
1863 CV* cv = (CV*)SvRV(sv);
1864 SV** mark;
1865 I32 items = 0;
1866 I32 oldsave;
02175a76 1867 int arg_was_real = 0;
a0d0e21e 1868
eca88f33 1869 retry:
4aa0a1f7 1870 if (!CvROOT(cv) && !CvXSUB(cv)) {
eca88f33
GS
1871 GV *gv = CvGV(cv);
1872 GV *autogv;
1873 if (gv) {
1874 SV *tmpstr;
1875 /* autoloaded stub? */
1876 if (cv != GvCV(gv) && (cv = GvCV(gv)))
1877 goto retry;
1878 autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv),
1879 GvNAMELEN(gv), FALSE);
1880 if (autogv && (cv = GvCV(autogv)))
1881 goto retry;
1882 tmpstr = sv_newmortal();
1883 gv_efullname3(tmpstr, gv, Nullch);
4aa0a1f7
AD
1884 DIE("Goto undefined subroutine &%s",SvPVX(tmpstr));
1885 }
1886 DIE("Goto undefined subroutine");
1887 }
1888
a0d0e21e
LW
1889 /* First do some returnish stuff. */
1890 cxix = dopoptosub(cxstack_ix);
1891 if (cxix < 0)
1892 DIE("Can't goto subroutine outside a subroutine");
1893 if (cxix < cxstack_ix)
1894 dounwind(cxix);
1895 TOPBLOCK(cx);
4e43f39e 1896 if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_ENTEREVAL)
b150fb22 1897 DIE("Can't goto subroutine from an eval-string");
3280af22 1898 mark = PL_stack_sp;
4e43f39e 1899 if (CxTYPE(cx) == CXt_SUB &&
b150fb22 1900 cx->blk_sub.hasargs) { /* put @_ back onto stack */
a0d0e21e
LW
1901 AV* av = cx->blk_sub.argarray;
1902
93965878 1903 items = AvFILLp(av) + 1;
3280af22
NIS
1904 PL_stack_sp++;
1905 EXTEND(PL_stack_sp, items); /* @_ could have been extended. */
1906 Copy(AvARRAY(av), PL_stack_sp, items, SV*);
1907 PL_stack_sp += items;
6d4ff0d2 1908#ifndef USE_THREADS
3280af22
NIS
1909 SvREFCNT_dec(GvAV(PL_defgv));
1910 GvAV(PL_defgv) = cx->blk_sub.savearray;
6d4ff0d2 1911#endif /* USE_THREADS */
02175a76
GB
1912 if (AvREAL(av)) {
1913 arg_was_real = 1;
1914 AvREAL_off(av); /* so av_clear() won't clobber elts */
1915 }
4633a7c4 1916 av_clear(av);
a0d0e21e 1917 }
1fa4e549
AD
1918 else if (CvXSUB(cv)) { /* put GvAV(defgv) back onto stack */
1919 AV* av;
1920 int i;
1921#ifdef USE_THREADS
533c011a 1922 av = (AV*)PL_curpad[0];
1fa4e549 1923#else
3280af22 1924 av = GvAV(PL_defgv);
1fa4e549
AD
1925#endif
1926 items = AvFILLp(av) + 1;
3280af22
NIS
1927 PL_stack_sp++;
1928 EXTEND(PL_stack_sp, items); /* @_ could have been extended. */
1929 Copy(AvARRAY(av), PL_stack_sp, items, SV*);
1930 PL_stack_sp += items;
1fa4e549 1931 }
4e43f39e 1932 if (CxTYPE(cx) == CXt_SUB &&
b150fb22 1933 !(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
a0d0e21e 1934 SvREFCNT_dec(cx->blk_sub.cv);
3280af22 1935 oldsave = PL_scopestack[PL_scopestack_ix - 1];
a0d0e21e
LW
1936 LEAVE_SCOPE(oldsave);
1937
1938 /* Now do some callish stuff. */
1939 SAVETMPS;
1940 if (CvXSUB(cv)) {
1941 if (CvOLDSTYLE(cv)) {
ecfc5424 1942 I32 (*fp3)_((int,int,int));
924508f0
GS
1943 while (SP > mark) {
1944 SP[1] = SP[0];
1945 SP--;
a0d0e21e 1946 }
ecfc5424
AD
1947 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
1948 items = (*fp3)(CvXSUBANY(cv).any_i32,
3280af22 1949 mark - PL_stack_base + 1,
ecfc5424 1950 items);
3280af22 1951 SP = PL_stack_base + items;
a0d0e21e
LW
1952 }
1953 else {
1fa4e549
AD
1954 SV **newsp;
1955 I32 gimme;
1956
3280af22 1957 PL_stack_sp--; /* There is no cv arg. */
1fa4e549
AD
1958 /* Push a mark for the start of arglist */
1959 PUSHMARK(mark);
1d583055 1960 (void)(*CvXSUB(cv))(cv _PERL_OBJECT_THIS);
1fa4e549 1961 /* Pop the current context like a decent sub should */
3280af22 1962 POPBLOCK(cx, PL_curpm);
1fa4e549 1963 /* Do _not_ use PUTBACK, keep the XSUB's return stack! */
a0d0e21e
LW
1964 }
1965 LEAVE;
1966 return pop_return();
1967 }
1968 else {
1969 AV* padlist = CvPADLIST(cv);
1970 SV** svp = AvARRAY(padlist);
4e43f39e 1971 if (CxTYPE(cx) == CXt_EVAL) {
3280af22
NIS
1972 PL_in_eval = cx->blk_eval.old_in_eval;
1973 PL_eval_root = cx->blk_eval.old_eval_root;
b150fb22
RH
1974 cx->cx_type = CXt_SUB;
1975 cx->blk_sub.hasargs = 0;
1976 }
a0d0e21e
LW
1977 cx->blk_sub.cv = cv;
1978 cx->blk_sub.olddepth = CvDEPTH(cv);
1979 CvDEPTH(cv)++;
1980 if (CvDEPTH(cv) < 2)
1981 (void)SvREFCNT_inc(cv);
1982 else { /* save temporaries on recursion? */
3280af22 1983 if (CvDEPTH(cv) == 100 && PL_dowarn)
44a8e56a 1984 sub_crush_depth(cv);
93965878 1985 if (CvDEPTH(cv) > AvFILLp(padlist)) {
a0d0e21e 1986 AV *newpad = newAV();
4aa0a1f7 1987 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
93965878 1988 I32 ix = AvFILLp((AV*)svp[1]);
a0d0e21e 1989 svp = AvARRAY(svp[0]);
748a9306 1990 for ( ;ix > 0; ix--) {
3280af22 1991 if (svp[ix] != &PL_sv_undef) {
748a9306 1992 char *name = SvPVX(svp[ix]);
5f05dabc 1993 if ((SvFLAGS(svp[ix]) & SVf_FAKE)
1994 || *name == '&')
1995 {
1996 /* outer lexical or anon code */
748a9306 1997 av_store(newpad, ix,
4aa0a1f7 1998 SvREFCNT_inc(oldpad[ix]) );
748a9306
LW
1999 }
2000 else { /* our own lexical */
2001 if (*name == '@')
2002 av_store(newpad, ix, sv = (SV*)newAV());
2003 else if (*name == '%')
2004 av_store(newpad, ix, sv = (SV*)newHV());
2005 else
2006 av_store(newpad, ix, sv = NEWSV(0,0));
2007 SvPADMY_on(sv);
2008 }
a0d0e21e
LW
2009 }
2010 else {
748a9306 2011 av_store(newpad, ix, sv = NEWSV(0,0));
a0d0e21e
LW
2012 SvPADTMP_on(sv);
2013 }
2014 }
2015 if (cx->blk_sub.hasargs) {
2016 AV* av = newAV();
2017 av_extend(av, 0);
2018 av_store(newpad, 0, (SV*)av);
2019 AvFLAGS(av) = AVf_REIFY;
2020 }
2021 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
93965878 2022 AvFILLp(padlist) = CvDEPTH(cv);
a0d0e21e
LW
2023 svp = AvARRAY(padlist);
2024 }
2025 }
6d4ff0d2
MB
2026#ifdef USE_THREADS
2027 if (!cx->blk_sub.hasargs) {
533c011a 2028 AV* av = (AV*)PL_curpad[0];
6d4ff0d2 2029
93965878 2030 items = AvFILLp(av) + 1;
6d4ff0d2
MB
2031 if (items) {
2032 /* Mark is at the end of the stack. */
924508f0
GS
2033 EXTEND(SP, items);
2034 Copy(AvARRAY(av), SP + 1, items, SV*);
2035 SP += items;
6d4ff0d2
MB
2036 PUTBACK ;
2037 }
2038 }
2039#endif /* USE_THREADS */
3280af22
NIS
2040 SAVESPTR(PL_curpad);
2041 PL_curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
6d4ff0d2
MB
2042#ifndef USE_THREADS
2043 if (cx->blk_sub.hasargs)
2044#endif /* USE_THREADS */
2045 {
3280af22 2046 AV* av = (AV*)PL_curpad[0];
a0d0e21e
LW
2047 SV** ary;
2048
6d4ff0d2 2049#ifndef USE_THREADS
3280af22
NIS
2050 cx->blk_sub.savearray = GvAV(PL_defgv);
2051 GvAV(PL_defgv) = (AV*)SvREFCNT_inc(av);
6d4ff0d2
MB
2052#endif /* USE_THREADS */
2053 cx->blk_sub.argarray = av;
a0d0e21e
LW
2054 ++mark;
2055
2056 if (items >= AvMAX(av) + 1) {
2057 ary = AvALLOC(av);
2058 if (AvARRAY(av) != ary) {
2059 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2060 SvPVX(av) = (char*)ary;
2061 }
2062 if (items >= AvMAX(av) + 1) {
2063 AvMAX(av) = items - 1;
2064 Renew(ary,items+1,SV*);
2065 AvALLOC(av) = ary;
2066 SvPVX(av) = (char*)ary;
2067 }
2068 }
2069 Copy(mark,AvARRAY(av),items,SV*);
93965878 2070 AvFILLp(av) = items - 1;
02175a76
GB
2071 /* preserve @_ nature */
2072 if (arg_was_real) {
2073 AvREIFY_off(av);
2074 AvREAL_on(av);
2075 }
a0d0e21e
LW
2076 while (items--) {
2077 if (*mark)
2078 SvTEMP_off(*mark);
2079 mark++;
2080 }
2081 }
491527d0 2082 if (PERLDB_SUB) { /* Checking curstash breaks DProf. */
44a8e56a 2083 /*
2084 * We do not care about using sv to call CV;
2085 * it's for informational purposes only.
2086 */
3280af22 2087 SV *sv = GvSV(PL_DBsub);
491527d0
GS
2088 CV *gotocv;
2089
2090 if (PERLDB_SUB_NN) {
2091 SvIVX(sv) = (IV)cv; /* Already upgraded, saved */
2092 } else {
2093 save_item(sv);
2094 gv_efullname3(sv, CvGV(cv), Nullch);
2095 }
2096 if ( PERLDB_GOTO
2097 && (gotocv = perl_get_cv("DB::goto", FALSE)) ) {
3280af22 2098 PUSHMARK( PL_stack_sp );
491527d0 2099 perl_call_sv((SV*)gotocv, G_SCALAR | G_NODEBUG);
3280af22 2100 PL_stack_sp--;
491527d0 2101 }
1ce6579f 2102 }
a0d0e21e
LW
2103 RETURNOP(CvSTART(cv));
2104 }
2105 }
2106 else
36596149 2107 label = SvPV(sv,n_a);
a0d0e21e 2108 }
533c011a 2109 else if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e
LW
2110 if (! do_dump)
2111 DIE("goto must have label");
2112 }
2113 else
2114 label = cPVOP->op_pv;
2115
2116 if (label && *label) {
2117 OP *gotoprobe = 0;
2118
2119 /* find label */
2120
3280af22 2121 PL_lastgotoprobe = 0;
a0d0e21e
LW
2122 *enterops = 0;
2123 for (ix = cxstack_ix; ix >= 0; ix--) {
2124 cx = &cxstack[ix];
4e43f39e 2125 switch (CxTYPE(cx)) {
a0d0e21e 2126 case CXt_EVAL:
3280af22 2127 gotoprobe = PL_eval_root; /* XXX not good for nested eval */
a0d0e21e
LW
2128 break;
2129 case CXt_LOOP:
2130 gotoprobe = cx->blk_oldcop->op_sibling;
2131 break;
2132 case CXt_SUBST:
2133 continue;
2134 case CXt_BLOCK:
2135 if (ix)
2136 gotoprobe = cx->blk_oldcop->op_sibling;
2137 else
3280af22 2138 gotoprobe = PL_main_root;
a0d0e21e 2139 break;
b3933176
CS
2140 case CXt_SUB:
2141 if (CvDEPTH(cx->blk_sub.cv)) {
2142 gotoprobe = CvROOT(cx->blk_sub.cv);
2143 break;
2144 }
2145 /* FALL THROUGH */
0a753a76 2146 case CXt_NULL:
2147 DIE("Can't \"goto\" outside a block");
a0d0e21e
LW
2148 default:
2149 if (ix)
2150 DIE("panic: goto");
3280af22 2151 gotoprobe = PL_main_root;
a0d0e21e
LW
2152 break;
2153 }
fc36a67e 2154 retop = dofindlabel(gotoprobe, label,
2155 enterops, enterops + GOTO_DEPTH);
a0d0e21e
LW
2156 if (retop)
2157 break;
3280af22 2158 PL_lastgotoprobe = gotoprobe;
a0d0e21e
LW
2159 }
2160 if (!retop)
2161 DIE("Can't find label %s", label);
2162
2163 /* pop unwanted frames */
2164
2165 if (ix < cxstack_ix) {
2166 I32 oldsave;
2167
2168 if (ix < 0)
2169 ix = 0;
2170 dounwind(ix);
2171 TOPBLOCK(cx);
3280af22 2172 oldsave = PL_scopestack[PL_scopestack_ix];
a0d0e21e
LW
2173 LEAVE_SCOPE(oldsave);
2174 }
2175
2176 /* push wanted frames */
2177
748a9306 2178 if (*enterops && enterops[1]) {
533c011a 2179 OP *oldop = PL_op;
748a9306 2180 for (ix = 1; enterops[ix]; ix++) {
533c011a 2181 PL_op = enterops[ix];
84902520
TB
2182 /* Eventually we may want to stack the needed arguments
2183 * for each op. For now, we punt on the hard ones. */
533c011a 2184 if (PL_op->op_type == OP_ENTERITER)
84902520
TB
2185 DIE("Can't \"goto\" into the middle of a foreach loop",
2186 label);
0824fdcb 2187 (CALLOP->op_ppaddr)(ARGS);
a0d0e21e 2188 }
533c011a 2189 PL_op = oldop;
a0d0e21e
LW
2190 }
2191 }
2192
2193 if (do_dump) {
a5f75d66 2194#ifdef VMS
6b88bc9c 2195 if (!retop) retop = PL_main_start;
a5f75d66 2196#endif
3280af22
NIS
2197 PL_restartop = retop;
2198 PL_do_undump = TRUE;
a0d0e21e
LW
2199
2200 my_unexec();
2201
3280af22
NIS
2202 PL_restartop = 0; /* hmm, must be GNU unexec().. */
2203 PL_do_undump = FALSE;
a0d0e21e
LW
2204 }
2205
2206 RETURNOP(retop);
2207}
2208
2209PP(pp_exit)
2210{
4e35701f 2211 djSP;
a0d0e21e
LW
2212 I32 anum;
2213
2214 if (MAXARG < 1)
2215 anum = 0;
ff0cee69 2216 else {
a0d0e21e 2217 anum = SvIVx(POPs);
ff0cee69 2218#ifdef VMSISH_EXIT
2219 if (anum == 1 && VMSISH_EXIT)
2220 anum = 0;
2221#endif
2222 }
a0d0e21e 2223 my_exit(anum);
3280af22 2224 PUSHs(&PL_sv_undef);
a0d0e21e
LW
2225 RETURN;
2226}
2227
2228#ifdef NOTYET
2229PP(pp_nswitch)
2230{
4e35701f 2231 djSP;
a0d0e21e
LW
2232 double value = SvNVx(GvSV(cCOP->cop_gv));
2233 register I32 match = I_32(value);
2234
2235 if (value < 0.0) {
2236 if (((double)match) > value)
2237 --match; /* was fractional--truncate other way */
2238 }
2239 match -= cCOP->uop.scop.scop_offset;
2240 if (match < 0)
2241 match = 0;
2242 else if (match > cCOP->uop.scop.scop_max)
2243 match = cCOP->uop.scop.scop_max;
6b88bc9c
GS
2244 PL_op = cCOP->uop.scop.scop_next[match];
2245 RETURNOP(PL_op);
a0d0e21e
LW
2246}
2247
2248PP(pp_cswitch)
2249{
4e35701f 2250 djSP;
a0d0e21e
LW
2251 register I32 match;
2252
6b88bc9c
GS
2253 if (PL_multiline)
2254 PL_op = PL_op->op_next; /* can't assume anything */
a0d0e21e 2255 else {
36596149
GB
2256 STRLEN n_a;
2257 match = *(SvPVx(GvSV(cCOP->cop_gv), n_a)) & 255;
a0d0e21e
LW
2258 match -= cCOP->uop.scop.scop_offset;
2259 if (match < 0)
2260 match = 0;
2261 else if (match > cCOP->uop.scop.scop_max)
2262 match = cCOP->uop.scop.scop_max;
6b88bc9c 2263 PL_op = cCOP->uop.scop.scop_next[match];
a0d0e21e 2264 }
6b88bc9c 2265 RETURNOP(PL_op);
a0d0e21e
LW
2266}
2267#endif
2268
2269/* Eval. */
2270
0824fdcb 2271STATIC void
8ac85365 2272save_lines(AV *array, SV *sv)
a0d0e21e
LW
2273{
2274 register char *s = SvPVX(sv);
2275 register char *send = SvPVX(sv) + SvCUR(sv);
2276 register char *t;
2277 register I32 line = 1;
2278
2279 while (s && s < send) {
2280 SV *tmpstr = NEWSV(85,0);
2281
2282 sv_upgrade(tmpstr, SVt_PVMG);
2283 t = strchr(s, '\n');
2284 if (t)
2285 t++;
2286 else
2287 t = send;
2288
2289 sv_setpvn(tmpstr, s, t - s);
2290 av_store(array, line++, tmpstr);
2291 s = t;
2292 }
2293}
2294
0824fdcb 2295STATIC OP *
8ac85365 2296docatch(OP *o)
1e422769 2297{
e858de61 2298 dTHR;
6224f72b 2299 int ret;
533c011a 2300 OP *oldop = PL_op;
54310121 2301 dJMPENV;
1e422769 2302
533c011a 2303 PL_op = o;
1e422769 2304#ifdef DEBUGGING
54310121 2305 assert(CATCH_GET == TRUE);
3280af22 2306 DEBUG_l(deb("Setting up local jumplevel %p, was %p\n", &cur_env, PL_top_env));
1e422769 2307#endif
6224f72b
GS
2308 JMPENV_PUSH(ret);
2309 switch (ret) {
1e422769 2310 default: /* topmost level handles it */
f2882d09 2311pass_the_buck:
54310121 2312 JMPENV_POP;
533c011a 2313 PL_op = oldop;
6224f72b 2314 JMPENV_JUMP(ret);
1e422769 2315 /* NOTREACHED */
6224f72b 2316 case 3:
f2882d09
GB
2317 if (!PL_restartop)
2318 goto pass_the_buck;
533c011a 2319 PL_op = PL_restartop;
3280af22 2320 PL_restartop = 0;
1e422769 2321 /* FALL THROUGH */
6224f72b 2322 case 0:
0824fdcb 2323 CALLRUNOPS();
1e422769 2324 break;
2325 }
54310121 2326 JMPENV_POP;
533c011a 2327 PL_op = oldop;
1e422769 2328 return Nullop;
2329}
2330
c277df42
IZ
2331OP *
2332sv_compile_2op(SV *sv, OP** startop, char *code, AV** avp)
2333/* sv Text to convert to OP tree. */
2334/* startop op_free() this to undo. */
2335/* code Short string id of the caller. */
2336{
2337 dSP; /* Make POPBLOCK work. */
2338 PERL_CONTEXT *cx;
2339 SV **newsp;
f987c7de 2340 I32 gimme = 0; /* SUSPECT - INITIALZE TO WHAT? NI-S */
c277df42
IZ
2341 I32 optype;
2342 OP dummy;
533c011a 2343 OP *oop = PL_op, *rop;
c277df42
IZ
2344 char tmpbuf[TYPE_DIGITS(long) + 12 + 10];
2345 char *safestr;
2346
2347 ENTER;
2348 lex_start(sv);
2349 SAVETMPS;
2350 /* switch to eval mode */
2351
cbce877f
IZ
2352 if (PL_curcop == &PL_compiling) {
2353 SAVESPTR(PL_compiling.cop_stash);
2354 PL_compiling.cop_stash = PL_curstash;
2355 }
3280af22
NIS
2356 SAVESPTR(PL_compiling.cop_filegv);
2357 SAVEI16(PL_compiling.cop_line);
2358 sprintf(tmpbuf, "_<(%.10s_eval %lu)", code, (unsigned long)++PL_evalseq);
2359 PL_compiling.cop_filegv = gv_fetchfile(tmpbuf+2);
2360 PL_compiling.cop_line = 1;
c277df42
IZ
2361 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
2362 deleting the eval's FILEGV from the stash before gv_check() runs
2363 (i.e. before run-time proper). To work around the coredump that
2364 ensues, we always turn GvMULTI_on for any globals that were
2365 introduced within evals. See force_ident(). GSAR 96-10-12 */
2366 safestr = savepv(tmpbuf);
3280af22 2367 SAVEDELETE(PL_defstash, safestr, strlen(safestr));
b3ac6de7 2368 SAVEHINTS();
d1ca3daa 2369#ifdef OP_IN_REGISTER
6b88bc9c 2370 PL_opsave = op;
d1ca3daa 2371#else
533c011a 2372 SAVEPPTR(PL_op);
d1ca3daa 2373#endif
3280af22 2374 PL_hints = 0;
c277df42 2375
533c011a
NIS
2376 PL_op = &dummy;
2377 PL_op->op_type = 0; /* Avoid uninit warning. */
2378 PL_op->op_flags = 0; /* Avoid uninit warning. */
c277df42 2379 PUSHBLOCK(cx, CXt_EVAL, SP);
6b88bc9c 2380 PUSHEVAL(cx, 0, PL_compiling.cop_filegv);
c277df42 2381 rop = doeval(G_SCALAR, startop);
3280af22 2382 POPBLOCK(cx,PL_curpm);
c277df42
IZ
2383 POPEVAL(cx);
2384
2385 (*startop)->op_type = OP_NULL;
2386 (*startop)->op_ppaddr = ppaddr[OP_NULL];
2387 lex_end();
3280af22 2388 *avp = (AV*)SvREFCNT_inc(PL_comppad);
c277df42 2389 LEAVE;
d1ca3daa 2390#ifdef OP_IN_REGISTER
6b88bc9c 2391 op = PL_opsave;
d1ca3daa 2392#endif
c277df42
IZ
2393 return rop;
2394}
2395
0f15f207 2396/* With USE_THREADS, eval_owner must be held on entry to doeval */
0824fdcb 2397STATIC OP *
c277df42 2398doeval(int gimme, OP** startop)
a0d0e21e
LW
2399{
2400 dSP;
533c011a 2401 OP *saveop = PL_op;
a0d0e21e 2402 HV *newstash;
ff3ff8d1 2403 CV *caller;
748a9306 2404 AV* comppadlist;
67a38de0 2405 I32 i;
a0d0e21e 2406
3280af22 2407 PL_in_eval = 1;
a0d0e21e 2408
1ce6579f 2409 PUSHMARK(SP);
2410
a0d0e21e
LW
2411 /* set up a scratch pad */
2412
3280af22
NIS
2413 SAVEI32(PL_padix);
2414 SAVESPTR(PL_curpad);
2415 SAVESPTR(PL_comppad);
2416 SAVESPTR(PL_comppad_name);
2417 SAVEI32(PL_comppad_name_fill);
2418 SAVEI32(PL_min_intro_pending);
2419 SAVEI32(PL_max_intro_pending);
748a9306 2420
3280af22 2421 caller = PL_compcv;
4e43f39e 2422 for (i = cxstack_ix - 1; i >= 0; i--) {
67a38de0 2423 PERL_CONTEXT *cx = &cxstack[i];
4e43f39e 2424 if (CxTYPE(cx) == CXt_EVAL)
67a38de0 2425 break;
4e43f39e 2426 else if (CxTYPE(cx) == CXt_SUB) {
67a38de0
NIS
2427 caller = cx->blk_sub.cv;
2428 break;
2429 }
2430 }
2431
3280af22
NIS
2432 SAVESPTR(PL_compcv);
2433 PL_compcv = (CV*)NEWSV(1104,0);
2434 sv_upgrade((SV *)PL_compcv, SVt_PVCV);
a375155a 2435 CvEVAL_on(PL_compcv);
11343788 2436#ifdef USE_THREADS
533c011a
NIS
2437 CvOWNER(PL_compcv) = 0;
2438 New(666, CvMUTEXP(PL_compcv), 1, perl_mutex);
2439 MUTEX_INIT(CvMUTEXP(PL_compcv));
11343788 2440#endif /* USE_THREADS */
748a9306 2441
3280af22
NIS
2442 PL_comppad = newAV();
2443 av_push(PL_comppad, Nullsv);
2444 PL_curpad = AvARRAY(PL_comppad);
2445 PL_comppad_name = newAV();
2446 PL_comppad_name_fill = 0;
2447 PL_min_intro_pending = 0;
2448 PL_padix = 0;
11343788 2449#ifdef USE_THREADS
533c011a
NIS
2450 av_store(PL_comppad_name, 0, newSVpv("@_", 2));
2451 PL_curpad[0] = (SV*)newAV();
2452 SvPADMY_on(PL_curpad[0]); /* XXX Needed? */
11343788 2453#endif /* USE_THREADS */
a0d0e21e 2454
748a9306
LW
2455 comppadlist = newAV();
2456 AvREAL_off(comppadlist);
3280af22
NIS
2457 av_store(comppadlist, 0, (SV*)PL_comppad_name);
2458 av_store(comppadlist, 1, (SV*)PL_comppad);
2459 CvPADLIST(PL_compcv) = comppadlist;
2c05e328 2460
c277df42 2461 if (!saveop || saveop->op_type != OP_REQUIRE)
3280af22 2462 CvOUTSIDE(PL_compcv) = (CV*)SvREFCNT_inc(caller);
07055b4c 2463
3280af22 2464 SAVEFREESV(PL_compcv);
748a9306 2465
a0d0e21e
LW
2466 /* make sure we compile in the right package */
2467
3280af22
NIS
2468 newstash = PL_curcop->cop_stash;
2469 if (PL_curstash != newstash) {
2470 SAVESPTR(PL_curstash);
2471 PL_curstash = newstash;
a0d0e21e 2472 }
3280af22
NIS
2473 SAVESPTR(PL_beginav);
2474 PL_beginav = newAV();
2475 SAVEFREESV(PL_beginav);
a0d0e21e
LW
2476
2477 /* try to compile it */
2478
3280af22
NIS
2479 PL_eval_root = Nullop;
2480 PL_error_count = 0;
2481 PL_curcop = &PL_compiling;
2482 PL_curcop->cop_arybase = 0;
2483 SvREFCNT_dec(PL_rs);
2484 PL_rs = newSVpv("\n", 1);
c277df42 2485 if (saveop && saveop->op_flags & OPf_SPECIAL)
3280af22 2486 PL_in_eval |= 4;
1ce6579f 2487 else
38a03e6e 2488 sv_setpv(ERRSV,"");
3280af22 2489 if (yyparse() || PL_error_count || !PL_eval_root) {
a0d0e21e
LW
2490 SV **newsp;
2491 I32 gimme;
c09156bb 2492 PERL_CONTEXT *cx;
c277df42 2493 I32 optype = 0; /* Might be reset by POPEVAL. */
36596149 2494 STRLEN n_a;
a0d0e21e 2495
533c011a 2496 PL_op = saveop;
3280af22
NIS
2497 if (PL_eval_root) {
2498 op_free(PL_eval_root);
2499 PL_eval_root = Nullop;
a0d0e21e 2500 }
3280af22 2501 SP = PL_stack_base + POPMARK; /* pop original mark */
c277df42 2502 if (!startop) {
3280af22 2503 POPBLOCK(cx,PL_curpm);
c277df42
IZ
2504 POPEVAL(cx);
2505 pop_return();
2506 }
a0d0e21e
LW
2507 lex_end();
2508 LEAVE;
7a2e2cd6 2509 if (optype == OP_REQUIRE) {
36596149 2510 char* msg = SvPVx(ERRSV, n_a);
7a2e2cd6 2511 DIE("%s", *msg ? msg : "Compilation failed in require");
c277df42 2512 } else if (startop) {
36596149 2513 char* msg = SvPVx(ERRSV, n_a);
c277df42 2514
3280af22 2515 POPBLOCK(cx,PL_curpm);
c277df42
IZ
2516 POPEVAL(cx);
2517 croak("%sCompilation failed in regexp", (*msg ? msg : "Unknown error\n"));
7a2e2cd6 2518 }
3280af22
NIS
2519 SvREFCNT_dec(PL_rs);
2520 PL_rs = SvREFCNT_inc(PL_nrs);
f2134d95 2521#ifdef USE_THREADS
533c011a
NIS
2522 MUTEX_LOCK(&PL_eval_mutex);
2523 PL_eval_owner = 0;
2524 COND_SIGNAL(&PL_eval_cond);
2525 MUTEX_UNLOCK(&PL_eval_mutex);
f2134d95 2526#endif /* USE_THREADS */
a0d0e21e
LW
2527 RETPUSHUNDEF;
2528 }
3280af22
NIS
2529 SvREFCNT_dec(PL_rs);
2530 PL_rs = SvREFCNT_inc(PL_nrs);
2531 PL_compiling.cop_line = 0;
c277df42 2532 if (startop) {
3280af22
NIS
2533 *startop = PL_eval_root;
2534 SvREFCNT_dec(CvOUTSIDE(PL_compcv));
2535 CvOUTSIDE(PL_compcv) = Nullcv;
c277df42 2536 } else
3280af22 2537 SAVEFREEOP(PL_eval_root);
54310121 2538 if (gimme & G_VOID)
3280af22 2539 scalarvoid(PL_eval_root);
54310121 2540 else if (gimme & G_ARRAY)
3280af22 2541 list(PL_eval_root);
a0d0e21e 2542 else
3280af22 2543 scalar(PL_eval_root);
a0d0e21e
LW
2544
2545 DEBUG_x(dump_eval());
2546
55497cff 2547 /* Register with debugger: */
84902520 2548 if (PERLDB_INTER && saveop->op_type == OP_REQUIRE) {
55497cff 2549 CV *cv = perl_get_cv("DB::postponed", FALSE);
55497cff 2550 if (cv) {
2551 dSP;
924508f0 2552 PUSHMARK(SP);
3280af22 2553 XPUSHs((SV*)PL_compiling.cop_filegv);
55497cff 2554 PUTBACK;
2555 perl_call_sv((SV*)cv, G_DISCARD);
2556 }
2557 }
2558
a0d0e21e
LW
2559 /* compiled okay, so do it */
2560
3280af22
NIS
2561 CvDEPTH(PL_compcv) = 1;
2562 SP = PL_stack_base + POPMARK; /* pop original mark */
533c011a 2563 PL_op = saveop; /* The caller may need it. */
b35b2403 2564#ifdef USE_THREADS
533c011a
NIS
2565 MUTEX_LOCK(&PL_eval_mutex);
2566 PL_eval_owner = 0;
2567 COND_SIGNAL(&PL_eval_cond);
2568 MUTEX_UNLOCK(&PL_eval_mutex);
b35b2403 2569#endif /* USE_THREADS */
5dc0d613 2570
3280af22 2571 RETURNOP(PL_eval_start);
a0d0e21e
LW
2572}
2573
2574PP(pp_require)
2575{
4e35701f 2576 djSP;
c09156bb 2577 register PERL_CONTEXT *cx;
a0d0e21e
LW
2578 SV *sv;
2579 char *name;
6132ea6c 2580 STRLEN len;
46fc3d4c 2581 char *tryname;
2582 SV *namesv = Nullsv;
a0d0e21e
LW
2583 SV** svp;
2584 I32 gimme = G_SCALAR;
760ac839 2585 PerlIO *tryrsfp = 0;
36596149 2586 STRLEN n_a;
a0d0e21e
LW
2587
2588 sv = POPs;
4633a7c4 2589 if (SvNIOKp(sv) && !SvPOKp(sv)) {
36477c24 2590 SET_NUMERIC_STANDARD();
3280af22 2591 if (atof(PL_patchlevel) + 0.00000999 < SvNV(sv))
a5f75d66 2592 DIE("Perl %s required--this is only version %s, stopped",
36596149 2593 SvPV(sv,n_a),PL_patchlevel);
a0d0e21e
LW
2594 RETPUSHYES;
2595 }
6132ea6c
GS
2596 name = SvPV(sv, len);
2597 if (!(name && len > 0 && *name))
a0d0e21e 2598 DIE("Null filename used");
4633a7c4 2599 TAINT_PROPER("require");
533c011a 2600 if (PL_op->op_type == OP_REQUIRE &&
3280af22
NIS
2601 (svp = hv_fetch(GvHVn(PL_incgv), name, len, 0)) &&
2602 *svp != &PL_sv_undef)
a0d0e21e
LW
2603 RETPUSHYES;
2604
2605 /* prepare to compile file */
2606
46fc3d4c 2607 if (*name == '/' ||
2608 (*name == '.' &&
2609 (name[1] == '/' ||
2610 (name[1] == '.' && name[2] == '/')))
4633a7c4 2611#ifdef DOSISH
46fc3d4c 2612 || (name[0] && name[1] == ':')
4633a7c4 2613#endif
ba42ef2f
WJ
2614#ifdef WIN32
2615 || (name[0] == '\\' && name[1] == '\\') /* UNC path */
2616#endif
748a9306 2617#ifdef VMS
46fc3d4c 2618 || (strchr(name,':') || ((*name == '[' || *name == '<') &&
2619 (isALNUM(name[1]) || strchr("$-_]>",name[1]))))
748a9306
LW
2620#endif
2621 )
a0d0e21e 2622 {
46fc3d4c 2623 tryname = name;
a868473f 2624 tryrsfp = PerlIO_open(name,PERL_SCRIPT_MODE);
a0d0e21e
LW
2625 }
2626 else {
3280af22 2627 AV *ar = GvAVn(PL_incgv);
a0d0e21e 2628 I32 i;
748a9306 2629#ifdef VMS
46fc3d4c 2630 char *unixname;
2631 if ((unixname = tounixspec(name, Nullch)) != Nullch)
2632#endif
2633 {
2634 namesv = NEWSV(806, 0);
2635 for (i = 0; i <= AvFILL(ar); i++) {
36596149 2636 char *dir = SvPVx(*av_fetch(ar, i, TRUE), n_a);
46fc3d4c 2637#ifdef VMS
2638 char *unixdir;
2639 if ((unixdir = tounixpath(dir, Nullch)) == Nullch)
2640 continue;
2641 sv_setpv(namesv, unixdir);
2642 sv_catpv(namesv, unixname);
748a9306 2643#else
46fc3d4c 2644 sv_setpvf(namesv, "%s/%s", dir, name);
748a9306 2645#endif
1f27a7cf 2646 TAINT_PROPER("require");
46fc3d4c 2647 tryname = SvPVX(namesv);
a868473f 2648 tryrsfp = PerlIO_open(tryname, PERL_SCRIPT_MODE);
46fc3d4c 2649 if (tryrsfp) {
2650 if (tryname[0] == '.' && tryname[1] == '/')
2651 tryname += 2;
2652 break;
2653 }
a0d0e21e
LW
2654 }
2655 }
2656 }
3280af22
NIS
2657 SAVESPTR(PL_compiling.cop_filegv);
2658 PL_compiling.cop_filegv = gv_fetchfile(tryrsfp ? tryname : name);
46fc3d4c 2659 SvREFCNT_dec(namesv);
a0d0e21e 2660 if (!tryrsfp) {
533c011a 2661 if (PL_op->op_type == OP_REQUIRE) {
cca87523 2662 SV *msg = sv_2mortal(newSVpvf("Can't locate %s in @INC", name));
2683423c 2663 SV *dirmsgsv = NEWSV(0, 0);
3280af22 2664 AV *ar = GvAVn(PL_incgv);
2683423c 2665 I32 i;
46fc3d4c 2666 if (instr(SvPVX(msg), ".h "))
2667 sv_catpv(msg, " (change .h to .ph maybe?)");
2668 if (instr(SvPVX(msg), ".ph "))
2669 sv_catpv(msg, " (did you run h2ph?)");
3e3baf6d 2670 sv_catpv(msg, " (@INC contains:");
2683423c 2671 for (i = 0; i <= AvFILL(ar); i++) {
36596149 2672 char *dir = SvPVx(*av_fetch(ar, i, TRUE), n_a);
3e3baf6d 2673 sv_setpvf(dirmsgsv, " %s", dir);
2683423c
JA
2674 sv_catsv(msg, dirmsgsv);
2675 }
3e3baf6d 2676 sv_catpvn(msg, ")", 1);
2683423c 2677 SvREFCNT_dec(dirmsgsv);
fc36a67e 2678 DIE("%_", msg);
a0d0e21e
LW
2679 }
2680
2681 RETPUSHUNDEF;
2682 }
1f6b3999
GB
2683 else
2684 SETERRNO(0, SS$_NORMAL);
a0d0e21e
LW
2685
2686 /* Assume success here to prevent recursive requirement. */
3280af22
NIS
2687 (void)hv_store(GvHVn(PL_incgv), name, strlen(name),
2688 newSVsv(GvSV(PL_compiling.cop_filegv)), 0 );
a0d0e21e
LW
2689
2690 ENTER;
2691 SAVETMPS;
2692 lex_start(sv_2mortal(newSVpv("",0)));
3f4fe389
GB
2693 SAVEGENERICSV(PL_rsfp_filters);
2694 PL_rsfp_filters = Nullav;
e50aee73 2695
3280af22 2696 PL_rsfp = tryrsfp;
a0d0e21e
LW
2697 name = savepv(name);
2698 SAVEFREEPV(name);
b3ac6de7 2699 SAVEHINTS();
3280af22 2700 PL_hints = 0;
a0d0e21e
LW
2701
2702 /* switch to eval mode */
2703
533c011a 2704 push_return(PL_op->op_next);
a0d0e21e 2705 PUSHBLOCK(cx, CXt_EVAL, SP);
6b88bc9c 2706 PUSHEVAL(cx, name, PL_compiling.cop_filegv);
a0d0e21e 2707
22d87168 2708 SAVEI16(PL_compiling.cop_line);
3280af22 2709 PL_compiling.cop_line = 0;
a0d0e21e
LW
2710
2711 PUTBACK;
0f15f207 2712#ifdef USE_THREADS
533c011a
NIS
2713 MUTEX_LOCK(&PL_eval_mutex);
2714 if (PL_eval_owner && PL_eval_owner != thr)
2715 while (PL_eval_owner)
2716 COND_WAIT(&PL_eval_cond, &PL_eval_mutex);
2717 PL_eval_owner = thr;
2718 MUTEX_UNLOCK(&PL_eval_mutex);
0f15f207 2719#endif /* USE_THREADS */
c277df42 2720 return DOCATCH(doeval(G_SCALAR, NULL));
a0d0e21e
LW
2721}
2722
2723PP(pp_dofile)
2724{
2725 return pp_require(ARGS);
2726}
2727
2728PP(pp_entereval)
2729{
4e35701f 2730 djSP;
c09156bb 2731 register PERL_CONTEXT *cx;
a0d0e21e 2732 dPOPss;
3280af22 2733 I32 gimme = GIMME_V, was = PL_sub_generation;
fc36a67e 2734 char tmpbuf[TYPE_DIGITS(long) + 12];
2735 char *safestr;
a0d0e21e 2736 STRLEN len;
55497cff 2737 OP *ret;
a0d0e21e
LW
2738
2739 if (!SvPV(sv,len) || !len)
2740 RETPUSHUNDEF;
748a9306 2741 TAINT_PROPER("eval");
a0d0e21e
LW
2742
2743 ENTER;
a0d0e21e 2744 lex_start(sv);
748a9306 2745 SAVETMPS;
a0d0e21e
LW
2746
2747 /* switch to eval mode */
2748
3280af22
NIS
2749 SAVESPTR(PL_compiling.cop_filegv);
2750 sprintf(tmpbuf, "_<(eval %lu)", (unsigned long)++PL_evalseq);
2751 PL_compiling.cop_filegv = gv_fetchfile(tmpbuf+2);
2752 PL_compiling.cop_line = 1;
55497cff 2753 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
2754 deleting the eval's FILEGV from the stash before gv_check() runs
2755 (i.e. before run-time proper). To work around the coredump that
2756 ensues, we always turn GvMULTI_on for any globals that were
2757 introduced within evals. See force_ident(). GSAR 96-10-12 */
2758 safestr = savepv(tmpbuf);
3280af22 2759 SAVEDELETE(PL_defstash, safestr, strlen(safestr));
b3ac6de7 2760 SAVEHINTS();
533c011a 2761 PL_hints = PL_op->op_targ;
a0d0e21e 2762
533c011a 2763 push_return(PL_op->op_next);
4e43f39e 2764 PUSHBLOCK(cx, (CXt_EVAL|CXp_REAL), SP);
6b88bc9c 2765 PUSHEVAL(cx, 0, PL_compiling.cop_filegv);
a0d0e21e
LW
2766
2767 /* prepare to compile string */
2768
3280af22
NIS
2769 if (PERLDB_LINE && PL_curstash != PL_debstash)
2770 save_lines(GvAV(PL_compiling.cop_filegv), PL_linestr);
a0d0e21e 2771 PUTBACK;
0f15f207 2772#ifdef USE_THREADS
533c011a
NIS
2773 MUTEX_LOCK(&PL_eval_mutex);
2774 if (PL_eval_owner && PL_eval_owner != thr)
2775 while (PL_eval_owner)
2776 COND_WAIT(&PL_eval_cond, &PL_eval_mutex);
2777 PL_eval_owner = thr;
2778 MUTEX_UNLOCK(&PL_eval_mutex);
0f15f207 2779#endif /* USE_THREADS */
c277df42 2780 ret = doeval(gimme, NULL);
3280af22 2781 if (PERLDB_INTER && was != PL_sub_generation /* Some subs defined here. */
533c011a 2782 && ret != PL_op->op_next) { /* Successive compilation. */
55497cff 2783 strcpy(safestr, "_<(eval )"); /* Anything fake and short. */
2784 }
1e422769 2785 return DOCATCH(ret);
a0d0e21e
LW
2786}
2787
2788PP(pp_leaveeval)
2789{
4e35701f 2790 djSP;
a0d0e21e
LW
2791 register SV **mark;
2792 SV **newsp;
2793 PMOP *newpm;
2794 I32 gimme;
c09156bb 2795 register PERL_CONTEXT *cx;
a0d0e21e 2796 OP *retop;
533c011a 2797 U8 save_flags = PL_op -> op_flags;
a0d0e21e
LW
2798 I32 optype;
2799
2800 POPBLOCK(cx,newpm);
2801 POPEVAL(cx);
2802 retop = pop_return();
2803
a1f49e72 2804 TAINT_NOT;
54310121 2805 if (gimme == G_VOID)
2806 MARK = newsp;
2807 else if (gimme == G_SCALAR) {
2808 MARK = newsp + 1;
2809 if (MARK <= SP) {
2810 if (SvFLAGS(TOPs) & SVs_TEMP)
2811 *MARK = TOPs;
2812 else
2813 *MARK = sv_mortalcopy(TOPs);
2814 }
a0d0e21e 2815 else {
54310121 2816 MEXTEND(mark,0);
3280af22 2817 *MARK = &PL_sv_undef;
a0d0e21e 2818 }
a0d0e21e
LW
2819 }
2820 else {
a1f49e72
CS
2821 /* in case LEAVE wipes old return values */
2822 for (mark = newsp + 1; mark <= SP; mark++) {
2823 if (!(SvFLAGS(*mark) & SVs_TEMP)) {
a0d0e21e 2824 *mark = sv_mortalcopy(*mark);
a1f49e72
CS
2825 TAINT_NOT; /* Each item is independent */
2826 }
2827 }
a0d0e21e 2828 }
3280af22 2829 PL_curpm = newpm; /* Don't pop $1 et al till now */
a0d0e21e 2830
84902520
TB
2831 /*
2832 * Closures mentioned at top level of eval cannot be referenced
2833 * again, and their presence indirectly causes a memory leak.
2834 * (Note that the fact that compcv and friends are still set here
2835 * is, AFAIK, an accident.) --Chip
2836 */
3280af22
NIS
2837 if (AvFILLp(PL_comppad_name) >= 0) {
2838 SV **svp = AvARRAY(PL_comppad_name);
84902520 2839 I32 ix;
3280af22 2840 for (ix = AvFILLp(PL_comppad_name); ix >= 0; ix--) {
84902520 2841 SV *sv = svp[ix];
3280af22 2842 if (sv && sv != &PL_sv_undef && *SvPVX(sv) == '&') {
84902520 2843 SvREFCNT_dec(sv);
3280af22 2844 svp[ix] = &PL_sv_undef;
84902520 2845
3280af22 2846 sv = PL_curpad[ix];
84902520
TB
2847 if (CvCLONE(sv)) {
2848 SvREFCNT_dec(CvOUTSIDE(sv));
2849 CvOUTSIDE(sv) = Nullcv;
2850 }
2851 else {
2852 SvREFCNT_dec(sv);
2853 sv = NEWSV(0,0);
2854 SvPADTMP_on(sv);
3280af22 2855 PL_curpad[ix] = sv;
84902520
TB
2856 }
2857 }
2858 }
2859 }
2860
4fdae800 2861#ifdef DEBUGGING
3280af22 2862 assert(CvDEPTH(PL_compcv) == 1);
4fdae800 2863#endif
3280af22 2864 CvDEPTH(PL_compcv) = 0;
f46d017c 2865 lex_end();
4fdae800 2866
1ce6579f 2867 if (optype == OP_REQUIRE &&
924508f0 2868 !(gimme == G_SCALAR ? SvTRUE(*SP) : SP > newsp))
54310121 2869 {
1ce6579f 2870 /* Unassume the success we assumed earlier. */
54310121 2871 char *name = cx->blk_eval.old_name;
3280af22 2872 (void)hv_delete(GvHVn(PL_incgv), name, strlen(name), G_DISCARD);
1ce6579f 2873 retop = die("%s did not return a true value", name);
f46d017c
GS
2874 /* die_where() did LEAVE, or we won't be here */
2875 }
2876 else {
2877 LEAVE;
2878 if (!(save_flags & OPf_SPECIAL))
2879 sv_setpv(ERRSV,"");
a0d0e21e 2880 }
a0d0e21e
LW
2881
2882 RETURNOP(retop);
2883}
2884
a0d0e21e
LW
2885PP(pp_entertry)
2886{
4e35701f 2887 djSP;
c09156bb 2888 register PERL_CONTEXT *cx;
54310121 2889 I32 gimme = GIMME_V;
a0d0e21e
LW
2890
2891 ENTER;
2892 SAVETMPS;
2893
2894 push_return(cLOGOP->op_other->op_next);
2895 PUSHBLOCK(cx, CXt_EVAL, SP);
2896 PUSHEVAL(cx, 0, 0);
533c011a 2897 PL_eval_root = PL_op; /* Only needed so that goto works right. */
a0d0e21e 2898
3280af22 2899 PL_in_eval = 1;
38a03e6e 2900 sv_setpv(ERRSV,"");
1e422769 2901 PUTBACK;
533c011a 2902 return DOCATCH(PL_op->op_next);
a0d0e21e
LW
2903}
2904
2905PP(pp_leavetry)
2906{
4e35701f 2907 djSP;
a0d0e21e
LW
2908 register SV **mark;
2909 SV **newsp;
2910 PMOP *newpm;
2911 I32 gimme;
c09156bb 2912 register PERL_CONTEXT *cx;
a0d0e21e
LW
2913 I32 optype;
2914
2915 POPBLOCK(cx,newpm);
2916 POPEVAL(cx);
2917 pop_return();
2918
a1f49e72 2919 TAINT_NOT;
54310121 2920 if (gimme == G_VOID)
2921 SP = newsp;
2922 else if (gimme == G_SCALAR) {
2923 MARK = newsp + 1;
2924 if (MARK <= SP) {
2925 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
2926 *MARK = TOPs;
2927 else
2928 *MARK = sv_mortalcopy(TOPs);
2929 }
a0d0e21e 2930 else {
54310121 2931 MEXTEND(mark,0);
3280af22 2932 *MARK = &PL_sv_undef;
a0d0e21e
LW
2933 }
2934 SP = MARK;
2935 }
2936 else {
a1f49e72
CS
2937 /* in case LEAVE wipes old return values */
2938 for (mark = newsp + 1; mark <= SP; mark++) {
2939 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
a0d0e21e 2940 *mark = sv_mortalcopy(*mark);
a1f49e72
CS
2941 TAINT_NOT; /* Each item is independent */
2942 }
2943 }
a0d0e21e 2944 }
3280af22 2945 PL_curpm = newpm; /* Don't pop $1 et al till now */
a0d0e21e
LW
2946
2947 LEAVE;
38a03e6e 2948 sv_setpv(ERRSV,"");
a0d0e21e
LW
2949 RETURN;
2950}
2951
0824fdcb 2952STATIC void
8ac85365 2953doparseform(SV *sv)
a0d0e21e
LW
2954{
2955 STRLEN len;
2956 register char *s = SvPV_force(sv, len);
2957 register char *send = s + len;
2958 register char *base;
2959 register I32 skipspaces = 0;
2960 bool noblank;
2961 bool repeat;
2962 bool postspace = FALSE;
2963 U16 *fops;
2964 register U16 *fpc;
2965 U16 *linepc;
2966 register I32 arg;
2967 bool ischop;
2968
55497cff 2969 if (len == 0)
bbce6d69 2970 croak("Null picture in formline");
55497cff 2971
2972 New(804, fops, (send - s)*3+10, U16); /* Almost certainly too long... */
a0d0e21e
LW
2973 fpc = fops;
2974
2975 if (s < send) {
2976 linepc = fpc;
2977 *fpc++ = FF_LINEMARK;
2978 noblank = repeat = FALSE;
2979 base = s;
2980 }
2981
2982 while (s <= send) {
2983 switch (*s++) {
2984 default:
2985 skipspaces = 0;
2986 continue;
2987
2988 case '~':
2989 if (*s == '~') {
2990 repeat = TRUE;
2991 *s = ' ';
2992 }
2993 noblank = TRUE;
2994 s[-1] = ' ';
2995 /* FALL THROUGH */
2996 case ' ': case '\t':
2997 skipspaces++;
2998 continue;
2999
3000 case '\n': case 0:
3001 arg = s - base;
3002 skipspaces++;
3003 arg -= skipspaces;
3004 if (arg) {
5f05dabc 3005 if (postspace)
a0d0e21e 3006 *fpc++ = FF_SPACE;
a0d0e21e
LW
3007 *fpc++ = FF_LITERAL;
3008 *fpc++ = arg;
3009 }
5f05dabc 3010 postspace = FALSE;
a0d0e21e
LW
3011 if (s <= send)
3012 skipspaces--;
3013 if (skipspaces) {
3014 *fpc++ = FF_SKIP;
3015 *fpc++ = skipspaces;
3016 }
3017 skipspaces = 0;
3018 if (s <= send)
3019 *fpc++ = FF_NEWLINE;
3020 if (noblank) {
3021 *fpc++ = FF_BLANK;
3022 if (repeat)
3023 arg = fpc - linepc + 1;
3024 else
3025 arg = 0;
3026 *fpc++ = arg;
3027 }
3028 if (s < send) {
3029 linepc = fpc;
3030 *fpc++ = FF_LINEMARK;
3031 noblank = repeat = FALSE;
3032 base = s;
3033 }
3034 else
3035 s++;
3036 continue;
3037
3038 case '@':
3039 case '^':
3040 ischop = s[-1] == '^';
3041
3042 if (postspace) {
3043 *fpc++ = FF_SPACE;
3044 postspace = FALSE;
3045 }
3046 arg = (s - base) - 1;
3047 if (arg) {
3048 *fpc++ = FF_LITERAL;
3049 *fpc++ = arg;
3050 }
3051
3052 base = s - 1;
3053 *fpc++ = FF_FETCH;
3054 if (*s == '*') {
3055 s++;
3056 *fpc++ = 0;
3057 *fpc++ = FF_LINEGLOB;
3058 }
3059 else if (*s == '#' || (*s == '.' && s[1] == '#')) {
3060 arg = ischop ? 512 : 0;
3061 base = s - 1;
3062 while (*s == '#')
3063 s++;
3064 if (*s == '.') {
3065 char *f;
3066 s++;
3067 f = s;
3068 while (*s == '#')
3069 s++;
3070 arg |= 256 + (s - f);
3071 }
3072 *fpc++ = s - base; /* fieldsize for FETCH */
3073 *fpc++ = FF_DECIMAL;
3074 *fpc++ = arg;
3075 }
3076 else {
3077 I32 prespace = 0;
3078 bool ismore = FALSE;
3079
3080 if (*s == '>') {
3081 while (*++s == '>') ;
3082 prespace = FF_SPACE;
3083 }
3084 else if (*s == '|') {
3085 while (*++s == '|') ;
3086 prespace = FF_HALFSPACE;
3087 postspace = TRUE;
3088 }
3089 else {
3090 if (*s == '<')
3091 while (*++s == '<') ;
3092 postspace = TRUE;
3093 }
3094 if (*s == '.' && s[1] == '.' && s[2] == '.') {
3095 s += 3;
3096 ismore = TRUE;
3097 }
3098 *fpc++ = s - base; /* fieldsize for FETCH */
3099
3100 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
3101
3102 if (prespace)
3103 *fpc++ = prespace;
3104 *fpc++ = FF_ITEM;
3105 if (ismore)
3106 *fpc++ = FF_MORE;
3107 if (ischop)
3108 *fpc++ = FF_CHOP;
3109 }
3110 base = s;
3111 skipspaces = 0;
3112 continue;
3113 }
3114 }
3115 *fpc++ = FF_END;
3116
3117 arg = fpc - fops;
3118 { /* need to jump to the next word */
3119 int z;
3120 z = WORD_ALIGN - SvCUR(sv) % WORD_ALIGN;
3121 SvGROW(sv, SvCUR(sv) + z + arg * sizeof(U16) + 4);
3122 s = SvPVX(sv) + SvCUR(sv) + z;
3123 }
3124 Copy(fops, s, arg, U16);
3125 Safefree(fops);
55497cff 3126 sv_magic(sv, Nullsv, 'f', Nullch, 0);
a0d0e21e
LW
3127 SvCOMPILED_on(sv);
3128}
4e35701f 3129
745d3a65
HM
3130/*
3131 * The rest of this file was derived from source code contributed
3132 * by Tom Horsley.
3133 *
3134 * NOTE: this code was derived from Tom Horsley's qsort replacement
3135 * and should not be confused with the original code.
3136 */
3137
3138/* Copyright (C) Tom Horsley, 1997. All rights reserved.
3139
3140 Permission granted to distribute under the same terms as perl which are
3141 (briefly):
3142
3143 This program is free software; you can redistribute it and/or modify
3144 it under the terms of either:
3145
3146 a) the GNU General Public License as published by the Free
3147 Software Foundation; either version 1, or (at your option) any
3148 later version, or
3149
3150 b) the "Artistic License" which comes with this Kit.
3151
3152 Details on the perl license can be found in the perl source code which
3153 may be located via the www.perl.com web page.
3154
3155 This is the most wonderfulest possible qsort I can come up with (and
3156 still be mostly portable) My (limited) tests indicate it consistently
3157 does about 20% fewer calls to compare than does the qsort in the Visual
3158 C++ library, other vendors may vary.
3159
3160 Some of the ideas in here can be found in "Algorithms" by Sedgewick,
3161 others I invented myself (or more likely re-invented since they seemed
3162 pretty obvious once I watched the algorithm operate for a while).
3163
3164 Most of this code was written while watching the Marlins sweep the Giants
3165 in the 1997 National League Playoffs - no Braves fans allowed to use this
3166 code (just kidding :-).
3167
3168 I realize that if I wanted to be true to the perl tradition, the only
3169 comment in this file would be something like:
3170
3171 ...they shuffled back towards the rear of the line. 'No, not at the
3172 rear!' the slave-driver shouted. 'Three files up. And stay there...
3173
3174 However, I really needed to violate that tradition just so I could keep
3175 track of what happens myself, not to mention some poor fool trying to
3176 understand this years from now :-).
3177*/
3178
3179/* ********************************************************** Configuration */
3180
3181#ifndef QSORT_ORDER_GUESS
3182#define QSORT_ORDER_GUESS 2 /* Select doubling version of the netBSD trick */
3183#endif
3184
3185/* QSORT_MAX_STACK is the largest number of partitions that can be stacked up for
3186 future processing - a good max upper bound is log base 2 of memory size
3187 (32 on 32 bit machines, 64 on 64 bit machines, etc). In reality can
3188 safely be smaller than that since the program is taking up some space and
3189 most operating systems only let you grab some subset of contiguous
3190 memory (not to mention that you are normally sorting data larger than
3191 1 byte element size :-).
3192*/
3193#ifndef QSORT_MAX_STACK
3194#define QSORT_MAX_STACK 32
3195#endif
3196
3197/* QSORT_BREAK_EVEN is the size of the largest partition we should insertion sort.
3198 Anything bigger and we use qsort. If you make this too small, the qsort
3199 will probably break (or become less efficient), because it doesn't expect
3200 the middle element of a partition to be the same as the right or left -
3201 you have been warned).
3202*/
3203#ifndef QSORT_BREAK_EVEN
3204#define QSORT_BREAK_EVEN 6
3205#endif
3206
3207/* ************************************************************* Data Types */
3208
3209/* hold left and right index values of a partition waiting to be sorted (the
3210 partition includes both left and right - right is NOT one past the end or
3211 anything like that).
3212*/
3213struct partition_stack_entry {
3214 int left;
3215 int right;
3216#ifdef QSORT_ORDER_GUESS
3217 int qsort_break_even;
3218#endif
3219};
3220
3221/* ******************************************************* Shorthand Macros */
3222
3223/* Note that these macros will be used from inside the qsort function where
3224 we happen to know that the variable 'elt_size' contains the size of an
3225 array element and the variable 'temp' points to enough space to hold a
3226 temp element and the variable 'array' points to the array being sorted
3227 and 'compare' is the pointer to the compare routine.
3228
3229 Also note that there are very many highly architecture specific ways
3230 these might be sped up, but this is simply the most generally portable
3231 code I could think of.
3232*/
161b471a 3233
745d3a65
HM
3234/* Return < 0 == 0 or > 0 as the value of elt1 is < elt2, == elt2, > elt2
3235*/
565764a8
DL
3236#ifdef PERL_OBJECT
3237#define qsort_cmp(elt1, elt2) \
3238 ((this->*compare)(array[elt1], array[elt2]))
3239#else
745d3a65
HM
3240#define qsort_cmp(elt1, elt2) \
3241 ((*compare)(array[elt1], array[elt2]))
565764a8 3242#endif
745d3a65
HM
3243
3244#ifdef QSORT_ORDER_GUESS
3245#define QSORT_NOTICE_SWAP swapped++;
3246#else
3247#define QSORT_NOTICE_SWAP
3248#endif
3249
3250/* swaps contents of array elements elt1, elt2.
3251*/
3252#define qsort_swap(elt1, elt2) \
3253 STMT_START { \
3254 QSORT_NOTICE_SWAP \
3255 temp = array[elt1]; \
3256 array[elt1] = array[elt2]; \
3257 array[elt2] = temp; \
3258 } STMT_END
3259
3260/* rotate contents of elt1, elt2, elt3 such that elt1 gets elt2, elt2 gets
3261 elt3 and elt3 gets elt1.
3262*/
3263#define qsort_rotate(elt1, elt2, elt3) \
3264 STMT_START { \
3265 QSORT_NOTICE_SWAP \
3266 temp = array[elt1]; \
3267 array[elt1] = array[elt2]; \
3268 array[elt2] = array[elt3]; \
3269 array[elt3] = temp; \
3270 } STMT_END
3271
3272/* ************************************************************ Debug stuff */
3273
3274#ifdef QSORT_DEBUG
3275
3276static void
3277break_here()
3278{
3279 return; /* good place to set a breakpoint */
3280}
3281
3282#define qsort_assert(t) (void)( (t) || (break_here(), 0) )
3283
3284static void
3285doqsort_all_asserts(
3286 void * array,
3287 size_t num_elts,
3288 size_t elt_size,
3289 int (*compare)(const void * elt1, const void * elt2),
3290 int pc_left, int pc_right, int u_left, int u_right)
3291{
3292 int i;
3293
3294 qsort_assert(pc_left <= pc_right);
3295 qsort_assert(u_right < pc_left);
3296 qsort_assert(pc_right < u_left);
3297 for (i = u_right + 1; i < pc_left; ++i) {
3298 qsort_assert(qsort_cmp(i, pc_left) < 0);
3299 }
3300 for (i = pc_left; i < pc_right; ++i) {
3301 qsort_assert(qsort_cmp(i, pc_right) == 0);
3302 }
3303 for (i = pc_right + 1; i < u_left; ++i) {
3304 qsort_assert(qsort_cmp(pc_right, i) < 0);
3305 }
3306}
3307
3308#define qsort_all_asserts(PC_LEFT, PC_RIGHT, U_LEFT, U_RIGHT) \
3309 doqsort_all_asserts(array, num_elts, elt_size, compare, \
3310 PC_LEFT, PC_RIGHT, U_LEFT, U_RIGHT)
3311
3312#else
3313
3314#define qsort_assert(t) ((void)0)
3315
3316#define qsort_all_asserts(PC_LEFT, PC_RIGHT, U_LEFT, U_RIGHT) ((void)0)
3317
3318#endif
3319
3320/* ****************************************************************** qsort */
3321
6cc33c6d 3322STATIC void
565764a8
DL
3323#ifdef PERL_OBJECT
3324qsortsv(SV ** array, size_t num_elts, SVCOMPARE compare)
3325#else
745d3a65
HM
3326qsortsv(
3327 SV ** array,
3328 size_t num_elts,
3329 I32 (*compare)(SV *a, SV *b))
565764a8 3330#endif
745d3a65
HM
3331{
3332 register SV * temp;
3333
3334 struct partition_stack_entry partition_stack[QSORT_MAX_STACK];
3335 int next_stack_entry = 0;
3336
3337 int part_left;
3338 int part_right;
3339#ifdef QSORT_ORDER_GUESS
3340 int qsort_break_even;
3341 int swapped;
3342#endif
161b471a 3343
745d3a65
HM
3344 /* Make sure we actually have work to do.
3345 */
3346 if (num_elts <= 1) {
3347 return;
3348 }
3349
3350 /* Setup the initial partition definition and fall into the sorting loop
3351 */
3352 part_left = 0;
3353 part_right = (int)(num_elts - 1);
3354#ifdef QSORT_ORDER_GUESS
3355 qsort_break_even = QSORT_BREAK_EVEN;
3356#else
3357#define qsort_break_even QSORT_BREAK_EVEN
3358#endif
3359 for ( ; ; ) {
3360 if ((part_right - part_left) >= qsort_break_even) {
3361 /* OK, this is gonna get hairy, so lets try to document all the
3362 concepts and abbreviations and variables and what they keep
3363 track of:
3364
3365 pc: pivot chunk - the set of array elements we accumulate in the
3366 middle of the partition, all equal in value to the original
3367 pivot element selected. The pc is defined by:
3368
3369 pc_left - the leftmost array index of the pc
3370 pc_right - the rightmost array index of the pc
3371
3372 we start with pc_left == pc_right and only one element
3373 in the pivot chunk (but it can grow during the scan).
3374
3375 u: uncompared elements - the set of elements in the partition
3376 we have not yet compared to the pivot value. There are two
3377 uncompared sets during the scan - one to the left of the pc
3378 and one to the right.
3379
3380 u_right - the rightmost index of the left side's uncompared set
3381 u_left - the leftmost index of the right side's uncompared set
3382
3383 The leftmost index of the left sides's uncompared set
3384 doesn't need its own variable because it is always defined
3385 by the leftmost edge of the whole partition (part_left). The
3386 same goes for the rightmost edge of the right partition
3387 (part_right).
3388
3389 We know there are no uncompared elements on the left once we
3390 get u_right < part_left and no uncompared elements on the
3391 right once u_left > part_right. When both these conditions
3392 are met, we have completed the scan of the partition.
3393
3394 Any elements which are between the pivot chunk and the
3395 uncompared elements should be less than the pivot value on
3396 the left side and greater than the pivot value on the right
3397 side (in fact, the goal of the whole algorithm is to arrange
3398 for that to be true and make the groups of less-than and
3399 greater-then elements into new partitions to sort again).
3400
3401 As you marvel at the complexity of the code and wonder why it
3402 has to be so confusing. Consider some of the things this level
3403 of confusion brings:
3404
3405 Once I do a compare, I squeeze every ounce of juice out of it. I
3406 never do compare calls I don't have to do, and I certainly never
3407 do redundant calls.
3408
3409 I also never swap any elements unless I can prove there is a
3410 good reason. Many sort algorithms will swap a known value with
3411 an uncompared value just to get things in the right place (or
3412 avoid complexity :-), but that uncompared value, once it gets
3413 compared, may then have to be swapped again. A lot of the
3414 complexity of this code is due to the fact that it never swaps
3415 anything except compared values, and it only swaps them when the
3416 compare shows they are out of position.
3417 */
3418 int pc_left, pc_right;
3419 int u_right, u_left;
3420
3421 int s;
3422
3423 pc_left = ((part_left + part_right) / 2);
3424 pc_right = pc_left;
3425 u_right = pc_left - 1;
3426 u_left = pc_right + 1;
3427
3428 /* Qsort works best when the pivot value is also the median value
3429 in the partition (unfortunately you can't find the median value
3430 without first sorting :-), so to give the algorithm a helping
3431 hand, we pick 3 elements and sort them and use the median value
3432 of that tiny set as the pivot value.
3433
3434 Some versions of qsort like to use the left middle and right as
3435 the 3 elements to sort so they can insure the ends of the
3436 partition will contain values which will stop the scan in the
3437 compare loop, but when you have to call an arbitrarily complex
3438 routine to do a compare, its really better to just keep track of
3439 array index values to know when you hit the edge of the
3440 partition and avoid the extra compare. An even better reason to
3441 avoid using a compare call is the fact that you can drop off the
3442 edge of the array if someone foolishly provides you with an
3443 unstable compare function that doesn't always provide consistent
3444 results.
3445
3446 So, since it is simpler for us to compare the three adjacent
3447 elements in the middle of the partition, those are the ones we
3448 pick here (conveniently pointed at by u_right, pc_left, and
3449 u_left). The values of the left, center, and right elements
3450 are refered to as l c and r in the following comments.
3451 */
3452
3453#ifdef QSORT_ORDER_GUESS
3454 swapped = 0;
3455#endif
3456 s = qsort_cmp(u_right, pc_left);
3457 if (s < 0) {
3458 /* l < c */
3459 s = qsort_cmp(pc_left, u_left);
3460 /* if l < c, c < r - already in order - nothing to do */
3461 if (s == 0) {
3462 /* l < c, c == r - already in order, pc grows */
3463 ++pc_right;
3464 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3465 } else if (s > 0) {
3466 /* l < c, c > r - need to know more */
3467 s = qsort_cmp(u_right, u_left);
3468 if (s < 0) {
3469 /* l < c, c > r, l < r - swap c & r to get ordered */
3470 qsort_swap(pc_left, u_left);
3471 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3472 } else if (s == 0) {
3473 /* l < c, c > r, l == r - swap c&r, grow pc */
3474 qsort_swap(pc_left, u_left);
3475 --pc_left;
3476 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3477 } else {
3478 /* l < c, c > r, l > r - make lcr into rlc to get ordered */
3479 qsort_rotate(pc_left, u_right, u_left);
3480 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3481 }
3482 }
3483 } else if (s == 0) {
3484 /* l == c */
3485 s = qsort_cmp(pc_left, u_left);
3486 if (s < 0) {
3487 /* l == c, c < r - already in order, grow pc */
3488 --pc_left;
3489 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3490 } else if (s == 0) {
3491 /* l == c, c == r - already in order, grow pc both ways */
3492 --pc_left;
3493 ++pc_right;
3494 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3495 } else {
3496 /* l == c, c > r - swap l & r, grow pc */
3497 qsort_swap(u_right, u_left);
3498 ++pc_right;
3499 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3500 }
3501 } else {
3502 /* l > c */
3503 s = qsort_cmp(pc_left, u_left);
3504 if (s < 0) {
3505 /* l > c, c < r - need to know more */
3506 s = qsort_cmp(u_right, u_left);
3507 if (s < 0) {
3508 /* l > c, c < r, l < r - swap l & c to get ordered */
3509 qsort_swap(u_right, pc_left);
3510 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3511 } else if (s == 0) {
3512 /* l > c, c < r, l == r - swap l & c, grow pc */
3513 qsort_swap(u_right, pc_left);
3514 ++pc_right;
3515 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3516 } else {
3517 /* l > c, c < r, l > r - rotate lcr into crl to order */
3518 qsort_rotate(u_right, pc_left, u_left);
3519 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3520 }
3521 } else if (s == 0) {
3522 /* l > c, c == r - swap ends, grow pc */
3523 qsort_swap(u_right, u_left);
3524 --pc_left;
3525 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3526 } else {
3527 /* l > c, c > r - swap ends to get in order */
3528 qsort_swap(u_right, u_left);
3529 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3530 }
3531 }
3532 /* We now know the 3 middle elements have been compared and
3533 arranged in the desired order, so we can shrink the uncompared
3534 sets on both sides
3535 */
3536 --u_right;
3537 ++u_left;
3538 qsort_all_asserts(pc_left, pc_right, u_left, u_right);
3539
3540 /* The above massive nested if was the simple part :-). We now have
3541 the middle 3 elements ordered and we need to scan through the
3542 uncompared sets on either side, swapping elements that are on
3543 the wrong side or simply shuffling equal elements around to get
3544 all equal elements into the pivot chunk.
3545 */
3546
3547 for ( ; ; ) {
3548 int still_work_on_left;
3549 int still_work_on_right;
3550
3551 /* Scan the uncompared values on the left. If I find a value
3552 equal to the pivot value, move it over so it is adjacent to
3553 the pivot chunk and expand the pivot chunk. If I find a value
3554 less than the pivot value, then just leave it - its already
3555 on the correct side of the partition. If I find a greater
3556 value, then stop the scan.
3557 */
3558 while (still_work_on_left = (u_right >= part_left)) {
3559 s = qsort_cmp(u_right, pc_left);
3560 if (s < 0) {
3561 --u_right;
3562 } else if (s == 0) {
3563 --pc_left;
3564 if (pc_left != u_right) {
3565 qsort_swap(u_right, pc_left);
3566 }
3567 --u_right;
3568 } else {
3569 break;
3570 }
3571 qsort_assert(u_right < pc_left);
3572 qsort_assert(pc_left <= pc_right);
3573 qsort_assert(qsort_cmp(u_right + 1, pc_left) <= 0);
3574 qsort_assert(qsort_cmp(pc_left, pc_right) == 0);
3575 }
3576
3577 /* Do a mirror image scan of uncompared values on the right
3578 */
3579 while (still_work_on_right = (u_left <= part_right)) {
3580 s = qsort_cmp(pc_right, u_left);
3581 if (s < 0) {
3582 ++u_left;
3583 } else if (s == 0) {
3584 ++pc_right;
3585 if (pc_right != u_left) {
3586 qsort_swap(pc_right, u_left);
3587 }
3588 ++u_left;
3589 } else {
3590 break;
3591 }
3592 qsort_assert(u_left > pc_right);
3593 qsort_assert(pc_left <= pc_right);
3594 qsort_assert(qsort_cmp(pc_right, u_left - 1) <= 0);
3595 qsort_assert(qsort_cmp(pc_left, pc_right) == 0);
3596 }
3597
3598 if (still_work_on_left) {
3599 /* I know I have a value on the left side which needs to be
3600 on the right side, but I need to know more to decide
3601 exactly the best thing to do with it.
3602 */
3603 if (still_work_on_right) {
3604 /* I know I have values on both side which are out of
3605 position. This is a big win because I kill two birds
3606 with one swap (so to speak). I can advance the
3607 uncompared pointers on both sides after swapping both
3608 of them into the right place.
3609 */
3610 qsort_swap(u_right, u_left);
3611 --u_right;
3612 ++u_left;
3613 qsort_all_asserts(pc_left, pc_right, u_left, u_right);
3614 } else {
3615 /* I have an out of position value on the left, but the
3616 right is fully scanned, so I "slide" the pivot chunk
3617 and any less-than values left one to make room for the
3618 greater value over on the right. If the out of position
3619 value is immediately adjacent to the pivot chunk (there
3620 are no less-than values), I can do that with a swap,
3621 otherwise, I have to rotate one of the less than values
3622 into the former position of the out of position value
3623 and the right end of the pivot chunk into the left end
3624 (got all that?).
3625 */
3626 --pc_left;
3627 if (pc_left == u_right) {
3628 qsort_swap(u_right, pc_right);
3629 qsort_all_asserts(pc_left, pc_right-1, u_left, u_right-1);
3630 } else {
3631 qsort_rotate(u_right, pc_left, pc_right);
3632 qsort_all_asserts(pc_left, pc_right-1, u_left, u_right-1);
3633 }
3634 --pc_right;
3635 --u_right;
3636 }
3637 } else if (still_work_on_right) {
3638 /* Mirror image of complex case above: I have an out of
3639 position value on the right, but the left is fully
3640 scanned, so I need to shuffle things around to make room
3641 for the right value on the left.
3642 */
3643 ++pc_right;
3644 if (pc_right == u_left) {
3645 qsort_swap(u_left, pc_left);
3646 qsort_all_asserts(pc_left+1, pc_right, u_left+1, u_right);
3647 } else {
3648 qsort_rotate(pc_right, pc_left, u_left);
3649 qsort_all_asserts(pc_left+1, pc_right, u_left+1, u_right);
3650 }
3651 ++pc_left;
3652 ++u_left;
3653 } else {
3654 /* No more scanning required on either side of partition,
3655 break out of loop and figure out next set of partitions
3656 */
3657 break;
3658 }
3659 }
3660
3661 /* The elements in the pivot chunk are now in the right place. They
3662 will never move or be compared again. All I have to do is decide
3663 what to do with the stuff to the left and right of the pivot
3664 chunk.
3665
3666 Notes on the QSORT_ORDER_GUESS ifdef code:
3667
3668 1. If I just built these partitions without swapping any (or
3669 very many) elements, there is a chance that the elements are
3670 already ordered properly (being properly ordered will
3671 certainly result in no swapping, but the converse can't be
3672 proved :-).
3673
3674 2. A (properly written) insertion sort will run faster on
3675 already ordered data than qsort will.
3676
3677 3. Perhaps there is some way to make a good guess about
3678 switching to an insertion sort earlier than partition size 6
3679 (for instance - we could save the partition size on the stack
3680 and increase the size each time we find we didn't swap, thus
3681 switching to insertion sort earlier for partitions with a
3682 history of not swapping).
3683
3684 4. Naturally, if I just switch right away, it will make
3685 artificial benchmarks with pure ascending (or descending)
3686 data look really good, but is that a good reason in general?
3687 Hard to say...
3688 */
3689
3690#ifdef QSORT_ORDER_GUESS
3691 if (swapped < 3) {
3692#if QSORT_ORDER_GUESS == 1
3693 qsort_break_even = (part_right - part_left) + 1;
3694#endif
3695#if QSORT_ORDER_GUESS == 2
3696 qsort_break_even *= 2;
3697#endif
3698#if QSORT_ORDER_GUESS == 3
3699 int prev_break = qsort_break_even;
3700 qsort_break_even *= qsort_break_even;
3701 if (qsort_break_even < prev_break) {
3702 qsort_break_even = (part_right - part_left) + 1;
3703 }
3704#endif
3705 } else {
3706 qsort_break_even = QSORT_BREAK_EVEN;
3707 }
3708#endif
3709
3710 if (part_left < pc_left) {
3711 /* There are elements on the left which need more processing.
3712 Check the right as well before deciding what to do.
3713 */
3714 if (pc_right < part_right) {
3715 /* We have two partitions to be sorted. Stack the biggest one
3716 and process the smallest one on the next iteration. This
3717 minimizes the stack height by insuring that any additional
3718 stack entries must come from the smallest partition which
3719 (because it is smallest) will have the fewest
3720 opportunities to generate additional stack entries.
3721 */
3722 if ((part_right - pc_right) > (pc_left - part_left)) {
3723 /* stack the right partition, process the left */
3724 partition_stack[next_stack_entry].left = pc_right + 1;
3725 partition_stack[next_stack_entry].right = part_right;
3726#ifdef QSORT_ORDER_GUESS
3727 partition_stack[next_stack_entry].qsort_break_even = qsort_break_even;
3728#endif
3729 part_right = pc_left - 1;
3730 } else {
3731 /* stack the left partition, process the right */
3732 partition_stack[next_stack_entry].left = part_left;
3733 partition_stack[next_stack_entry].right = pc_left - 1;
3734#ifdef QSORT_ORDER_GUESS
3735 partition_stack[next_stack_entry].qsort_break_even = qsort_break_even;
3736#endif
3737 part_left = pc_right + 1;
3738 }
3739 qsort_assert(next_stack_entry < QSORT_MAX_STACK);
3740 ++next_stack_entry;
3741 } else {
3742 /* The elements on the left are the only remaining elements
3743 that need sorting, arrange for them to be processed as the
3744 next partition.
3745 */
3746 part_right = pc_left - 1;
3747 }
3748 } else if (pc_right < part_right) {
3749 /* There is only one chunk on the right to be sorted, make it
3750 the new partition and loop back around.
3751 */
3752 part_left = pc_right + 1;
3753 } else {
3754 /* This whole partition wound up in the pivot chunk, so
3755 we need to get a new partition off the stack.
3756 */
3757 if (next_stack_entry == 0) {
3758 /* the stack is empty - we are done */
3759 break;
3760 }
3761 --next_stack_entry;
3762 part_left = partition_stack[next_stack_entry].left;
3763 part_right = partition_stack[next_stack_entry].right;
3764#ifdef QSORT_ORDER_GUESS
3765 qsort_break_even = partition_stack[next_stack_entry].qsort_break_even;
3766#endif
3767 }
3768 } else {
3769 /* This partition is too small to fool with qsort complexity, just
3770 do an ordinary insertion sort to minimize overhead.
3771 */
3772 int i;
3773 /* Assume 1st element is in right place already, and start checking
3774 at 2nd element to see where it should be inserted.
3775 */
3776 for (i = part_left + 1; i <= part_right; ++i) {
3777 int j;
3778 /* Scan (backwards - just in case 'i' is already in right place)
3779 through the elements already sorted to see if the ith element
3780 belongs ahead of one of them.
3781 */
3782 for (j = i - 1; j >= part_left; --j) {
3783 if (qsort_cmp(i, j) >= 0) {
3784 /* i belongs right after j
3785 */
3786 break;
3787 }
3788 }
3789 ++j;
3790 if (j != i) {
3791 /* Looks like we really need to move some things
3792 */
b6917549 3793 int k;
745d3a65 3794 temp = array[i];
b6917549
HM
3795 for (k = i - 1; k >= j; --k)
3796 array[k + 1] = array[k];
745d3a65
HM
3797 array[j] = temp;
3798 }
3799 }
3800
3801 /* That partition is now sorted, grab the next one, or get out
3802 of the loop if there aren't any more.
3803 */
3804
3805 if (next_stack_entry == 0) {
3806 /* the stack is empty - we are done */
3807 break;
3808 }
3809 --next_stack_entry;
3810 part_left = partition_stack[next_stack_entry].left;
3811 part_right = partition_stack[next_stack_entry].right;
3812#ifdef QSORT_ORDER_GUESS
3813 qsort_break_even = partition_stack[next_stack_entry].qsort_break_even;
3814#endif
3815 }
3816 }
3817
3818 /* Believe it or not, the array is sorted at this point! */
3819}