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