forked from Perl/perl5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmg.c
3843 lines (3393 loc) · 97.4 KB
/
mg.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
/* mg.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.
*
*/
/*
* Sam sat on the ground and put his head in his hands. 'I wish I had never
* come here, and I don't want to see no more magic,' he said, and fell silent.
*
* [p.363 of _The Lord of the Rings_, II/vii: "The Mirror of Galadriel"]
*/
/*
=head1 Magic
"Magic" is special data attached to SV structures in order to give them
"magical" properties. When any Perl code tries to read from, or assign to,
an SV marked as magical, it calls the 'get' or 'set' function associated
with that SV's magic. A get is called prior to reading an SV, in order to
give it a chance to update its internal value (get on $. writes the line
number of the last read filehandle into the SV's IV slot), while
set is called after an SV has been written to, in order to allow it to make
use of its changed value (set on $/ copies the SV's new value to the
PL_rs global variable).
Magic is implemented as a linked list of MAGIC structures attached to the
SV. Each MAGIC struct holds the type of the magic, a pointer to an array
of functions that implement the get(), set(), length() etc functions,
plus space for some flags and pointers. For example, a tied variable has
a MAGIC structure that contains a pointer to the object associated with the
tie.
=for apidoc Ayh||MAGIC
=cut
*/
#include "EXTERN.h"
#define PERL_IN_MG_C
#include "perl.h"
#include "feature.h"
#if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
# ifdef I_GRP
# include <grp.h>
# endif
#endif
#if defined(HAS_SETGROUPS)
# ifndef NGROUPS
# define NGROUPS 32
# endif
#endif
#ifdef __hpux
# include <sys/pstat.h>
#endif
#ifdef HAS_PRCTL_SET_NAME
# include <sys/prctl.h>
#endif
#ifdef __Lynx__
/* Missing protos on LynxOS */
void setruid(uid_t id);
void seteuid(uid_t id);
void setrgid(uid_t id);
void setegid(uid_t id);
#endif
/*
* Pre-magic setup and post-magic takedown.
* Use the "DESTRUCTOR" scope cleanup to reinstate magic.
*/
struct magic_state {
SV* mgs_sv;
I32 mgs_ss_ix;
U32 mgs_flags;
bool mgs_bumped;
};
/* MGS is typedef'ed to struct magic_state in perl.h */
STATIC void
S_save_magic_flags(pTHX_ I32 mgs_ix, SV *sv, U32 flags)
{
MGS* mgs;
bool bumped = FALSE;
PERL_ARGS_ASSERT_SAVE_MAGIC_FLAGS;
assert(SvMAGICAL(sv));
/* we shouldn't really be called here with RC==0, but it can sometimes
* happen via mg_clear() (which also shouldn't be called when RC==0,
* but it can happen). Handle this case gracefully(ish) by not RC++
* and thus avoiding the resultant double free */
if (SvREFCNT(sv) > 0) {
/* guard against sv getting freed midway through the mg clearing,
* by holding a private reference for the duration. */
SvREFCNT_inc_simple_void_NN(sv);
bumped = TRUE;
}
SAVEDESTRUCTOR_X(S_restore_magic, INT2PTR(void*, (IV)mgs_ix));
mgs = SSPTR(mgs_ix, MGS*);
mgs->mgs_sv = sv;
mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
mgs->mgs_bumped = bumped;
SvFLAGS(sv) &= ~flags;
SvREADONLY_off(sv);
}
#define save_magic(a,b) save_magic_flags(a,b,SVs_GMG|SVs_SMG|SVs_RMG)
/*
=for apidoc mg_magical
Turns on the magical status of an SV. See C<L</sv_magic>>.
=cut
*/
void
Perl_mg_magical(SV *sv)
{
const MAGIC* mg;
PERL_ARGS_ASSERT_MG_MAGICAL;
SvMAGICAL_off(sv);
if ((mg = SvMAGIC(sv))) {
do {
const MGVTBL* const vtbl = mg->mg_virtual;
if (vtbl) {
if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
SvGMAGICAL_on(sv);
if (vtbl->svt_set)
SvSMAGICAL_on(sv);
if (vtbl->svt_clear)
SvRMAGICAL_on(sv);
}
} while ((mg = mg->mg_moremagic));
if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)))
SvRMAGICAL_on(sv);
}
}
/*
=for apidoc mg_get
Do magic before a value is retrieved from the SV. The type of SV must
be >= C<SVt_PVMG>. See C<L</sv_magic>>.
=cut
*/
int
Perl_mg_get(pTHX_ SV *sv)
{
const I32 mgs_ix = SSNEW(sizeof(MGS));
bool saved = FALSE;
bool have_new = 0;
bool taint_only = TRUE; /* the only get method seen is taint */
MAGIC *newmg, *head, *cur, *mg;
PERL_ARGS_ASSERT_MG_GET;
if (PL_localizing == 1 && sv == DEFSV) return 0;
/* We must call svt_get(sv, mg) for each valid entry in the linked
list of magic. svt_get() may delete the current entry, add new
magic to the head of the list, or upgrade the SV. AMS 20010810 */
newmg = cur = head = mg = SvMAGIC(sv);
while (mg) {
const MGVTBL * const vtbl = mg->mg_virtual;
MAGIC * const nextmg = mg->mg_moremagic; /* it may delete itself */
if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
/* taint's mg get is so dumb it doesn't need flag saving */
if (mg->mg_type != PERL_MAGIC_taint) {
taint_only = FALSE;
if (!saved) {
save_magic(mgs_ix, sv);
saved = TRUE;
}
}
vtbl->svt_get(aTHX_ sv, mg);
/* guard against magic having been deleted - eg FETCH calling
* untie */
if (!SvMAGIC(sv)) {
/* recalculate flags */
(SSPTR(mgs_ix, MGS *))->mgs_flags &= ~(SVs_GMG|SVs_SMG|SVs_RMG);
break;
}
/* recalculate flags if this entry was deleted. */
if (mg->mg_flags & MGf_GSKIP)
(SSPTR(mgs_ix, MGS *))->mgs_flags &=
~(SVs_GMG|SVs_SMG|SVs_RMG);
}
else if (vtbl == &PL_vtbl_utf8) {
/* get-magic can reallocate the PV, unless there's only taint
* magic */
if (taint_only) {
MAGIC *mg2;
for (mg2 = nextmg; mg2; mg2 = mg2->mg_moremagic) {
if ( mg2->mg_type != PERL_MAGIC_taint
&& !(mg2->mg_flags & MGf_GSKIP)
&& mg2->mg_virtual
&& mg2->mg_virtual->svt_get
) {
taint_only = FALSE;
break;
}
}
}
if (!taint_only)
magic_setutf8(sv, mg);
}
mg = nextmg;
if (have_new) {
/* Have we finished with the new entries we saw? Start again
where we left off (unless there are more new entries). */
if (mg == head) {
have_new = 0;
mg = cur;
head = newmg;
}
}
/* Were any new entries added? */
if (!have_new && (newmg = SvMAGIC(sv)) != head) {
have_new = 1;
cur = mg;
mg = newmg;
/* recalculate flags */
(SSPTR(mgs_ix, MGS *))->mgs_flags &= ~(SVs_GMG|SVs_SMG|SVs_RMG);
}
}
if (saved)
restore_magic(INT2PTR(void *, (IV)mgs_ix));
return 0;
}
/*
=for apidoc mg_set
Do magic after a value is assigned to the SV. See C<L</sv_magic>>.
=cut
*/
int
Perl_mg_set(pTHX_ SV *sv)
{
const I32 mgs_ix = SSNEW(sizeof(MGS));
MAGIC* mg;
MAGIC* nextmg;
PERL_ARGS_ASSERT_MG_SET;
if (PL_localizing == 2 && sv == DEFSV) return 0;
save_magic_flags(mgs_ix, sv, SVs_GMG|SVs_SMG); /* leave SVs_RMG on */
for (mg = SvMAGIC(sv); mg; mg = nextmg) {
const MGVTBL* vtbl = mg->mg_virtual;
nextmg = mg->mg_moremagic; /* it may delete itself */
if (mg->mg_flags & MGf_GSKIP) {
mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
(SSPTR(mgs_ix, MGS*))->mgs_flags &= ~(SVs_GMG|SVs_SMG|SVs_RMG);
}
if (PL_localizing == 2
&& PERL_MAGIC_TYPE_IS_VALUE_MAGIC(mg->mg_type))
continue;
if (vtbl && vtbl->svt_set)
vtbl->svt_set(aTHX_ sv, mg);
}
restore_magic(INT2PTR(void*, (IV)mgs_ix));
return 0;
}
/*
=for apidoc mg_length
Reports on the SV's length in bytes, calling length magic if available,
but does not set the UTF8 flag on C<sv>. It will fall back to 'get'
magic if there is no 'length' magic, but with no indication as to
whether it called 'get' magic. It assumes C<sv> is a C<PVMG> or
higher. Use C<sv_len()> instead.
=cut
*/
U32
Perl_mg_length(pTHX_ SV *sv)
{
MAGIC* mg;
STRLEN len;
PERL_ARGS_ASSERT_MG_LENGTH;
for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
const MGVTBL * const vtbl = mg->mg_virtual;
if (vtbl && vtbl->svt_len) {
const I32 mgs_ix = SSNEW(sizeof(MGS));
save_magic(mgs_ix, sv);
/* omit MGf_GSKIP -- not changed here */
len = vtbl->svt_len(aTHX_ sv, mg);
restore_magic(INT2PTR(void*, (IV)mgs_ix));
return len;
}
}
(void)SvPV_const(sv, len);
return len;
}
I32
Perl_mg_size(pTHX_ SV *sv)
{
MAGIC* mg;
PERL_ARGS_ASSERT_MG_SIZE;
for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
const MGVTBL* const vtbl = mg->mg_virtual;
if (vtbl && vtbl->svt_len) {
const I32 mgs_ix = SSNEW(sizeof(MGS));
I32 len;
save_magic(mgs_ix, sv);
/* omit MGf_GSKIP -- not changed here */
len = vtbl->svt_len(aTHX_ sv, mg);
restore_magic(INT2PTR(void*, (IV)mgs_ix));
return len;
}
}
switch(SvTYPE(sv)) {
case SVt_PVAV:
return AvFILLp((const AV *) sv); /* Fallback to non-tied array */
case SVt_PVHV:
/* FIXME */
default:
Perl_croak(aTHX_ "Size magic not implemented");
}
NOT_REACHED; /* NOTREACHED */
}
/*
=for apidoc mg_clear
Clear something magical that the SV represents. See C<L</sv_magic>>.
=cut
*/
int
Perl_mg_clear(pTHX_ SV *sv)
{
const I32 mgs_ix = SSNEW(sizeof(MGS));
MAGIC* mg;
MAGIC *nextmg;
PERL_ARGS_ASSERT_MG_CLEAR;
save_magic(mgs_ix, sv);
for (mg = SvMAGIC(sv); mg; mg = nextmg) {
const MGVTBL* const vtbl = mg->mg_virtual;
/* omit GSKIP -- never set here */
nextmg = mg->mg_moremagic; /* it may delete itself */
if (vtbl && vtbl->svt_clear)
vtbl->svt_clear(aTHX_ sv, mg);
}
restore_magic(INT2PTR(void*, (IV)mgs_ix));
return 0;
}
static MAGIC*
S_mg_findext_flags(const SV *sv, int type, const MGVTBL *vtbl, U32 flags)
{
assert(flags <= 1);
if (sv) {
MAGIC *mg;
for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
if (mg->mg_type == type && (!flags || mg->mg_virtual == vtbl)) {
return mg;
}
}
}
return NULL;
}
/*
=for apidoc mg_find
Finds the magic pointer for C<type> matching the SV. See C<L</sv_magic>>.
=cut
*/
MAGIC*
Perl_mg_find(const SV *sv, int type)
{
return S_mg_findext_flags(sv, type, NULL, 0);
}
/*
=for apidoc mg_findext
Finds the magic pointer of C<type> with the given C<vtbl> for the C<SV>. See
C<L</sv_magicext>>.
=cut
*/
MAGIC*
Perl_mg_findext(const SV *sv, int type, const MGVTBL *vtbl)
{
return S_mg_findext_flags(sv, type, vtbl, 1);
}
MAGIC *
Perl_mg_find_mglob(pTHX_ SV *sv)
{
PERL_ARGS_ASSERT_MG_FIND_MGLOB;
if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
/* This sv is only a delegate. //g magic must be attached to
its target. */
vivify_defelem(sv);
sv = LvTARG(sv);
}
if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
return S_mg_findext_flags(sv, PERL_MAGIC_regex_global, 0, 0);
return NULL;
}
/*
=for apidoc mg_copy
Copies the magic from one SV to another. See C<L</sv_magic>>.
=cut
*/
int
Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
{
int count = 0;
MAGIC* mg;
PERL_ARGS_ASSERT_MG_COPY;
for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
const MGVTBL* const vtbl = mg->mg_virtual;
if ((mg->mg_flags & MGf_COPY) && vtbl->svt_copy){
count += vtbl->svt_copy(aTHX_ sv, mg, nsv, key, klen);
}
else {
const char type = mg->mg_type;
if (isUPPER(type) && type != PERL_MAGIC_uvar) {
sv_magic(nsv,
(type == PERL_MAGIC_tied)
? SvTIED_obj(sv, mg)
: mg->mg_obj,
toLOWER(type), key, klen);
count++;
}
}
}
return count;
}
/*
=for apidoc mg_localize
Copy some of the magic from an existing SV to new localized version of that
SV. Container magic (I<e.g.>, C<%ENV>, C<$1>, C<tie>)
gets copied, value magic doesn't (I<e.g.>,
C<taint>, C<pos>).
If C<setmagic> is false then no set magic will be called on the new (empty) SV.
This typically means that assignment will soon follow (e.g. S<C<'local $x = $y'>>),
and that will handle the magic.
=cut
*/
void
Perl_mg_localize(pTHX_ SV *sv, SV *nsv, bool setmagic)
{
MAGIC *mg;
PERL_ARGS_ASSERT_MG_LOCALIZE;
if (nsv == DEFSV)
return;
for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
const MGVTBL* const vtbl = mg->mg_virtual;
if (PERL_MAGIC_TYPE_IS_VALUE_MAGIC(mg->mg_type))
continue;
if ((mg->mg_flags & MGf_LOCAL) && vtbl->svt_local)
(void)vtbl->svt_local(aTHX_ nsv, mg);
else
sv_magicext(nsv, mg->mg_obj, mg->mg_type, vtbl,
mg->mg_ptr, mg->mg_len);
/* container types should remain read-only across localization */
SvFLAGS(nsv) |= SvREADONLY(sv);
}
if (SvTYPE(nsv) >= SVt_PVMG && SvMAGIC(nsv)) {
SvFLAGS(nsv) |= SvMAGICAL(sv);
if (setmagic) {
PL_localizing = 1;
SvSETMAGIC(nsv);
PL_localizing = 0;
}
}
}
#define mg_free_struct(sv, mg) S_mg_free_struct(aTHX_ sv, mg)
static void
S_mg_free_struct(pTHX_ SV *sv, MAGIC *mg)
{
const MGVTBL* const vtbl = mg->mg_virtual;
if (vtbl && vtbl->svt_free)
vtbl->svt_free(aTHX_ sv, mg);
if (mg->mg_len > 0)
Safefree(mg->mg_ptr);
else if (mg->mg_len == HEf_SVKEY)
SvREFCNT_dec(MUTABLE_SV(mg->mg_ptr));
if (mg->mg_flags & MGf_REFCOUNTED)
SvREFCNT_dec(mg->mg_obj);
Safefree(mg);
}
/*
=for apidoc mg_free
Free any magic storage used by the SV. See C<L</sv_magic>>.
=cut
*/
int
Perl_mg_free(pTHX_ SV *sv)
{
MAGIC* mg;
MAGIC* moremagic;
PERL_ARGS_ASSERT_MG_FREE;
for (mg = SvMAGIC(sv); mg; mg = moremagic) {
moremagic = mg->mg_moremagic;
mg_free_struct(sv, mg);
SvMAGIC_set(sv, moremagic);
}
SvMAGIC_set(sv, NULL);
SvMAGICAL_off(sv);
return 0;
}
/*
=for apidoc mg_free_type
Remove any magic of type C<how> from the SV C<sv>. See L</sv_magic>.
=cut
*/
void
Perl_mg_free_type(pTHX_ SV *sv, int how)
{
MAGIC *mg, *prevmg, *moremg;
PERL_ARGS_ASSERT_MG_FREE_TYPE;
for (prevmg = NULL, mg = SvMAGIC(sv); mg; prevmg = mg, mg = moremg) {
moremg = mg->mg_moremagic;
if (mg->mg_type == how) {
MAGIC *newhead;
/* temporarily move to the head of the magic chain, in case
custom free code relies on this historical aspect of mg_free */
if (prevmg) {
prevmg->mg_moremagic = moremg;
mg->mg_moremagic = SvMAGIC(sv);
SvMAGIC_set(sv, mg);
}
newhead = mg->mg_moremagic;
mg_free_struct(sv, mg);
SvMAGIC_set(sv, newhead);
mg = prevmg;
}
}
mg_magical(sv);
}
/*
=for apidoc mg_freeext
Remove any magic of type C<how> using virtual table C<vtbl> from the
SV C<sv>. See L</sv_magic>.
C<mg_freeext(sv, how, NULL)> is equivalent to C<mg_free_type(sv, how)>.
=cut
*/
void
Perl_mg_freeext(pTHX_ SV *sv, int how, const MGVTBL *vtbl)
{
MAGIC *mg, *prevmg, *moremg;
PERL_ARGS_ASSERT_MG_FREEEXT;
for (prevmg = NULL, mg = SvMAGIC(sv); mg; prevmg = mg, mg = moremg) {
MAGIC *newhead;
moremg = mg->mg_moremagic;
if (mg->mg_type == how && (vtbl == NULL || mg->mg_virtual == vtbl)) {
/* temporarily move to the head of the magic chain, in case
custom free code relies on this historical aspect of mg_free */
if (prevmg) {
prevmg->mg_moremagic = moremg;
mg->mg_moremagic = SvMAGIC(sv);
SvMAGIC_set(sv, mg);
}
newhead = mg->mg_moremagic;
mg_free_struct(sv, mg);
SvMAGIC_set(sv, newhead);
mg = prevmg;
}
}
mg_magical(sv);
}
#include <signal.h>
U32
Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
{
PERL_UNUSED_ARG(sv);
PERL_ARGS_ASSERT_MAGIC_REGDATA_CNT;
if (PL_curpm) {
REGEXP * const rx = PM_GETRE(PL_curpm);
if (rx) {
const SSize_t n = (SSize_t)mg->mg_obj;
if (n == '+') { /* @+ */
/* return the number possible */
return RX_NPARENS(rx);
} else { /* @- @^CAPTURE @{^CAPTURE} */
I32 paren = RX_LASTPAREN(rx);
/* return the last filled */
while ( paren >= 0
&& (RX_OFFS(rx)[paren].start == -1
|| RX_OFFS(rx)[paren].end == -1) )
paren--;
if (n == '-') {
/* @- */
return (U32)paren;
} else {
/* @^CAPTURE @{^CAPTURE} */
return paren >= 0 ? (U32)(paren-1) : (U32)-1;
}
}
}
}
return (U32)-1;
}
/* @-, @+ */
int
Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
{
PERL_ARGS_ASSERT_MAGIC_REGDATUM_GET;
if (PL_curpm) {
REGEXP * const rx = PM_GETRE(PL_curpm);
if (rx) {
const SSize_t n = (SSize_t)mg->mg_obj;
/* @{^CAPTURE} does not contain $&, so we need to increment by 1 */
const I32 paren = mg->mg_len
+ (n == '\003' ? 1 : 0);
SSize_t s;
SSize_t t;
if (paren < 0)
return 0;
if (paren <= (I32)RX_NPARENS(rx) &&
(s = RX_OFFS(rx)[paren].start) != -1 &&
(t = RX_OFFS(rx)[paren].end) != -1)
{
SSize_t i;
if (n == '+') /* @+ */
i = t;
else if (n == '-') /* @- */
i = s;
else { /* @^CAPTURE @{^CAPTURE} */
CALLREG_NUMBUF_FETCH(rx,paren,sv);
return 0;
}
if (RX_MATCH_UTF8(rx)) {
const char * const b = RX_SUBBEG(rx);
if (b)
i = RX_SUBCOFFSET(rx) +
utf8_length((U8*)b,
(U8*)(b-RX_SUBOFFSET(rx)+i));
}
sv_setuv(sv, i);
return 0;
}
}
}
sv_set_undef(sv);
return 0;
}
/* @-, @+ */
int
Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
{
PERL_ARGS_ASSERT_MAGIC_REGDATUM_SET;
PERL_UNUSED_CONTEXT;
PERL_UNUSED_ARG(sv);
PERL_UNUSED_ARG(mg);
Perl_croak_no_modify();
NORETURN_FUNCTION_END;
}
#define SvRTRIM(sv) STMT_START { \
if (SvPOK(sv)) { \
STRLEN len = SvCUR(sv); \
char * const p = SvPVX(sv); \
while (len > 0 && isSPACE(p[len-1])) \
--len; \
SvCUR_set(sv, len); \
p[len] = '\0'; \
} \
} STMT_END
void
Perl_emulate_cop_io(pTHX_ const COP *const c, SV *const sv)
{
PERL_ARGS_ASSERT_EMULATE_COP_IO;
if (!(CopHINTS_get(c) & (HINT_LEXICAL_IO_IN|HINT_LEXICAL_IO_OUT)))
sv_set_undef(sv);
else {
SvPVCLEAR(sv);
SvUTF8_off(sv);
if ((CopHINTS_get(c) & HINT_LEXICAL_IO_IN)) {
SV *const value = cop_hints_fetch_pvs(c, "open<", 0);
assert(value);
sv_catsv(sv, value);
}
sv_catpvs(sv, "\0");
if ((CopHINTS_get(c) & HINT_LEXICAL_IO_OUT)) {
SV *const value = cop_hints_fetch_pvs(c, "open>", 0);
assert(value);
sv_catsv(sv, value);
}
}
}
STATIC void
S_fixup_errno_string(pTHX_ SV* sv)
{
/* Do what is necessary to fixup the non-empty string in 'sv' for return to
* Perl space. */
PERL_ARGS_ASSERT_FIXUP_ERRNO_STRING;
assert(SvOK(sv));
if(strEQ(SvPVX(sv), "")) {
sv_catpv(sv, UNKNOWN_ERRNO_MSG);
}
else {
/* In some locales the error string may come back as UTF-8, in which
* case we should turn on that flag. This didn't use to happen, and to
* avoid as many possible backward compatibility issues as possible, we
* don't turn on the flag unless we have to. So the flag stays off for
* an entirely invariant string. We assume that if the string looks
* like UTF-8 in a single script, it really is UTF-8: "text in any
* other encoding that uses bytes with the high bit set is extremely
* unlikely to pass a UTF-8 validity test"
* (http://en.wikipedia.org/wiki/Charset_detection). There is a
* potential that we will get it wrong however, especially on short
* error message text, so do an additional check. */
if ( ! IN_BYTES /* respect 'use bytes' */
&& is_utf8_non_invariant_string((U8*) SvPVX_const(sv), SvCUR(sv))
#ifdef USE_LOCALE_MESSAGES
&& _is_cur_LC_category_utf8(LC_MESSAGES)
#else /* If can't check directly, at least can see if script is consistent,
under UTF-8, which gives us an extra measure of confidence. */
&& isSCRIPT_RUN((const U8 *) SvPVX_const(sv), (U8 *) SvEND(sv),
TRUE) /* Means assume UTF-8 */
#endif
) {
SvUTF8_on(sv);
}
}
}
/*
=for apidoc_section Errno
=for apidoc sv_string_from_errnum
Generates the message string describing an OS error and returns it as
an SV. C<errnum> must be a value that C<errno> could take, identifying
the type of error.
If C<tgtsv> is non-null then the string will be written into that SV
(overwriting existing content) and it will be returned. If C<tgtsv>
is a null pointer then the string will be written into a new mortal SV
which will be returned.
The message will be taken from whatever locale would be used by C<$!>,
and will be encoded in the SV in whatever manner would be used by C<$!>.
The details of this process are subject to future change. Currently,
the message is taken from the C locale by default (usually producing an
English message), and from the currently selected locale when in the scope
of the C<use locale> pragma. A heuristic attempt is made to decode the
message from the locale's character encoding, but it will only be decoded
as either UTF-8 or ISO-8859-1. It is always correctly decoded in a UTF-8
locale, usually in an ISO-8859-1 locale, and never in any other locale.
The SV is always returned containing an actual string, and with no other
OK bits set. Unlike C<$!>, a message is even yielded for C<errnum> zero
(meaning success), and if no useful message is available then a useless
string (currently empty) is returned.
=cut
*/
SV *
Perl_sv_string_from_errnum(pTHX_ int errnum, SV *tgtsv)
{
char const *errstr;
if(!tgtsv)
tgtsv = sv_newmortal();
errstr = my_strerror(errnum);
if(errstr) {
sv_setpv(tgtsv, errstr);
fixup_errno_string(tgtsv);
} else {
SvPVCLEAR(tgtsv);
}
return tgtsv;
}
#ifdef VMS
#include <descrip.h>
#include <starlet.h>
#endif
int
Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
{
I32 paren;
const char *s = NULL;
REGEXP *rx;
const char * const remaining = mg->mg_ptr + 1;
char nextchar;
PERL_ARGS_ASSERT_MAGIC_GET;
if (!mg->mg_ptr) {
paren = mg->mg_len;
if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
do_numbuf_fetch:
CALLREG_NUMBUF_FETCH(rx,paren,sv);
}
else
goto set_undef;
return 0;
}
nextchar = *remaining;
switch (*mg->mg_ptr) {
case '\001': /* ^A */
if (SvOK(PL_bodytarget)) sv_copypv(sv, PL_bodytarget);
else
sv_set_undef(sv);
if (SvTAINTED(PL_bodytarget))
SvTAINTED_on(sv);
break;
case '\003': /* ^C, ^CHILD_ERROR_NATIVE */
if (nextchar == '\0') {
sv_setiv(sv, (IV)PL_minus_c);
}
else if (strEQ(remaining, "HILD_ERROR_NATIVE")) {
sv_setiv(sv, (IV)STATUS_NATIVE);
}
break;
case '\004': /* ^D */
sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
break;
case '\005': /* ^E */
if (nextchar != '\0') {
if (strEQ(remaining, "NCODING"))
sv_set_undef(sv);
break;
}
#if defined(VMS) || defined(OS2) || defined(WIN32)
# if defined(VMS)
{
char msg[255];
$DESCRIPTOR(msgdsc,msg);
sv_setnv(sv,(NV) vaxc$errno);
if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
else
SvPVCLEAR(sv);
}
#elif defined(OS2)
if (!(_emx_env & 0x200)) { /* Under DOS */
sv_setnv(sv, (NV)errno);
sv_setpv(sv, errno ? my_strerror(errno) : "");
} else {
if (errno != errno_isOS2) {
const int tmp = _syserrno();
if (tmp) /* 2nd call to _syserrno() makes it 0 */
Perl_rc = tmp;
}
sv_setnv(sv, (NV)Perl_rc);
sv_setpv(sv, os2error(Perl_rc));
}
if (SvOK(sv) && strNE(SvPVX(sv), "")) {
fixup_errno_string(sv);
}
# elif defined(WIN32)
{
const DWORD dwErr = GetLastError();
sv_setnv(sv, (NV)dwErr);
if (dwErr) {
PerlProc_GetOSError(sv, dwErr);
fixup_errno_string(sv);
}
else
SvPVCLEAR(sv);
SetLastError(dwErr);
}
# else
# error Missing code for platform
# endif
SvRTRIM(sv);
SvNOK_on(sv); /* what a wonderful hack! */
break;
#endif /* End of platforms with special handling for $^E; others just fall
through to $! */
/* FALLTHROUGH */
case '!':
{
dSAVE_ERRNO;
#ifdef VMS
sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
#else