forked from Perl/perl5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperlio.c
5255 lines (4882 loc) · 123 KB
/
perlio.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
/*
* perlio.c
* Copyright (c) 1996-2006, Nick Ing-Simmons
* Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011 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.
*/
/*
* Hour after hour for nearly three weary days he had jogged up and down,
* over passes, and through long dales, and across many streams.
*
* [pp.791-792 of _The Lord of the Rings_, V/iii: "The Muster of Rohan"]
*/
/* This file contains the functions needed to implement PerlIO, which
* is Perl's private replacement for the C stdio library. This is used
* by default unless you compile with -Uuseperlio or run with
* PERLIO=:stdio (but don't do this unless you know what you're doing)
*/
/*
* If we have ActivePerl-like PERL_IMPLICIT_SYS then we need a dTHX to get
* at the dispatch tables, even when we do not need it for other reasons.
* Invent a dSYS macro to abstract this out
*/
#ifdef PERL_IMPLICIT_SYS
#define dSYS dTHX
#else
#define dSYS dNOOP
#endif
#define PERLIO_NOT_STDIO 0
/*
* This file provides those parts of PerlIO abstraction
* which are not #defined in perlio.h.
* Which these are depends on various Configure #ifdef's
*/
#include "EXTERN.h"
#define PERL_IN_PERLIO_C
#include "perl.h"
#ifdef PERL_IMPLICIT_CONTEXT
#undef dSYS
#define dSYS dTHX
#endif
#include "XSUB.h"
#ifdef VMS
#include <rms.h>
#endif
#define PerlIO_lockcnt(f) (((PerlIOl*)(f))->head->flags)
/* Call the callback or PerlIOBase, and return failure. */
#define Perl_PerlIO_or_Base(f, callback, base, failure, args) \
if (PerlIOValid(f)) { \
const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
if (tab && tab->callback) \
return (*tab->callback) args; \
else \
return PerlIOBase_ ## base args; \
} \
else \
SETERRNO(EBADF, SS_IVCHAN); \
return failure
/* Call the callback or fail, and return failure. */
#define Perl_PerlIO_or_fail(f, callback, failure, args) \
if (PerlIOValid(f)) { \
const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
if (tab && tab->callback) \
return (*tab->callback) args; \
SETERRNO(EINVAL, LIB_INVARG); \
} \
else \
SETERRNO(EBADF, SS_IVCHAN); \
return failure
/* Call the callback or PerlIOBase, and be void. */
#define Perl_PerlIO_or_Base_void(f, callback, base, args) \
if (PerlIOValid(f)) { \
const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
if (tab && tab->callback) \
(*tab->callback) args; \
else \
PerlIOBase_ ## base args; \
} \
else \
SETERRNO(EBADF, SS_IVCHAN)
/* Call the callback or fail, and be void. */
#define Perl_PerlIO_or_fail_void(f, callback, args) \
if (PerlIOValid(f)) { \
const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
if (tab && tab->callback) \
(*tab->callback) args; \
else \
SETERRNO(EINVAL, LIB_INVARG); \
} \
else \
SETERRNO(EBADF, SS_IVCHAN)
#if defined(__osf__) && _XOPEN_SOURCE < 500
extern int fseeko(FILE *, off_t, int);
extern off_t ftello(FILE *);
#endif
#define NATIVE_0xd CR_NATIVE
#define NATIVE_0xa LF_NATIVE
EXTERN_C int perlsio_binmode(FILE *fp, int iotype, int mode);
int
perlsio_binmode(FILE *fp, int iotype, int mode)
{
/*
* This used to be contents of do_binmode in doio.c
*/
#ifdef DOSISH
dTHX;
PERL_UNUSED_ARG(iotype);
#ifdef NETWARE
if (PerlLIO_setmode(fp, mode) != -1) {
#else
if (PerlLIO_setmode(fileno(fp), mode) != -1) {
#endif
return 1;
}
else
return 0;
#else
# if defined(USEMYBINMODE)
dTHX;
# if defined(__CYGWIN__)
PERL_UNUSED_ARG(iotype);
# endif
if (my_binmode(fp, iotype, mode) != FALSE)
return 1;
else
return 0;
# else
PERL_UNUSED_ARG(fp);
PERL_UNUSED_ARG(iotype);
PERL_UNUSED_ARG(mode);
return 1;
# endif
#endif
}
#ifndef O_ACCMODE
#define O_ACCMODE 3 /* Assume traditional implementation */
#endif
int
PerlIO_intmode2str(int rawmode, char *mode, int *writing)
{
const int result = rawmode & O_ACCMODE;
int ix = 0;
int ptype;
switch (result) {
case O_RDONLY:
ptype = IoTYPE_RDONLY;
break;
case O_WRONLY:
ptype = IoTYPE_WRONLY;
break;
case O_RDWR:
default:
ptype = IoTYPE_RDWR;
break;
}
if (writing)
*writing = (result != O_RDONLY);
if (result == O_RDONLY) {
mode[ix++] = 'r';
}
#ifdef O_APPEND
else if (rawmode & O_APPEND) {
mode[ix++] = 'a';
if (result != O_WRONLY)
mode[ix++] = '+';
}
#endif
else {
if (result == O_WRONLY)
mode[ix++] = 'w';
else {
mode[ix++] = 'r';
mode[ix++] = '+';
}
}
#if O_BINARY != 0
/* Unless O_BINARY is different from zero, bit-and:ing
* with it won't do much good. */
if (rawmode & O_BINARY)
mode[ix++] = 'b';
# endif
mode[ix] = '\0';
return ptype;
}
#ifndef PERLIO_LAYERS
int
PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
{
if (!names || !*names
|| strEQ(names, ":crlf")
|| strEQ(names, ":raw")
|| strEQ(names, ":bytes")
) {
return 0;
}
Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl", names);
/*
* NOTREACHED
*/
return -1;
}
void
PerlIO_destruct(pTHX)
{
}
int
PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
{
return perlsio_binmode(fp, iotype, mode);
}
PerlIO *
PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
{
#if defined(PERL_MICRO)
return NULL;
#elif defined(PERL_IMPLICIT_SYS)
return PerlSIO_fdupopen(f);
#else
# ifdef WIN32
return win32_fdupopen(f);
# else
if (f) {
const int fd = PerlLIO_dup_cloexec(PerlIO_fileno(f));
if (fd >= 0) {
char mode[8];
# ifdef DJGPP
const int omode = djgpp_get_stream_mode(f);
# else
const int omode = fcntl(fd, F_GETFL);
# endif
PerlIO_intmode2str(omode,mode,NULL);
/* the r+ is a hack */
return PerlIO_fdopen(fd, mode);
}
return NULL;
}
else {
SETERRNO(EBADF, SS_IVCHAN);
}
# endif
return NULL;
#endif
}
/*
* De-mux PerlIO_openn() into fdopen, freopen and fopen type entries
*/
PerlIO *
PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
int imode, int perm, PerlIO *old, int narg, SV **args)
{
if (narg) {
if (narg > 1) {
Perl_croak(aTHX_ "More than one argument to open");
}
if (*args == &PL_sv_undef)
return PerlIO_tmpfile();
else {
STRLEN len;
const char *name = SvPV_const(*args, len);
if (!IS_SAFE_PATHNAME(name, len, "open"))
return NULL;
if (*mode == IoTYPE_NUMERIC) {
fd = PerlLIO_open3_cloexec(name, imode, perm);
if (fd >= 0)
return PerlIO_fdopen(fd, mode + 1);
}
else if (old) {
return PerlIO_reopen(name, mode, old);
}
else {
return PerlIO_open(name, mode);
}
}
}
else {
return PerlIO_fdopen(fd, (char *) mode);
}
return NULL;
}
XS(XS_PerlIO__Layer__find); /* prototype to pass -Wmissing-prototypes */
XS(XS_PerlIO__Layer__find)
{
dXSARGS;
if (items < 2)
Perl_croak(aTHX_ "Usage class->find(name[,load])");
else {
const char * const name = SvPV_nolen_const(ST(1));
ST(0) = (strEQ(name, "crlf")
|| strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef;
XSRETURN(1);
}
}
void
Perl_boot_core_PerlIO(pTHX)
{
newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
}
#endif
/*======================================================================================*/
/*
* Implement all the PerlIO interface ourselves.
*/
#include "perliol.h"
void
PerlIO_debug(const char *fmt, ...)
{
va_list ap;
dSYS;
if (!DEBUG_i_TEST)
return;
va_start(ap, fmt);
if (!PL_perlio_debug_fd) {
if (!TAINTING_get &&
PerlProc_getuid() == PerlProc_geteuid() &&
PerlProc_getgid() == PerlProc_getegid()) {
const char * const s = PerlEnv_getenv("PERLIO_DEBUG");
if (s && *s)
PL_perlio_debug_fd = PerlLIO_open3_cloexec(s,
O_WRONLY | O_CREAT | O_APPEND, 0666);
else
PL_perlio_debug_fd = PerlLIO_dup_cloexec(2); /* stderr */
} else {
/* tainting or set*id, so ignore the environment and send the
debug output to stderr, like other -D switches. */
PL_perlio_debug_fd = PerlLIO_dup_cloexec(2); /* stderr */
}
}
if (PL_perlio_debug_fd > 0) {
#ifdef USE_ITHREADS
const char * const s = CopFILE(PL_curcop);
/* Use fixed buffer as sv_catpvf etc. needs SVs */
char buffer[1024];
const STRLEN len1 = my_snprintf(buffer, sizeof(buffer), "%.40s:%" IVdf " ", s ? s : "(none)", (IV) CopLINE(PL_curcop));
# ifdef USE_QUADMATH
# ifdef HAS_VSNPRINTF
/* my_vsnprintf() isn't available with quadmath, but the native vsnprintf()
should be, otherwise the system isn't likely to support quadmath.
Nothing should be calling PerlIO_debug() with floating point anyway.
*/
const STRLEN len2 = vsnprintf(buffer + len1, sizeof(buffer) - len1, fmt, ap);
# else
STATIC_ASSERT_STMT(0);
# endif
# else
const STRLEN len2 = my_vsnprintf(buffer + len1, sizeof(buffer) - len1, fmt, ap);
# endif
PERL_UNUSED_RESULT(PerlLIO_write(PL_perlio_debug_fd, buffer, len1 + len2));
#else
const char *s = CopFILE(PL_curcop);
STRLEN len;
SV * const sv = Perl_newSVpvf(aTHX_ "%s:%" IVdf " ", s ? s : "(none)",
(IV) CopLINE(PL_curcop));
Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap);
s = SvPV_const(sv, len);
PERL_UNUSED_RESULT(PerlLIO_write(PL_perlio_debug_fd, s, len));
SvREFCNT_dec(sv);
#endif
}
va_end(ap);
}
/*--------------------------------------------------------------------------------------*/
/*
* Inner level routines
*/
/* check that the head field of each layer points back to the head */
#ifdef DEBUGGING
# define VERIFY_HEAD(f) PerlIO_verify_head(aTHX_ f)
static void
PerlIO_verify_head(pTHX_ PerlIO *f)
{
PerlIOl *head, *p;
int seen = 0;
#ifndef PERL_IMPLICIT_SYS
PERL_UNUSED_CONTEXT;
#endif
if (!PerlIOValid(f))
return;
p = head = PerlIOBase(f)->head;
assert(p);
do {
assert(p->head == head);
if (p == (PerlIOl*)f)
seen = 1;
p = p->next;
} while (p);
assert(seen);
}
#else
# define VERIFY_HEAD(f)
#endif
/*
* Table of pointers to the PerlIO structs (malloc'ed)
*/
#define PERLIO_TABLE_SIZE 64
static void
PerlIO_init_table(pTHX)
{
if (PL_perlio)
return;
Newxz(PL_perlio, PERLIO_TABLE_SIZE, PerlIOl);
}
PerlIO *
PerlIO_allocate(pTHX)
{
/*
* Find a free slot in the table, allocating new table as necessary
*/
PerlIOl **last;
PerlIOl *f;
last = &PL_perlio;
while ((f = *last)) {
int i;
last = (PerlIOl **) (f);
for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
if (!((++f)->next)) {
goto good_exit;
}
}
}
Newxz(f,PERLIO_TABLE_SIZE,PerlIOl);
if (!f) {
return NULL;
}
*last = (PerlIOl*) f++;
good_exit:
f->flags = 0; /* lockcnt */
f->tab = NULL;
f->head = f;
return (PerlIO*) f;
}
#undef PerlIO_fdupopen
PerlIO *
PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
{
if (PerlIOValid(f)) {
const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
DEBUG_i( PerlIO_debug("fdupopen f=%p param=%p\n",(void*)f,(void*)param) );
if (tab && tab->Dup)
return (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX), f, param, flags);
else {
return PerlIOBase_dup(aTHX_ PerlIO_allocate(aTHX), f, param, flags);
}
}
else
SETERRNO(EBADF, SS_IVCHAN);
return NULL;
}
void
PerlIO_cleantable(pTHX_ PerlIOl **tablep)
{
PerlIOl * const table = *tablep;
if (table) {
int i;
PerlIO_cleantable(aTHX_(PerlIOl **) & (table[0]));
for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) {
PerlIOl * const f = table + i;
if (f->next) {
PerlIO_close(&(f->next));
}
}
Safefree(table);
*tablep = NULL;
}
}
PerlIO_list_t *
PerlIO_list_alloc(pTHX)
{
PerlIO_list_t *list;
PERL_UNUSED_CONTEXT;
Newxz(list, 1, PerlIO_list_t);
list->refcnt = 1;
return list;
}
void
PerlIO_list_free(pTHX_ PerlIO_list_t *list)
{
if (list) {
if (--list->refcnt == 0) {
if (list->array) {
IV i;
for (i = 0; i < list->cur; i++)
SvREFCNT_dec(list->array[i].arg);
Safefree(list->array);
}
Safefree(list);
}
}
}
void
PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
{
PerlIO_pair_t *p;
PERL_UNUSED_CONTEXT;
if (list->cur >= list->len) {
const IV new_len = list->len + 8;
if (list->array)
Renew(list->array, new_len, PerlIO_pair_t);
else
Newx(list->array, new_len, PerlIO_pair_t);
list->len = new_len;
}
p = &(list->array[list->cur++]);
p->funcs = funcs;
if ((p->arg = arg)) {
SvREFCNT_inc_simple_void_NN(arg);
}
}
PerlIO_list_t *
PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param)
{
PerlIO_list_t *list = NULL;
if (proto) {
int i;
list = PerlIO_list_alloc(aTHX);
for (i=0; i < proto->cur; i++) {
SV *arg = proto->array[i].arg;
#ifdef USE_ITHREADS
if (arg && param)
arg = sv_dup(arg, param);
#else
PERL_UNUSED_ARG(param);
#endif
PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
}
}
return list;
}
void
PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param)
{
#ifdef USE_ITHREADS
PerlIOl **table = &proto->Iperlio;
PerlIOl *f;
PL_perlio = NULL;
PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param);
PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param);
PerlIO_init_table(aTHX);
DEBUG_i( PerlIO_debug("Clone %p from %p\n",(void*)aTHX,(void*)proto) );
while ((f = *table)) {
int i;
table = (PerlIOl **) (f++);
for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
if (f->next) {
(void) fp_dup(&(f->next), 0, param);
}
f++;
}
}
#else
PERL_UNUSED_CONTEXT;
PERL_UNUSED_ARG(proto);
PERL_UNUSED_ARG(param);
#endif
}
void
PerlIO_destruct(pTHX)
{
PerlIOl **table = &PL_perlio;
PerlIOl *f;
#ifdef USE_ITHREADS
DEBUG_i( PerlIO_debug("Destruct %p\n",(void*)aTHX) );
#endif
while ((f = *table)) {
int i;
table = (PerlIOl **) (f++);
for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
PerlIO *x = &(f->next);
const PerlIOl *l;
while ((l = *x)) {
if (l->tab && l->tab->kind & PERLIO_K_DESTRUCT) {
DEBUG_i( PerlIO_debug("Destruct popping %s\n", l->tab->name) );
PerlIO_flush(x);
PerlIO_pop(aTHX_ x);
}
else {
x = PerlIONext(x);
}
}
f++;
}
}
}
void
PerlIO_pop(pTHX_ PerlIO *f)
{
const PerlIOl *l = *f;
VERIFY_HEAD(f);
if (l) {
DEBUG_i( PerlIO_debug("PerlIO_pop f=%p %s\n", (void*)f,
l->tab ? l->tab->name : "(Null)") );
if (l->tab && l->tab->Popped) {
/*
* If popped returns non-zero do not free its layer structure
* it has either done so itself, or it is shared and still in
* use
*/
if ((*l->tab->Popped) (aTHX_ f) != 0)
return;
}
if (PerlIO_lockcnt(f)) {
/* we're in use; defer freeing the structure */
PerlIOBase(f)->flags = PERLIO_F_CLEARED;
PerlIOBase(f)->tab = NULL;
}
else {
*f = l->next;
Safefree(l);
}
}
}
/* Return as an array the stack of layers on a filehandle. Note that
* the stack is returned top-first in the array, and there are three
* times as many array elements as there are layers in the stack: the
* first element of a layer triplet is the name, the second one is the
* arguments, and the third one is the flags. */
AV *
PerlIO_get_layers(pTHX_ PerlIO *f)
{
AV * const av = newAV();
if (PerlIOValid(f)) {
PerlIOl *l = PerlIOBase(f);
while (l) {
/* There is some collusion in the implementation of
XS_PerlIO_get_layers - it knows that name and flags are
generated as fresh SVs here, and takes advantage of that to
"copy" them by taking a reference. If it changes here, it needs
to change there too. */
SV * const name = l->tab && l->tab->name ?
newSVpv(l->tab->name, 0) : &PL_sv_undef;
SV * const arg = l->tab && l->tab->Getarg ?
(*l->tab->Getarg)(aTHX_ &l, 0, 0) : &PL_sv_undef;
av_push(av, name);
av_push(av, arg);
av_push(av, newSViv((IV)l->flags));
l = l->next;
}
}
return av;
}
/*--------------------------------------------------------------------------------------*/
/*
* XS Interface for perl code
*/
PerlIO_funcs *
PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load)
{
IV i;
if ((SSize_t) len <= 0)
len = strlen(name);
for (i = 0; i < PL_known_layers->cur; i++) {
PerlIO_funcs * const f = PL_known_layers->array[i].funcs;
const STRLEN this_len = strlen(f->name);
if (this_len == len && memEQ(f->name, name, len)) {
DEBUG_i( PerlIO_debug("%.*s => %p\n", (int) len, name, (void*)f) );
return f;
}
}
if (load && PL_subname && PL_def_layerlist
&& PL_def_layerlist->cur >= 2) {
if (PL_in_load_module) {
Perl_croak(aTHX_ "Recursive call to Perl_load_module in PerlIO_find_layer");
return NULL;
} else {
SV * const pkgsv = newSVpvs("PerlIO");
SV * const layer = newSVpvn(name, len);
CV * const cv = get_cvs("PerlIO::Layer::NoWarnings", 0);
ENTER;
SAVEBOOL(PL_in_load_module);
if (cv) {
SAVEGENERICSV(PL_warnhook);
PL_warnhook = MUTABLE_SV((SvREFCNT_inc_simple_NN(cv)));
}
PL_in_load_module = TRUE;
/*
* The two SVs are magically freed by load_module
*/
Perl_load_module(aTHX_ 0, pkgsv, NULL, layer, NULL);
LEAVE;
return PerlIO_find_layer(aTHX_ name, len, 0);
}
}
DEBUG_i( PerlIO_debug("Cannot find %.*s\n", (int) len, name) );
return NULL;
}
#ifdef USE_ATTRIBUTES_FOR_PERLIO
static int
perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
{
if (SvROK(sv)) {
IO * const io = GvIOn(MUTABLE_GV(SvRV(sv)));
PerlIO * const ifp = IoIFP(io);
PerlIO * const ofp = IoOFP(io);
Perl_warn(aTHX_ "set %" SVf " %p %p %p",
SVfARG(sv), (void*)io, (void*)ifp, (void*)ofp);
}
return 0;
}
static int
perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
{
if (SvROK(sv)) {
IO * const io = GvIOn(MUTABLE_GV(SvRV(sv)));
PerlIO * const ifp = IoIFP(io);
PerlIO * const ofp = IoOFP(io);
Perl_warn(aTHX_ "get %" SVf " %p %p %p",
SVfARG(sv), (void*)io, (void*)ifp, (void*)ofp);
}
return 0;
}
static int
perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
{
Perl_warn(aTHX_ "clear %" SVf, SVfARG(sv));
return 0;
}
static int
perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
{
Perl_warn(aTHX_ "free %" SVf, SVfARG(sv));
return 0;
}
MGVTBL perlio_vtab = {
perlio_mg_get,
perlio_mg_set,
NULL, /* len */
perlio_mg_clear,
perlio_mg_free
};
XS(XS_io_MODIFY_SCALAR_ATTRIBUTES); /* prototype to pass -Wmissing-prototypes */
XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
{
dXSARGS;
SV * const sv = SvRV(ST(1));
AV * const av = newAV();
MAGIC *mg;
int count = 0;
int i;
sv_magic(sv, MUTABLE_SV(av), PERL_MAGIC_ext, NULL, 0);
SvRMAGICAL_off(sv);
mg = mg_find(sv, PERL_MAGIC_ext);
mg->mg_virtual = &perlio_vtab;
mg_magical(sv);
Perl_warn(aTHX_ "attrib %" SVf, SVfARG(sv));
for (i = 2; i < items; i++) {
STRLEN len;
const char * const name = SvPV_const(ST(i), len);
SV * const layer = PerlIO_find_layer(aTHX_ name, len, 1);
if (layer) {
av_push(av, SvREFCNT_inc_simple_NN(layer));
}
else {
ST(count) = ST(i);
count++;
}
}
SvREFCNT_dec(av);
XSRETURN(count);
}
#endif /* USE_ATTRIBUTES_FOR_PERLIO */
SV *
PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
{
HV * const stash = gv_stashpvs("PerlIO::Layer", GV_ADD);
SV * const sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash);
return sv;
}
XS(XS_PerlIO__Layer__NoWarnings); /* prototype to pass -Wmissing-prototypes */
XS(XS_PerlIO__Layer__NoWarnings)
{
/* This is used as a %SIG{__WARN__} handler to suppress warnings
during loading of layers.
*/
dXSARGS;
PERL_UNUSED_VAR(items);
DEBUG_i(
if (items)
PerlIO_debug("warning:%s\n",SvPV_nolen_const(ST(0))) );
XSRETURN(0);
}
XS(XS_PerlIO__Layer__find); /* prototype to pass -Wmissing-prototypes */
XS(XS_PerlIO__Layer__find)
{
dXSARGS;
if (items < 2)
Perl_croak(aTHX_ "Usage class->find(name[,load])");
else {
STRLEN len;
const char * const name = SvPV_const(ST(1), len);
const bool load = (items > 2) ? SvTRUE_NN(ST(2)) : 0;
PerlIO_funcs * const layer = PerlIO_find_layer(aTHX_ name, len, load);
ST(0) =
(layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) :
&PL_sv_undef;
XSRETURN(1);
}
}
void
PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
{
if (!PL_known_layers)
PL_known_layers = PerlIO_list_alloc(aTHX);
PerlIO_list_push(aTHX_ PL_known_layers, tab, NULL);
DEBUG_i( PerlIO_debug("define %s %p\n", tab->name, (void*)tab) );
}
int
PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
{
if (names) {
const char *s = names;
while (*s) {
while (isSPACE(*s) || *s == ':')
s++;
if (*s) {
STRLEN llen = 0;
const char *e = s;
const char *as = NULL;
STRLEN alen = 0;
if (!isIDFIRST(*s)) {
/*
* Message is consistent with how attribute lists are
* passed. Even though this means "foo : : bar" is
* seen as an invalid separator character.
*/
const char q = ((*s == '\'') ? '"' : '\'');
Perl_ck_warner(aTHX_ packWARN(WARN_LAYER),
"Invalid separator character %c%c%c in PerlIO layer specification %s",
q, *s, q, s);
SETERRNO(EINVAL, LIB_INVARG);
return -1;
}
do {
e++;
} while (isWORDCHAR(*e));
llen = e - s;
if (*e == '(') {
int nesting = 1;
as = ++e;
while (nesting) {
switch (*e++) {
case ')':
if (--nesting == 0)
alen = (e - 1) - as;
break;
case '(':
++nesting;
break;
case '\\':
/*
* It's a nul terminated string, not allowed
* to \ the terminating null. Anything other
* character is passed over.
*/
if (*e++) {
break;
}
/* Fall through */
case '\0':
e--;
Perl_ck_warner(aTHX_ packWARN(WARN_LAYER),
"Argument list not closed for PerlIO layer \"%.*s\"",
(int) (e - s), s);
return -1;
default:
/*
* boring.
*/
break;
}
}
}
if (e > s) {
PerlIO_funcs * const layer =
PerlIO_find_layer(aTHX_ s, llen, 1);
if (layer) {
SV *arg = NULL;
if (as)
arg = newSVpvn(as, alen);
PerlIO_list_push(aTHX_ av, layer,
(arg) ? arg : &PL_sv_undef);
SvREFCNT_dec(arg);
}
else {
Perl_ck_warner(aTHX_ packWARN(WARN_LAYER), "Unknown PerlIO layer \"%.*s\"",
(int) llen, s);
return -1;
}
}
s = e;
}
}
}
return 0;
}
void
PerlIO_default_buffer(pTHX_ PerlIO_list_t *av)
{
PERLIO_FUNCS_DECL(*tab) = &PerlIO_perlio;
#ifdef PERLIO_USING_CRLF
tab = &PerlIO_crlf;
#else
if (PerlIO_stdio.Set_ptrcnt)
tab = &PerlIO_stdio;
#endif
DEBUG_i( PerlIO_debug("Pushing %s\n", tab->name) );
PerlIO_list_push(aTHX_ av, (PerlIO_funcs *)tab, &PL_sv_undef);
}
SV *
PerlIO_arg_fetch(PerlIO_list_t *av, IV n)
{
return av->array[n].arg;
}