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