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