-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpp_ctl.c
4962 lines (4476 loc) · 120 KB
/
pp_ctl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* pp_ctl.c
*
* Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
* 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
*/
/*
* Now far ahead the Road has gone,
* And I must follow, if I can,
* Pursuing it with eager feet,
* Until it joins some larger way
* Where many paths and errands meet.
* And whither then? I cannot say.
*
* [Bilbo on p.35 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
*/
/* This file contains control-oriented pp ("push/pop") functions that
* execute the opcodes that make up a perl program. A typical pp function
* expects to find its arguments on the stack, and usually pushes its
* results onto the stack, hence the 'pp' terminology. Each OP structure
* contains a pointer to the relevant pp_foo() function.
*
* Control-oriented means things like pp_enteriter() and pp_next(), which
* alter the flow of control of the program.
*/
#include "EXTERN.h"
#define PERL_IN_PP_CTL_C
#include "perl.h"
#ifndef WORD_ALIGN
#define WORD_ALIGN sizeof(U32)
#endif
#define DOCATCH(o) ((CATCH_GET == TRUE) ? docatch(o) : (o))
#define dopoptosub(plop) dopoptosub_at(cxstack, (plop))
PP(pp_wantarray)
{
dVAR;
dSP;
I32 cxix;
EXTEND(SP, 1);
cxix = dopoptosub(cxstack_ix);
if (cxix < 0)
RETPUSHUNDEF;
switch (cxstack[cxix].blk_gimme) {
case G_ARRAY:
RETPUSHYES;
case G_SCALAR:
RETPUSHNO;
default:
RETPUSHUNDEF;
}
}
PP(pp_regcreset)
{
dVAR;
/* XXXX Should store the old value to allow for tie/overload - and
restore in regcomp, where marked with XXXX. */
PL_reginterp_cnt = 0;
TAINT_NOT;
return NORMAL;
}
PP(pp_regcomp)
{
dVAR;
dSP;
register PMOP *pm = (PMOP*)cLOGOP->op_other;
SV *tmpstr;
MAGIC *mg = NULL;
REGEXP * re;
/* prevent recompiling under /o and ithreads. */
#if defined(USE_ITHREADS)
if (pm->op_pmflags & PMf_KEEP && PM_GETRE(pm)) {
if (PL_op->op_flags & OPf_STACKED) {
dMARK;
SP = MARK;
}
else
(void)POPs;
RETURN;
}
#endif
if (PL_op->op_flags & OPf_STACKED) {
/* multiple args; concatentate them */
dMARK; dORIGMARK;
tmpstr = PAD_SV(ARGTARG);
sv_setpvs(tmpstr, "");
while (++MARK <= SP) {
if (PL_amagic_generation) {
SV *sv;
if ((SvAMAGIC(tmpstr) || SvAMAGIC(*MARK)) &&
(sv = amagic_call(tmpstr, *MARK, concat_amg, AMGf_assign)))
{
sv_setsv(tmpstr, sv);
continue;
}
}
sv_catsv(tmpstr, *MARK);
}
SvSETMAGIC(tmpstr);
SP = ORIGMARK;
}
else
tmpstr = POPs;
if (SvROK(tmpstr)) {
SV * const sv = SvRV(tmpstr);
if(SvMAGICAL(sv))
mg = mg_find(sv, PERL_MAGIC_qr);
}
if (mg) {
regexp * const re = reg_temp_copy((regexp *)mg->mg_obj);
ReREFCNT_dec(PM_GETRE(pm));
PM_SETRE(pm, re);
}
else {
STRLEN len;
const char *t = SvOK(tmpstr) ? SvPV_const(tmpstr, len) : "";
re = PM_GETRE(pm);
/* Check against the last compiled regexp. */
if (!re || !RX_PRECOMP(re) || RX_PRELEN(re) != (I32)len ||
memNE(RX_PRECOMP(re), t, len))
{
const regexp_engine *eng = re ? RX_ENGINE(re) : NULL;
U32 pm_flags = pm->op_pmflags & PMf_COMPILETIME;
if (re) {
ReREFCNT_dec(re);
PM_SETRE(pm, NULL); /* crucial if regcomp aborts */
} else if (PL_curcop->cop_hints_hash) {
SV *ptr = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash, 0,
"regcomp", 7, 0, 0);
if (ptr && SvIOK(ptr) && SvIV(ptr))
eng = INT2PTR(regexp_engine*,SvIV(ptr));
}
if (PL_op->op_flags & OPf_SPECIAL)
PL_reginterp_cnt = I32_MAX; /* Mark as safe. */
if (DO_UTF8(tmpstr))
pm_flags |= RXf_UTF8;
if (eng)
PM_SETRE(pm, CALLREGCOMP_ENG(eng, tmpstr, pm_flags));
else
PM_SETRE(pm, CALLREGCOMP(tmpstr, pm_flags));
PL_reginterp_cnt = 0; /* XXXX Be extra paranoid - needed
inside tie/overload accessors. */
}
}
re = PM_GETRE(pm);
#ifndef INCOMPLETE_TAINTS
if (PL_tainting) {
if (PL_tainted)
RX_EXTFLAGS(re) |= RXf_TAINTED;
else
RX_EXTFLAGS(re) &= ~RXf_TAINTED;
}
#endif
if (!RX_PRELEN(PM_GETRE(pm)) && PL_curpm)
pm = PL_curpm;
#if !defined(USE_ITHREADS)
/* can't change the optree at runtime either */
/* PMf_KEEP is handled differently under threads to avoid these problems */
if (pm->op_pmflags & PMf_KEEP) {
pm->op_private &= ~OPpRUNTIME; /* no point compiling again */
cLOGOP->op_first->op_next = PL_op->op_next;
}
#endif
RETURN;
}
PP(pp_substcont)
{
dVAR;
dSP;
register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
register PMOP * const pm = (PMOP*) cLOGOP->op_other;
register SV * const dstr = cx->sb_dstr;
register char *s = cx->sb_s;
register char *m = cx->sb_m;
char *orig = cx->sb_orig;
register REGEXP * const rx = cx->sb_rx;
SV *nsv = NULL;
REGEXP *old = PM_GETRE(pm);
if(old != rx) {
if(old)
ReREFCNT_dec(old);
PM_SETRE(pm,ReREFCNT_inc(rx));
}
rxres_restore(&cx->sb_rxres, rx);
RX_MATCH_UTF8_set(rx, DO_UTF8(cx->sb_targ));
if (cx->sb_iters++) {
const I32 saviters = cx->sb_iters;
if (cx->sb_iters > cx->sb_maxiters)
DIE(aTHX_ "Substitution loop");
if (!(cx->sb_rxtainted & 2) && SvTAINTED(TOPs))
cx->sb_rxtainted |= 2;
sv_catsv(dstr, POPs);
/* Are we done */
if (CxONCE(cx) || !CALLREGEXEC(rx, s, cx->sb_strend, orig,
s == m, cx->sb_targ, NULL,
((cx->sb_rflags & REXEC_COPY_STR)
? (REXEC_IGNOREPOS|REXEC_NOT_FIRST)
: (REXEC_COPY_STR|REXEC_IGNOREPOS|REXEC_NOT_FIRST))))
{
SV * const targ = cx->sb_targ;
assert(cx->sb_strend >= s);
if(cx->sb_strend > s) {
if (DO_UTF8(dstr) && !SvUTF8(targ))
sv_catpvn_utf8_upgrade(dstr, s, cx->sb_strend - s, nsv);
else
sv_catpvn(dstr, s, cx->sb_strend - s);
}
cx->sb_rxtainted |= RX_MATCH_TAINTED(rx);
#ifdef PERL_OLD_COPY_ON_WRITE
if (SvIsCOW(targ)) {
sv_force_normal_flags(targ, SV_COW_DROP_PV);
} else
#endif
{
SvPV_free(targ);
}
SvPV_set(targ, SvPVX(dstr));
SvCUR_set(targ, SvCUR(dstr));
SvLEN_set(targ, SvLEN(dstr));
if (DO_UTF8(dstr))
SvUTF8_on(targ);
SvPV_set(dstr, NULL);
TAINT_IF(cx->sb_rxtainted & 1);
mPUSHi(saviters - 1);
(void)SvPOK_only_UTF8(targ);
TAINT_IF(cx->sb_rxtainted);
SvSETMAGIC(targ);
SvTAINT(targ);
LEAVE_SCOPE(cx->sb_oldsave);
POPSUBST(cx);
RETURNOP(pm->op_next);
}
cx->sb_iters = saviters;
}
if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
m = s;
s = orig;
cx->sb_orig = orig = RX_SUBBEG(rx);
s = orig + (m - s);
cx->sb_strend = s + (cx->sb_strend - m);
}
cx->sb_m = m = RX_OFFS(rx)[0].start + orig;
if (m > s) {
if (DO_UTF8(dstr) && !SvUTF8(cx->sb_targ))
sv_catpvn_utf8_upgrade(dstr, s, m - s, nsv);
else
sv_catpvn(dstr, s, m-s);
}
cx->sb_s = RX_OFFS(rx)[0].end + orig;
{ /* Update the pos() information. */
SV * const sv = cx->sb_targ;
MAGIC *mg;
SvUPGRADE(sv, SVt_PVMG);
if (!(mg = mg_find(sv, PERL_MAGIC_regex_global))) {
#ifdef PERL_OLD_COPY_ON_WRITE
if (SvIsCOW(sv))
sv_force_normal_flags(sv, 0);
#endif
mg = sv_magicext(sv, NULL, PERL_MAGIC_regex_global, &PL_vtbl_mglob,
NULL, 0);
}
mg->mg_len = m - orig;
}
if (old != rx)
(void)ReREFCNT_inc(rx);
cx->sb_rxtainted |= RX_MATCH_TAINTED(rx);
rxres_save(&cx->sb_rxres, rx);
RETURNOP(pm->op_pmstashstartu.op_pmreplstart);
}
void
Perl_rxres_save(pTHX_ void **rsp, REGEXP *rx)
{
UV *p = (UV*)*rsp;
U32 i;
PERL_ARGS_ASSERT_RXRES_SAVE;
PERL_UNUSED_CONTEXT;
if (!p || p[1] < RX_NPARENS(rx)) {
#ifdef PERL_OLD_COPY_ON_WRITE
i = 7 + RX_NPARENS(rx) * 2;
#else
i = 6 + RX_NPARENS(rx) * 2;
#endif
if (!p)
Newx(p, i, UV);
else
Renew(p, i, UV);
*rsp = (void*)p;
}
*p++ = PTR2UV(RX_MATCH_COPIED(rx) ? RX_SUBBEG(rx) : NULL);
RX_MATCH_COPIED_off(rx);
#ifdef PERL_OLD_COPY_ON_WRITE
*p++ = PTR2UV(rx->saved_copy);
rx->saved_copy = NULL;
#endif
*p++ = RX_NPARENS(rx);
*p++ = PTR2UV(RX_SUBBEG(rx));
*p++ = (UV)RX_SUBLEN(rx);
for (i = 0; i <= RX_NPARENS(rx); ++i) {
*p++ = (UV)RX_OFFS(rx)[i].start;
*p++ = (UV)RX_OFFS(rx)[i].end;
}
}
void
Perl_rxres_restore(pTHX_ void **rsp, REGEXP *rx)
{
UV *p = (UV*)*rsp;
U32 i;
PERL_ARGS_ASSERT_RXRES_RESTORE;
PERL_UNUSED_CONTEXT;
RX_MATCH_COPY_FREE(rx);
RX_MATCH_COPIED_set(rx, *p);
*p++ = 0;
#ifdef PERL_OLD_COPY_ON_WRITE
if (rx->saved_copy)
SvREFCNT_dec (rx->saved_copy);
rx->saved_copy = INT2PTR(SV*,*p);
*p++ = 0;
#endif
RX_NPARENS(rx) = *p++;
RX_SUBBEG(rx) = INT2PTR(char*,*p++);
RX_SUBLEN(rx) = (I32)(*p++);
for (i = 0; i <= RX_NPARENS(rx); ++i) {
RX_OFFS(rx)[i].start = (I32)(*p++);
RX_OFFS(rx)[i].end = (I32)(*p++);
}
}
void
Perl_rxres_free(pTHX_ void **rsp)
{
UV * const p = (UV*)*rsp;
PERL_ARGS_ASSERT_RXRES_FREE;
PERL_UNUSED_CONTEXT;
if (p) {
#ifdef PERL_POISON
void *tmp = INT2PTR(char*,*p);
Safefree(tmp);
if (*p)
PoisonFree(*p, 1, sizeof(*p));
#else
Safefree(INT2PTR(char*,*p));
#endif
#ifdef PERL_OLD_COPY_ON_WRITE
if (p[1]) {
SvREFCNT_dec (INT2PTR(SV*,p[1]));
}
#endif
Safefree(p);
*rsp = NULL;
}
}
PP(pp_formline)
{
dVAR; dSP; dMARK; dORIGMARK;
register SV * const tmpForm = *++MARK;
register U32 *fpc;
register char *t;
const char *f;
register I32 arg;
register SV *sv = NULL;
const char *item = NULL;
I32 itemsize = 0;
I32 fieldsize = 0;
I32 lines = 0;
bool chopspace = (strchr(PL_chopset, ' ') != NULL);
const char *chophere = NULL;
char *linemark = NULL;
NV value;
bool gotsome = FALSE;
STRLEN len;
const STRLEN fudge = SvPOK(tmpForm)
? (SvCUR(tmpForm) * (IN_BYTES ? 1 : 3) + 1) : 0;
bool item_is_utf8 = FALSE;
bool targ_is_utf8 = FALSE;
SV * nsv = NULL;
OP * parseres = NULL;
const char *fmt;
if (!SvMAGICAL(tmpForm) || !SvCOMPILED(tmpForm)) {
if (SvREADONLY(tmpForm)) {
SvREADONLY_off(tmpForm);
parseres = doparseform(tmpForm);
SvREADONLY_on(tmpForm);
}
else
parseres = doparseform(tmpForm);
if (parseres)
return parseres;
}
SvPV_force(PL_formtarget, len);
if (DO_UTF8(PL_formtarget))
targ_is_utf8 = TRUE;
t = SvGROW(PL_formtarget, len + fudge + 1); /* XXX SvCUR bad */
t += len;
f = SvPV_const(tmpForm, len);
/* need to jump to the next word */
fpc = (U32*)(f + len + WORD_ALIGN - SvCUR(tmpForm) % WORD_ALIGN);
for (;;) {
DEBUG_f( {
const char *name = "???";
arg = -1;
switch (*fpc) {
case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
case FF_CHECKNL: name = "CHECKNL"; break;
case FF_CHECKCHOP: name = "CHECKCHOP"; break;
case FF_SPACE: name = "SPACE"; break;
case FF_HALFSPACE: name = "HALFSPACE"; break;
case FF_ITEM: name = "ITEM"; break;
case FF_CHOP: name = "CHOP"; break;
case FF_LINEGLOB: name = "LINEGLOB"; break;
case FF_NEWLINE: name = "NEWLINE"; break;
case FF_MORE: name = "MORE"; break;
case FF_LINEMARK: name = "LINEMARK"; break;
case FF_END: name = "END"; break;
case FF_0DECIMAL: name = "0DECIMAL"; break;
case FF_LINESNGL: name = "LINESNGL"; break;
}
if (arg >= 0)
PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg);
else
PerlIO_printf(Perl_debug_log, "%-16s\n", name);
} );
switch (*fpc++) {
case FF_LINEMARK:
linemark = t;
lines++;
gotsome = FALSE;
break;
case FF_LITERAL:
arg = *fpc++;
if (targ_is_utf8 && !SvUTF8(tmpForm)) {
SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
*t = '\0';
sv_catpvn_utf8_upgrade(PL_formtarget, f, arg, nsv);
t = SvEND(PL_formtarget);
f += arg;
break;
}
if (!targ_is_utf8 && DO_UTF8(tmpForm)) {
SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
*t = '\0';
sv_utf8_upgrade(PL_formtarget);
SvGROW(PL_formtarget, SvCUR(PL_formtarget) + fudge + 1);
t = SvEND(PL_formtarget);
targ_is_utf8 = TRUE;
}
while (arg--)
*t++ = *f++;
break;
case FF_SKIP:
f += *fpc++;
break;
case FF_FETCH:
arg = *fpc++;
f += arg;
fieldsize = arg;
if (MARK < SP)
sv = *++MARK;
else {
sv = &PL_sv_no;
if (ckWARN(WARN_SYNTAX))
Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Not enough format arguments");
}
break;
case FF_CHECKNL:
{
const char *send;
const char *s = item = SvPV_const(sv, len);
itemsize = len;
if (DO_UTF8(sv)) {
itemsize = sv_len_utf8(sv);
if (itemsize != (I32)len) {
I32 itembytes;
if (itemsize > fieldsize) {
itemsize = fieldsize;
itembytes = itemsize;
sv_pos_u2b(sv, &itembytes, 0);
}
else
itembytes = len;
send = chophere = s + itembytes;
while (s < send) {
if (*s & ~31)
gotsome = TRUE;
else if (*s == '\n')
break;
s++;
}
item_is_utf8 = TRUE;
itemsize = s - item;
sv_pos_b2u(sv, &itemsize);
break;
}
}
item_is_utf8 = FALSE;
if (itemsize > fieldsize)
itemsize = fieldsize;
send = chophere = s + itemsize;
while (s < send) {
if (*s & ~31)
gotsome = TRUE;
else if (*s == '\n')
break;
s++;
}
itemsize = s - item;
break;
}
case FF_CHECKCHOP:
{
const char *s = item = SvPV_const(sv, len);
itemsize = len;
if (DO_UTF8(sv)) {
itemsize = sv_len_utf8(sv);
if (itemsize != (I32)len) {
I32 itembytes;
if (itemsize <= fieldsize) {
const char *send = chophere = s + itemsize;
while (s < send) {
if (*s == '\r') {
itemsize = s - item;
chophere = s;
break;
}
if (*s++ & ~31)
gotsome = TRUE;
}
}
else {
const char *send;
itemsize = fieldsize;
itembytes = itemsize;
sv_pos_u2b(sv, &itembytes, 0);
send = chophere = s + itembytes;
while (s < send || (s == send && isSPACE(*s))) {
if (isSPACE(*s)) {
if (chopspace)
chophere = s;
if (*s == '\r')
break;
}
else {
if (*s & ~31)
gotsome = TRUE;
if (strchr(PL_chopset, *s))
chophere = s + 1;
}
s++;
}
itemsize = chophere - item;
sv_pos_b2u(sv, &itemsize);
}
item_is_utf8 = TRUE;
break;
}
}
item_is_utf8 = FALSE;
if (itemsize <= fieldsize) {
const char *const send = chophere = s + itemsize;
while (s < send) {
if (*s == '\r') {
itemsize = s - item;
chophere = s;
break;
}
if (*s++ & ~31)
gotsome = TRUE;
}
}
else {
const char *send;
itemsize = fieldsize;
send = chophere = s + itemsize;
while (s < send || (s == send && isSPACE(*s))) {
if (isSPACE(*s)) {
if (chopspace)
chophere = s;
if (*s == '\r')
break;
}
else {
if (*s & ~31)
gotsome = TRUE;
if (strchr(PL_chopset, *s))
chophere = s + 1;
}
s++;
}
itemsize = chophere - item;
}
break;
}
case FF_SPACE:
arg = fieldsize - itemsize;
if (arg) {
fieldsize -= arg;
while (arg-- > 0)
*t++ = ' ';
}
break;
case FF_HALFSPACE:
arg = fieldsize - itemsize;
if (arg) {
arg /= 2;
fieldsize -= arg;
while (arg-- > 0)
*t++ = ' ';
}
break;
case FF_ITEM:
{
const char *s = item;
arg = itemsize;
if (item_is_utf8) {
if (!targ_is_utf8) {
SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
*t = '\0';
sv_utf8_upgrade(PL_formtarget);
SvGROW(PL_formtarget, SvCUR(PL_formtarget) + fudge + 1);
t = SvEND(PL_formtarget);
targ_is_utf8 = TRUE;
}
while (arg--) {
if (UTF8_IS_CONTINUED(*s)) {
STRLEN skip = UTF8SKIP(s);
switch (skip) {
default:
Move(s,t,skip,char);
s += skip;
t += skip;
break;
case 7: *t++ = *s++;
case 6: *t++ = *s++;
case 5: *t++ = *s++;
case 4: *t++ = *s++;
case 3: *t++ = *s++;
case 2: *t++ = *s++;
case 1: *t++ = *s++;
}
}
else {
if ( !((*t++ = *s++) & ~31) )
t[-1] = ' ';
}
}
break;
}
if (targ_is_utf8 && !item_is_utf8) {
SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
*t = '\0';
sv_catpvn_utf8_upgrade(PL_formtarget, s, arg, nsv);
for (; t < SvEND(PL_formtarget); t++) {
#ifdef EBCDIC
const int ch = *t;
if (iscntrl(ch))
#else
if (!(*t & ~31))
#endif
*t = ' ';
}
break;
}
while (arg--) {
#ifdef EBCDIC
const int ch = *t++ = *s++;
if (iscntrl(ch))
#else
if ( !((*t++ = *s++) & ~31) )
#endif
t[-1] = ' ';
}
break;
}
case FF_CHOP:
{
const char *s = chophere;
if (chopspace) {
while (isSPACE(*s))
s++;
}
sv_chop(sv,s);
SvSETMAGIC(sv);
break;
}
case FF_LINESNGL:
chopspace = 0;
case FF_LINEGLOB:
{
const bool oneline = fpc[-1] == FF_LINESNGL;
const char *s = item = SvPV_const(sv, len);
item_is_utf8 = DO_UTF8(sv);
itemsize = len;
if (itemsize) {
STRLEN to_copy = itemsize;
const char *const send = s + len;
const U8 *source = (const U8 *) s;
U8 *tmp = NULL;
gotsome = TRUE;
chophere = s + itemsize;
while (s < send) {
if (*s++ == '\n') {
if (oneline) {
to_copy = s - SvPVX_const(sv) - 1;
chophere = s;
break;
} else {
if (s == send) {
itemsize--;
to_copy--;
} else
lines++;
}
}
}
if (targ_is_utf8 && !item_is_utf8) {
source = tmp = bytes_to_utf8(source, &to_copy);
SvCUR_set(PL_formtarget,
t - SvPVX_const(PL_formtarget));
} else {
if (item_is_utf8 && !targ_is_utf8) {
/* Upgrade targ to UTF8, and then we reduce it to
a problem we have a simple solution for. */
SvCUR_set(PL_formtarget,
t - SvPVX_const(PL_formtarget));
targ_is_utf8 = TRUE;
/* Don't need get magic. */
sv_utf8_upgrade_flags(PL_formtarget, 0);
} else {
SvCUR_set(PL_formtarget,
t - SvPVX_const(PL_formtarget));
}
/* Easy. They agree. */
assert (item_is_utf8 == targ_is_utf8);
}
SvGROW(PL_formtarget,
SvCUR(PL_formtarget) + to_copy + fudge + 1);
t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget);
Copy(source, t, to_copy, char);
t += to_copy;
SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) + to_copy);
if (item_is_utf8) {
if (SvGMAGICAL(sv)) {
/* Mustn't call sv_pos_b2u() as it does a second
mg_get(). Is this a bug? Do we need a _flags()
variant? */
itemsize = utf8_length(source, source + itemsize);
} else {
sv_pos_b2u(sv, &itemsize);
}
assert(!tmp);
} else if (tmp) {
Safefree(tmp);
}
}
break;
}
case FF_0DECIMAL:
arg = *fpc++;
#if defined(USE_LONG_DOUBLE)
fmt = (const char *)
((arg & 256) ?
"%#0*.*" PERL_PRIfldbl : "%0*.*" PERL_PRIfldbl);
#else
fmt = (const char *)
((arg & 256) ?
"%#0*.*f" : "%0*.*f");
#endif
goto ff_dec;
case FF_DECIMAL:
arg = *fpc++;
#if defined(USE_LONG_DOUBLE)
fmt = (const char *)
((arg & 256) ? "%#*.*" PERL_PRIfldbl : "%*.*" PERL_PRIfldbl);
#else
fmt = (const char *)
((arg & 256) ? "%#*.*f" : "%*.*f");
#endif
ff_dec:
/* If the field is marked with ^ and the value is undefined,
blank it out. */
if ((arg & 512) && !SvOK(sv)) {
arg = fieldsize;
while (arg--)
*t++ = ' ';
break;
}
gotsome = TRUE;
value = SvNV(sv);
/* overflow evidence */
if (num_overflow(value, fieldsize, arg)) {
arg = fieldsize;
while (arg--)
*t++ = '#';
break;
}
/* Formats aren't yet marked for locales, so assume "yes". */
{
STORE_NUMERIC_STANDARD_SET_LOCAL();
my_snprintf(t, SvLEN(PL_formtarget) - (t - SvPVX(PL_formtarget)), fmt, (int) fieldsize, (int) arg & 255, value);
RESTORE_NUMERIC_STANDARD();
}
t += fieldsize;
break;
case FF_NEWLINE:
f++;
while (t-- > linemark && *t == ' ') ;
t++;
*t++ = '\n';
break;
case FF_BLANK:
arg = *fpc++;
if (gotsome) {
if (arg) { /* repeat until fields exhausted? */
*t = '\0';
SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
lines += FmLINES(PL_formtarget);
if (lines == 200) {
arg = t - linemark;
if (strnEQ(linemark, linemark - arg, arg))
DIE(aTHX_ "Runaway format");
}
if (targ_is_utf8)
SvUTF8_on(PL_formtarget);
FmLINES(PL_formtarget) = lines;
SP = ORIGMARK;
RETURNOP(cLISTOP->op_first);
}
}
else {
t = linemark;
lines--;
}
break;
case FF_MORE:
{
const char *s = chophere;
const char *send = item + len;
if (chopspace) {
while (isSPACE(*s) && (s < send))
s++;
}
if (s < send) {
char *s1;
arg = fieldsize - itemsize;
if (arg) {
fieldsize -= arg;
while (arg-- > 0)
*t++ = ' ';
}
s1 = t - 3;
if (strnEQ(s1," ",3)) {
while (s1 > SvPVX_const(PL_formtarget) && isSPACE(s1[-1]))
s1--;
}
*s1++ = '.';
*s1++ = '.';
*s1++ = '.';
}
break;
}
case FF_END:
*t = '\0';
SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
if (targ_is_utf8)
SvUTF8_on(PL_formtarget);
FmLINES(PL_formtarget) += lines;
SP = ORIGMARK;
RETPUSHYES;
}
}
}
PP(pp_grepstart)
{
dVAR; dSP;
SV *src;
if (PL_stack_base + *PL_markstack_ptr == SP) {
(void)POPMARK;
if (GIMME_V == G_SCALAR)
mXPUSHi(0);
RETURNOP(PL_op->op_next->op_next);
}
PL_stack_sp = PL_stack_base + *PL_markstack_ptr + 1;
pp_pushmark(); /* push dst */
pp_pushmark(); /* push src */
ENTER; /* enter outer scope */
SAVETMPS;
if (PL_op->op_private & OPpGREP_LEX)
SAVESPTR(PAD_SVl(PL_op->op_targ));
else
SAVE_DEFSV;
ENTER; /* enter inner scope */
SAVEVPTR(PL_curpm);
src = PL_stack_base[*PL_markstack_ptr];
SvTEMP_off(src);
if (PL_op->op_private & OPpGREP_LEX)
PAD_SVl(PL_op->op_targ) = src;
else
DEFSV_set(src);
PUTBACK;
if (PL_op->op_type == OP_MAPSTART)
pp_pushmark(); /* push top */
return ((LOGOP*)PL_op->op_next)->op_other;
}
PP(pp_mapwhile)
{
dVAR; dSP;
const I32 gimme = GIMME_V;
I32 items = (SP - PL_stack_base) - *PL_markstack_ptr; /* how many new items */
I32 count;
I32 shift;
SV** src;
SV** dst;
/* first, move source pointer to the next item in the source list */
++PL_markstack_ptr[-1];
/* if there are new items, push them into the destination list */
if (items && gimme != G_VOID) {