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