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