forked from Perl/perl5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpp_ctl.c
5847 lines (5186 loc) · 160 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"
#include "feature.h"
#define RUN_PP_CATCHABLY(thispp) \
STMT_START { if (CATCH_GET) return docatch(thispp); } STMT_END
#define dopopto_cursub() \
(PL_curstackinfo->si_cxsubix >= 0 \
? PL_curstackinfo->si_cxsubix \
: dopoptosub_at(cxstack, cxstack_ix))
#define dopoptosub(plop) dopoptosub_at(cxstack, (plop))
PP(pp_wantarray)
{
dSP;
I32 cxix;
const PERL_CONTEXT *cx;
EXTEND(SP, 1);
if (PL_op->op_private & OPpOFFBYONE) {
if (!(cx = caller_cx(1,NULL))) RETPUSHUNDEF;
}
else {
cxix = dopopto_cursub();
if (cxix < 0)
RETPUSHUNDEF;
cx = &cxstack[cxix];
}
switch (cx->blk_gimme) {
case G_ARRAY:
RETPUSHYES;
case G_SCALAR:
RETPUSHNO;
default:
RETPUSHUNDEF;
}
}
PP(pp_regcreset)
{
TAINT_NOT;
return NORMAL;
}
PP(pp_regcomp)
{
dSP;
PMOP *pm = (PMOP*)cLOGOP->op_other;
SV **args;
int nargs;
REGEXP *re = NULL;
REGEXP *new_re;
const regexp_engine *eng;
bool is_bare_re= FALSE;
if (PL_op->op_flags & OPf_STACKED) {
dMARK;
nargs = SP - MARK;
args = ++MARK;
}
else {
nargs = 1;
args = SP;
}
/* prevent recompiling under /o and ithreads. */
#if defined(USE_ITHREADS)
if (pm->op_pmflags & PMf_KEEP && PM_GETRE(pm)) {
SP = args-1;
RETURN;
}
#endif
re = PM_GETRE(pm);
assert (re != (REGEXP*) &PL_sv_undef);
eng = re ? RX_ENGINE(re) : current_re_engine();
new_re = (eng->op_comp
? eng->op_comp
: &Perl_re_op_compile
)(aTHX_ args, nargs, pm->op_code_list, eng, re,
&is_bare_re,
(pm->op_pmflags & RXf_PMf_FLAGCOPYMASK),
pm->op_pmflags |
(PL_op->op_flags & OPf_SPECIAL ? PMf_USE_RE_EVAL : 0));
if (pm->op_pmflags & PMf_HAS_CV)
ReANY(new_re)->qr_anoncv
= (CV*) SvREFCNT_inc(PAD_SV(PL_op->op_targ));
if (is_bare_re) {
REGEXP *tmp;
/* The match's LHS's get-magic might need to access this op's regexp
(e.g. $' =~ /$re/ while foo; see bug 70764). So we must call
get-magic now before we replace the regexp. Hopefully this hack can
be replaced with the approach described at
http://www.nntp.perl.org/group/perl.perl5.porters/2007/03/msg122415.html
some day. */
if (pm->op_type == OP_MATCH) {
SV *lhs;
const bool was_tainted = TAINT_get;
if (pm->op_flags & OPf_STACKED)
lhs = args[-1];
else if (pm->op_targ)
lhs = PAD_SV(pm->op_targ);
else lhs = DEFSV;
SvGETMAGIC(lhs);
/* Restore the previous value of PL_tainted (which may have been
modified by get-magic), to avoid incorrectly setting the
RXf_TAINTED flag with RX_TAINT_on further down. */
TAINT_set(was_tainted);
#ifdef NO_TAINT_SUPPORT
PERL_UNUSED_VAR(was_tainted);
#endif
}
tmp = reg_temp_copy(NULL, new_re);
ReREFCNT_dec(new_re);
new_re = tmp;
}
if (re != new_re) {
ReREFCNT_dec(re);
PM_SETRE(pm, new_re);
}
assert(TAINTING_get || !TAINT_get);
if (TAINT_get) {
SvTAINTED_on((SV*)new_re);
RX_TAINT_on(new_re);
}
/* handle the empty pattern */
if (!RX_PRELEN(PM_GETRE(pm)) && PL_curpm) {
if (PL_curpm == PL_reg_curpm) {
if (PL_curpm_under && PL_curpm_under == PL_reg_curpm) {
Perl_croak(aTHX_ "Infinite recursion via empty pattern");
}
}
}
#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) {
cLOGOP->op_first->op_next = PL_op->op_next;
}
#endif
SP = args-1;
RETURN;
}
PP(pp_substcont)
{
dSP;
PERL_CONTEXT *cx = CX_CUR();
PMOP * const pm = (PMOP*) cLOGOP->op_other;
SV * const dstr = cx->sb_dstr;
char *s = cx->sb_s;
char *m = cx->sb_m;
char *orig = cx->sb_orig;
REGEXP * const rx = cx->sb_rx;
SV *nsv = NULL;
REGEXP *old = PM_GETRE(pm);
PERL_ASYNC_CHECK();
if(old != rx) {
if(old)
ReREFCNT_dec(old);
PM_SETRE(pm,ReREFCNT_inc(rx));
}
rxres_restore(&cx->sb_rxres, rx);
if (cx->sb_iters++) {
const SSize_t saviters = cx->sb_iters;
if (cx->sb_iters > cx->sb_maxiters)
DIE(aTHX_ "Substitution loop");
SvGETMAGIC(TOPs); /* possibly clear taint on $1 etc: #67962 */
/* See "how taint works" above pp_subst() */
sv_catsv_nomg(dstr, POPs);
if (UNLIKELY(TAINT_get))
cx->sb_rxtainted |= SUBST_TAINT_REPL;
if (CxONCE(cx) || s < orig ||
!CALLREGEXEC(rx, s, cx->sb_strend, orig,
(s == m), cx->sb_targ, NULL,
(REXEC_IGNOREPOS|REXEC_NOT_FIRST|REXEC_FAIL_ON_UNDERFLOW)))
{
SV *targ = cx->sb_targ;
assert(cx->sb_strend >= s);
if(cx->sb_strend > s) {
if (DO_UTF8(dstr) && !SvUTF8(targ))
sv_catpvn_nomg_utf8_upgrade(dstr, s, cx->sb_strend - s, nsv);
else
sv_catpvn_nomg(dstr, s, cx->sb_strend - s);
}
if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */
cx->sb_rxtainted |= SUBST_TAINT_PAT;
if (pm->op_pmflags & PMf_NONDESTRUCT) {
PUSHs(dstr);
/* From here on down we're using the copy, and leaving the
original untouched. */
targ = dstr;
}
else {
SV_CHECK_THINKFIRST_COW_DROP(targ);
if (isGV(targ)) Perl_croak_no_modify();
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);
PL_tainted = 0;
mPUSHi(saviters - 1);
(void)SvPOK_only_UTF8(targ);
}
/* update the taint state of various variables in
* preparation for final exit.
* See "how taint works" above pp_subst() */
if (TAINTING_get) {
if ((cx->sb_rxtainted & SUBST_TAINT_PAT) ||
((cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
== (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
)
(RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */
if (!(cx->sb_rxtainted & SUBST_TAINT_BOOLRET)
&& (cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT))
)
SvTAINTED_on(TOPs); /* taint return value */
/* needed for mg_set below */
TAINT_set(
cBOOL(cx->sb_rxtainted &
(SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL))
);
/* sv_magic(), when adding magic (e.g.taint magic), also
* recalculates any pos() magic, converting any byte offset
* to utf8 offset. Make sure pos() is reset before this
* happens rather than using the now invalid value (since
* we've just replaced targ's pvx buffer with the
* potentially shorter dstr buffer). Normally (i.e. in
* non-taint cases), pos() gets removed a few lines later
* with the SvSETMAGIC().
*/
{
MAGIC *mg;
mg = mg_find_mglob(targ);
if (mg) {
MgBYTEPOS_set(mg, targ, SvPVX(targ), -1);
}
}
SvTAINT(TARG);
}
/* PL_tainted must be correctly set for this mg_set */
SvSETMAGIC(TARG);
TAINT_NOT;
CX_LEAVE_SCOPE(cx);
CX_POPSUBST(cx);
CX_POP(cx);
PERL_ASYNC_CHECK();
RETURNOP(pm->op_next);
NOT_REACHED; /* NOTREACHED */
}
cx->sb_iters = saviters;
}
if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
m = s;
s = orig;
assert(!RX_SUBOFFSET(rx));
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_nomg_utf8_upgrade(dstr, s, m - s, nsv);
else
sv_catpvn_nomg(dstr, s, m-s);
}
cx->sb_s = RX_OFFS(rx)[0].end + orig;
{ /* Update the pos() information. */
SV * const sv
= (pm->op_pmflags & PMf_NONDESTRUCT) ? cx->sb_dstr : cx->sb_targ;
MAGIC *mg;
/* the string being matched against may no longer be a string,
* e.g. $_=0; s/.../$_++/ge */
if (!SvPOK(sv))
SvPV_force_nomg_nolen(sv);
if (!(mg = mg_find_mglob(sv))) {
mg = sv_magicext_mglob(sv);
}
MgBYTEPOS_set(mg, sv, SvPVX(sv), m - orig);
}
if (old != rx)
(void)ReREFCNT_inc(rx);
/* update the taint state of various variables in preparation
* for calling the code block.
* See "how taint works" above pp_subst() */
if (TAINTING_get) {
if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */
cx->sb_rxtainted |= SUBST_TAINT_PAT;
if ((cx->sb_rxtainted & SUBST_TAINT_PAT) ||
((cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
== (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
)
(RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */
if (cx->sb_iters > 1 && (cx->sb_rxtainted &
(SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL)))
SvTAINTED_on((pm->op_pmflags & PMf_NONDESTRUCT)
? cx->sb_dstr : cx->sb_targ);
TAINT_NOT;
}
rxres_save(&cx->sb_rxres, rx);
PL_curpm = pm;
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_ANY_COW
i = 7 + (RX_NPARENS(rx)+1) * 2;
#else
i = 6 + (RX_NPARENS(rx)+1) * 2;
#endif
if (!p)
Newx(p, i, UV);
else
Renew(p, i, UV);
*rsp = (void*)p;
}
/* what (if anything) to free on croak */
*p++ = PTR2UV(RX_MATCH_COPIED(rx) ? RX_SUBBEG(rx) : NULL);
RX_MATCH_COPIED_off(rx);
*p++ = RX_NPARENS(rx);
#ifdef PERL_ANY_COW
*p++ = PTR2UV(RX_SAVED_COPY(rx));
RX_SAVED_COPY(rx) = NULL;
#endif
*p++ = PTR2UV(RX_SUBBEG(rx));
*p++ = (UV)RX_SUBLEN(rx);
*p++ = (UV)RX_SUBOFFSET(rx);
*p++ = (UV)RX_SUBCOFFSET(rx);
for (i = 0; i <= RX_NPARENS(rx); ++i) {
*p++ = (UV)RX_OFFS(rx)[i].start;
*p++ = (UV)RX_OFFS(rx)[i].end;
}
}
static void
S_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;
RX_NPARENS(rx) = *p++;
#ifdef PERL_ANY_COW
if (RX_SAVED_COPY(rx))
SvREFCNT_dec (RX_SAVED_COPY(rx));
RX_SAVED_COPY(rx) = INT2PTR(SV*,*p);
*p++ = 0;
#endif
RX_SUBBEG(rx) = INT2PTR(char*,*p++);
RX_SUBLEN(rx) = (I32)(*p++);
RX_SUBOFFSET(rx) = (I32)*p++;
RX_SUBCOFFSET(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++);
}
}
static void
S_rxres_free(pTHX_ void **rsp)
{
UV * const p = (UV*)*rsp;
PERL_ARGS_ASSERT_RXRES_FREE;
PERL_UNUSED_CONTEXT;
if (p) {
void *tmp = INT2PTR(char*,*p);
#ifdef PERL_POISON
#ifdef PERL_ANY_COW
U32 i = 9 + p[1] * 2;
#else
U32 i = 8 + p[1] * 2;
#endif
#endif
#ifdef PERL_ANY_COW
SvREFCNT_dec (INT2PTR(SV*,p[2]));
#endif
#ifdef PERL_POISON
PoisonFree(p, i, sizeof(UV));
#endif
Safefree(tmp);
Safefree(p);
*rsp = NULL;
}
}
#define FORM_NUM_BLANK (1<<30)
#define FORM_NUM_POINT (1<<29)
PP(pp_formline)
{
dSP; dMARK; dORIGMARK;
SV * const tmpForm = *++MARK;
SV *formsv; /* contains text of original format */
U32 *fpc; /* format ops program counter */
char *t; /* current append position in target string */
const char *f; /* current position in format string */
I32 arg;
SV *sv = NULL; /* current item */
const char *item = NULL;/* string value of current item */
I32 itemsize = 0; /* length (chars) of item, possibly truncated */
I32 itembytes = 0; /* as itemsize, but length in bytes */
I32 fieldsize = 0; /* width of current field */
I32 lines = 0; /* number of lines that have been output */
bool chopspace = (strchr(PL_chopset, ' ') != NULL); /* does $: have space */
const char *chophere = NULL; /* where to chop current item */
STRLEN linemark = 0; /* pos of start of line in output */
NV value;
bool gotsome = FALSE; /* seen at least one non-blank item on this line */
STRLEN len; /* length of current sv */
STRLEN linemax; /* estimate of output size in bytes */
bool item_is_utf8 = FALSE;
bool targ_is_utf8 = FALSE;
const char *fmt;
MAGIC *mg = NULL;
U8 *source; /* source of bytes to append */
STRLEN to_copy; /* how may bytes to append */
char trans; /* what chars to translate */
bool copied_form = FALSE; /* have we duplicated the form? */
mg = doparseform(tmpForm);
fpc = (U32*)mg->mg_ptr;
/* the actual string the format was compiled from.
* with overload etc, this may not match tmpForm */
formsv = mg->mg_obj;
SvPV_force(PL_formtarget, len);
if (SvTAINTED(tmpForm) || SvTAINTED(formsv))
SvTAINTED_on(PL_formtarget);
if (DO_UTF8(PL_formtarget))
targ_is_utf8 = TRUE;
/* this is an initial estimate of how much output buffer space
* to allocate. It may be exceeded later */
linemax = (SvCUR(formsv) * (IN_BYTES ? 1 : 3) + 1);
t = SvGROW(PL_formtarget, len + linemax + 1);
/* XXX from now onwards, SvCUR(PL_formtarget) is invalid */
t += len;
f = SvPV_const(formsv, len);
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: /* start (or end) of a line */
linemark = t - SvPVX(PL_formtarget);
lines++;
gotsome = FALSE;
break;
case FF_LITERAL: /* append <arg> literal chars */
to_copy = *fpc++;
source = (U8 *)f;
f += to_copy;
trans = '~';
item_is_utf8 = targ_is_utf8 ? !!DO_UTF8(formsv) : !!SvUTF8(formsv);
goto append;
case FF_SKIP: /* skip <arg> chars in format */
f += *fpc++;
break;
case FF_FETCH: /* get next item and set field size to <arg> */
arg = *fpc++;
f += arg;
fieldsize = arg;
if (MARK < SP)
sv = *++MARK;
else {
sv = &PL_sv_no;
Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Not enough format arguments");
}
if (SvTAINTED(sv))
SvTAINTED_on(PL_formtarget);
break;
case FF_CHECKNL: /* find max len of item (up to \n) that fits field */
{
const char *s = item = SvPV_const(sv, len);
const char *send = s + len;
itemsize = 0;
item_is_utf8 = DO_UTF8(sv);
while (s < send) {
if (!isCNTRL(*s))
gotsome = TRUE;
else if (*s == '\n')
break;
if (item_is_utf8)
s += UTF8SKIP(s);
else
s++;
itemsize++;
if (itemsize == fieldsize)
break;
}
itembytes = s - item;
chophere = s;
break;
}
case FF_CHECKCHOP: /* like CHECKNL, but up to highest split point */
{
const char *s = item = SvPV_const(sv, len);
const char *send = s + len;
I32 size = 0;
chophere = NULL;
item_is_utf8 = DO_UTF8(sv);
while (s < send) {
/* look for a legal split position */
if (isSPACE(*s)) {
if (*s == '\r') {
chophere = s;
itemsize = size;
break;
}
if (chopspace) {
/* provisional split point */
chophere = s;
itemsize = size;
}
/* we delay testing fieldsize until after we've
* processed the possible split char directly
* following the last field char; so if fieldsize=3
* and item="a b cdef", we consume "a b", not "a".
* Ditto further down.
*/
if (size == fieldsize)
break;
}
else {
if (strchr(PL_chopset, *s)) {
/* provisional split point */
/* for a non-space split char, we include
* the split char; hence the '+1' */
chophere = s + 1;
itemsize = size;
}
if (size == fieldsize)
break;
if (!isCNTRL(*s))
gotsome = TRUE;
}
if (item_is_utf8)
s += UTF8SKIP(s);
else
s++;
size++;
}
if (!chophere || s == send) {
chophere = s;
itemsize = size;
}
itembytes = chophere - item;
break;
}
case FF_SPACE: /* append padding space (diff of field, item size) */
arg = fieldsize - itemsize;
if (arg) {
fieldsize -= arg;
while (arg-- > 0)
*t++ = ' ';
}
break;
case FF_HALFSPACE: /* like FF_SPACE, but only append half as many */
arg = fieldsize - itemsize;
if (arg) {
arg /= 2;
fieldsize -= arg;
while (arg-- > 0)
*t++ = ' ';
}
break;
case FF_ITEM: /* append a text item, while blanking ctrl chars */
to_copy = itembytes;
source = (U8 *)item;
trans = 1;
goto append;
case FF_CHOP: /* (for ^*) chop the current item */
if (sv != &PL_sv_no) {
const char *s = chophere;
if (!copied_form &&
((sv == tmpForm || SvSMAGICAL(sv))
|| (SvGMAGICAL(tmpForm) && !sv_only_taint_gmagic(tmpForm))) ) {
/* sv and tmpForm are either the same SV, or magic might allow modification
of tmpForm when sv is modified, so copy */
SV *newformsv = sv_mortalcopy(formsv);
U32 *new_compiled;
f = SvPV_nolen(newformsv) + (f - SvPV_nolen(formsv));
Newx(new_compiled, mg->mg_len / sizeof(U32), U32);
memcpy(new_compiled, mg->mg_ptr, mg->mg_len);
SAVEFREEPV(new_compiled);
fpc = new_compiled + (fpc - (U32*)mg->mg_ptr);
formsv = newformsv;
copied_form = TRUE;
}
if (chopspace) {
while (isSPACE(*s))
s++;
}
if (SvPOKp(sv))
sv_chop(sv,s);
else
/* tied, overloaded or similar strangeness.
* Do it the hard way */
sv_setpvn(sv, s, len - (s-item));
SvSETMAGIC(sv);
break;
}
/* FALLTHROUGH */
case FF_LINESNGL: /* process ^* */
chopspace = 0;
/* FALLTHROUGH */
case FF_LINEGLOB: /* process @* */
{
const bool oneline = fpc[-1] == FF_LINESNGL;
const char *s = item = SvPV_const(sv, len);
const char *const send = s + len;
item_is_utf8 = DO_UTF8(sv);
chophere = s + len;
if (!len)
break;
trans = 0;
gotsome = TRUE;
source = (U8 *) s;
to_copy = len;
while (s < send) {
if (*s++ == '\n') {
if (oneline) {
to_copy = s - item - 1;
chophere = s;
break;
} else {
if (s == send) {
to_copy--;
} else
lines++;
}
}
}
}
append:
/* append to_copy bytes from source to PL_formstring.
* item_is_utf8 implies source is utf8.
* if trans, translate certain characters during the copy */
{
U8 *tmp = NULL;
STRLEN grow = 0;
SvCUR_set(PL_formtarget,
t - SvPVX_const(PL_formtarget));
if (targ_is_utf8 && !item_is_utf8) {
source = tmp = bytes_to_utf8(source, &to_copy);
grow = to_copy;
} else {
if (item_is_utf8 && !targ_is_utf8) {
U8 *s;
/* Upgrade targ to UTF8, and then we reduce it to
a problem we have a simple solution for.
Don't need get magic. */
sv_utf8_upgrade_nomg(PL_formtarget);
targ_is_utf8 = TRUE;
/* re-calculate linemark */
s = (U8*)SvPVX(PL_formtarget);
/* the bytes we initially allocated to append the
* whole line may have been gobbled up during the
* upgrade, so allocate a whole new line's worth
* for safety */
grow = linemax;
while (linemark--)
s += UTF8_SAFE_SKIP(s,
(U8 *) SvEND(PL_formtarget));
linemark = s - (U8*)SvPVX(PL_formtarget);
}
/* Easy. They agree. */
assert (item_is_utf8 == targ_is_utf8);
}
if (!trans)
/* @* and ^* are the only things that can exceed
* the linemax, so grow by the output size, plus
* a whole new form's worth in case of any further
* output */
grow = linemax + to_copy;
if (grow)
SvGROW(PL_formtarget, SvCUR(PL_formtarget) + grow + 1);
t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget);
Copy(source, t, to_copy, char);
if (trans) {
/* blank out ~ or control chars, depending on trans.
* works on bytes not chars, so relies on not
* matching utf8 continuation bytes */
U8 *s = (U8*)t;
U8 *send = s + to_copy;
while (s < send) {
const int ch = *s;
if (trans == '~' ? (ch == '~') : isCNTRL(ch))
*s = ' ';
s++;
}
}
t += to_copy;
SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) + to_copy);
if (tmp)
Safefree(tmp);
break;
}
case FF_0DECIMAL: /* like FF_DECIMAL but for 0### */
arg = *fpc++;
fmt = (const char *)
((arg & FORM_NUM_POINT) ? "%#0*.*" NVff : "%0*.*" NVff);
goto ff_dec;
case FF_DECIMAL: /* do @##, ^##, where <arg>=(precision|flags) */
arg = *fpc++;
fmt = (const char *)
((arg & FORM_NUM_POINT) ? "%#*.*" NVff : "%*.*" NVff);
ff_dec:
/* If the field is marked with ^ and the value is undefined,
blank it out. */
if ((arg & FORM_NUM_BLANK) && !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". */
{
Size_t max = SvLEN(PL_formtarget) - (t - SvPVX(PL_formtarget));
int len;
DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
STORE_LC_NUMERIC_SET_TO_NEEDED();
arg &= ~(FORM_NUM_POINT|FORM_NUM_BLANK);
#ifdef USE_QUADMATH
{
int len;
if (!quadmath_format_valid(fmt))
Perl_croak_nocontext("panic: quadmath invalid format \"%s\"", fmt);
len = quadmath_snprintf(t, max, fmt, (int) fieldsize, (int) arg, value);
if (len == -1)
Perl_croak_nocontext("panic: quadmath_snprintf failed, format \"%s\"", fmt);
}
#else
/* we generate fmt ourselves so it is safe */
GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral);
len = my_snprintf(t, max, fmt, (int) fieldsize, (int) arg, value);
GCC_DIAG_RESTORE_STMT;
#endif
PERL_MY_SNPRINTF_POST_GUARD(len, max);
RESTORE_LC_NUMERIC();
}
t += fieldsize;
break;
case FF_NEWLINE: /* delete trailing spaces, then append \n */
f++;
while (t-- > (SvPVX(PL_formtarget) + linemark) && *t == ' ') ;
t++;
*t++ = '\n';
break;
case FF_BLANK: /* for arg==0: do '~'; for arg>0 : do '~~' */
arg = *fpc++;
if (gotsome) {
if (arg) { /* repeat until fields exhausted? */
fpc--;
goto end;
}
}
else {
t = SvPVX(PL_formtarget) + linemark;
lines--;
}
break;
case FF_MORE: /* replace long end of string with '...' */
{
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 (strBEGINs(s1," ")) {
while (s1 > SvPVX_const(PL_formtarget) && isSPACE(s1[-1]))
s1--;
}
*s1++ = '.';
*s1++ = '.';
*s1++ = '.';
}
break;
}
case FF_END: /* tidy up, then return */
end:
assert(t < SvPVX_const(PL_formtarget) + SvLEN(PL_formtarget));
*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;
if (fpc[-1] == FF_BLANK)
RETURNOP(cLISTOP->op_first);
else
RETPUSHYES;
}
}
}
/* also used for: pp_mapstart() */
PP(pp_grepstart)
{
dSP;
SV *src;
if (PL_stack_base + TOPMARK == SP) {
(void)POPMARK;
if (GIMME_V == G_SCALAR)
XPUSHs(&PL_sv_zero);
RETURNOP(PL_op->op_next->op_next);
}
PL_stack_sp = PL_stack_base + TOPMARK + 1;
Perl_pp_pushmark(aTHX); /* push dst */
Perl_pp_pushmark(aTHX); /* push src */
ENTER_with_name("grep"); /* enter outer scope */
SAVETMPS;
SAVE_DEFSV;
ENTER_with_name("grep_item"); /* enter inner scope */
SAVEVPTR(PL_curpm);
src = PL_stack_base[TOPMARK];
if (SvPADTMP(src)) {
src = PL_stack_base[TOPMARK] = sv_mortalcopy(src);
PL_tmps_floor++;
}
SvTEMP_off(src);
DEFSV_set(src);
PUTBACK;
if (PL_op->op_type == OP_MAPSTART)