xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/pod/perliol.pod (revision 0:68f95e015346)
1*0Sstevel@tonic-gate=head1 NAME
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateperliol - C API for Perl's implementation of IO in Layers.
4*0Sstevel@tonic-gate
5*0Sstevel@tonic-gate=head1 SYNOPSIS
6*0Sstevel@tonic-gate
7*0Sstevel@tonic-gate    /* Defining a layer ... */
8*0Sstevel@tonic-gate    #include <perliol.h>
9*0Sstevel@tonic-gate
10*0Sstevel@tonic-gate=head1 DESCRIPTION
11*0Sstevel@tonic-gate
12*0Sstevel@tonic-gateThis document describes the behavior and implementation of the PerlIO
13*0Sstevel@tonic-gateabstraction described in L<perlapio> when C<USE_PERLIO> is defined (and
14*0Sstevel@tonic-gateC<USE_SFIO> is not).
15*0Sstevel@tonic-gate
16*0Sstevel@tonic-gate=head2 History and Background
17*0Sstevel@tonic-gate
18*0Sstevel@tonic-gateThe PerlIO abstraction was introduced in perl5.003_02 but languished as
19*0Sstevel@tonic-gatejust an abstraction until perl5.7.0. However during that time a number
20*0Sstevel@tonic-gateof perl extensions switched to using it, so the API is mostly fixed to
21*0Sstevel@tonic-gatemaintain (source) compatibility.
22*0Sstevel@tonic-gate
23*0Sstevel@tonic-gateThe aim of the implementation is to provide the PerlIO API in a flexible
24*0Sstevel@tonic-gateand platform neutral manner. It is also a trial of an "Object Oriented
25*0Sstevel@tonic-gateC, with vtables" approach which may be applied to perl6.
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate=head2 Basic Structure
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gatePerlIO is a stack of layers.
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gateThe low levels of the stack work with the low-level operating system
32*0Sstevel@tonic-gatecalls (file descriptors in C) getting bytes in and out, the higher
33*0Sstevel@tonic-gatelayers of the stack buffer, filter, and otherwise manipulate the I/O,
34*0Sstevel@tonic-gateand return characters (or bytes) to Perl.  Terms I<above> and I<below>
35*0Sstevel@tonic-gateare used to refer to the relative positioning of the stack layers.
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gateA layer contains a "vtable", the table of I/O operations (at C level
38*0Sstevel@tonic-gatea table of function pointers), and status flags.  The functions in the
39*0Sstevel@tonic-gatevtable implement operations like "open", "read", and "write".
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gateWhen I/O, for example "read", is requested, the request goes from Perl
42*0Sstevel@tonic-gatefirst down the stack using "read" functions of each layer, then at the
43*0Sstevel@tonic-gatebottom the input is requested from the operating system services, then
44*0Sstevel@tonic-gatethe result is returned up the stack, finally being interpreted as Perl
45*0Sstevel@tonic-gatedata.
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gateThe requests do not necessarily go always all the way down to the
48*0Sstevel@tonic-gateoperating system: that's where PerlIO buffering comes into play.
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gateWhen you do an open() and specify extra PerlIO layers to be deployed,
51*0Sstevel@tonic-gatethe layers you specify are "pushed" on top of the already existing
52*0Sstevel@tonic-gatedefault stack.  One way to see it is that "operating system is
53*0Sstevel@tonic-gateon the left" and "Perl is on the right".
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gateWhat exact layers are in this default stack depends on a lot of
56*0Sstevel@tonic-gatethings: your operating system, Perl version, Perl compile time
57*0Sstevel@tonic-gateconfiguration, and Perl runtime configuration.  See L<PerlIO>,
58*0Sstevel@tonic-gateL<perlrun/PERLIO>, and L<open> for more information.
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gatebinmode() operates similarly to open(): by default the specified
61*0Sstevel@tonic-gatelayers are pushed on top of the existing stack.
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gateHowever, note that even as the specified layers are "pushed on top"
64*0Sstevel@tonic-gatefor open() and binmode(), this doesn't mean that the effects are
65*0Sstevel@tonic-gatelimited to the "top": PerlIO layers can be very 'active' and inspect
66*0Sstevel@tonic-gateand affect layers also deeper in the stack.  As an example there
67*0Sstevel@tonic-gateis a layer called "raw" which repeatedly "pops" layers until
68*0Sstevel@tonic-gateit reaches the first layer that has declared itself capable of
69*0Sstevel@tonic-gatehandling binary data.  The "pushed" layers are processed in left-to-right
70*0Sstevel@tonic-gateorder.
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gatesysopen() operates (unsurprisingly) at a lower level in the stack than
73*0Sstevel@tonic-gateopen().  For example in UNIX or UNIX-like systems sysopen() operates
74*0Sstevel@tonic-gatedirectly at the level of file descriptors: in the terms of PerlIO
75*0Sstevel@tonic-gatelayers, it uses only the "unix" layer, which is a rather thin wrapper
76*0Sstevel@tonic-gateon top of the UNIX file descriptors.
77*0Sstevel@tonic-gate
78*0Sstevel@tonic-gate=head2 Layers vs Disciplines
79*0Sstevel@tonic-gate
80*0Sstevel@tonic-gateInitial discussion of the ability to modify IO streams behaviour used
81*0Sstevel@tonic-gatethe term "discipline" for the entities which were added. This came (I
82*0Sstevel@tonic-gatebelieve) from the use of the term in "sfio", which in turn borrowed it
83*0Sstevel@tonic-gatefrom "line disciplines" on Unix terminals. However, this document (and
84*0Sstevel@tonic-gatethe C code) uses the term "layer".
85*0Sstevel@tonic-gate
86*0Sstevel@tonic-gateThis is, I hope, a natural term given the implementation, and should
87*0Sstevel@tonic-gateavoid connotations that are inherent in earlier uses of "discipline"
88*0Sstevel@tonic-gatefor things which are rather different.
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate=head2 Data Structures
91*0Sstevel@tonic-gate
92*0Sstevel@tonic-gateThe basic data structure is a PerlIOl:
93*0Sstevel@tonic-gate
94*0Sstevel@tonic-gate	typedef struct _PerlIO PerlIOl;
95*0Sstevel@tonic-gate	typedef struct _PerlIO_funcs PerlIO_funcs;
96*0Sstevel@tonic-gate	typedef PerlIOl *PerlIO;
97*0Sstevel@tonic-gate
98*0Sstevel@tonic-gate	struct _PerlIO
99*0Sstevel@tonic-gate	{
100*0Sstevel@tonic-gate	 PerlIOl *	next;       /* Lower layer */
101*0Sstevel@tonic-gate	 PerlIO_funcs *	tab;        /* Functions for this layer */
102*0Sstevel@tonic-gate	 IV		flags;      /* Various flags for state */
103*0Sstevel@tonic-gate	};
104*0Sstevel@tonic-gate
105*0Sstevel@tonic-gateA C<PerlIOl *> is a pointer to the struct, and the I<application>
106*0Sstevel@tonic-gatelevel C<PerlIO *> is a pointer to a C<PerlIOl *> - i.e. a pointer
107*0Sstevel@tonic-gateto a pointer to the struct. This allows the application level C<PerlIO *>
108*0Sstevel@tonic-gateto remain constant while the actual C<PerlIOl *> underneath
109*0Sstevel@tonic-gatechanges. (Compare perl's C<SV *> which remains constant while its
110*0Sstevel@tonic-gateC<sv_any> field changes as the scalar's type changes.) An IO stream is
111*0Sstevel@tonic-gatethen in general represented as a pointer to this linked-list of
112*0Sstevel@tonic-gate"layers".
113*0Sstevel@tonic-gate
114*0Sstevel@tonic-gateIt should be noted that because of the double indirection in a C<PerlIO *>,
115*0Sstevel@tonic-gatea C<< &(perlio->next) >> "is" a C<PerlIO *>, and so to some degree
116*0Sstevel@tonic-gateat least one layer can use the "standard" API on the next layer down.
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gateA "layer" is composed of two parts:
119*0Sstevel@tonic-gate
120*0Sstevel@tonic-gate=over 4
121*0Sstevel@tonic-gate
122*0Sstevel@tonic-gate=item 1.
123*0Sstevel@tonic-gate
124*0Sstevel@tonic-gateThe functions and attributes of the "layer class".
125*0Sstevel@tonic-gate
126*0Sstevel@tonic-gate=item 2.
127*0Sstevel@tonic-gate
128*0Sstevel@tonic-gateThe per-instance data for a particular handle.
129*0Sstevel@tonic-gate
130*0Sstevel@tonic-gate=back
131*0Sstevel@tonic-gate
132*0Sstevel@tonic-gate=head2 Functions and Attributes
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gateThe functions and attributes are accessed via the "tab" (for table)
135*0Sstevel@tonic-gatemember of C<PerlIOl>. The functions (methods of the layer "class") are
136*0Sstevel@tonic-gatefixed, and are defined by the C<PerlIO_funcs> type. They are broadly the
137*0Sstevel@tonic-gatesame as the public C<PerlIO_xxxxx> functions:
138*0Sstevel@tonic-gate
139*0Sstevel@tonic-gate  struct _PerlIO_funcs
140*0Sstevel@tonic-gate  {
141*0Sstevel@tonic-gate   Size_t		fsize;
142*0Sstevel@tonic-gate   char *		name;
143*0Sstevel@tonic-gate   Size_t		size;
144*0Sstevel@tonic-gate   IV		kind;
145*0Sstevel@tonic-gate   IV		(*Pushed)(pTHX_ PerlIO *f,const char *mode,SV *arg, PerlIO_funcs *tab);
146*0Sstevel@tonic-gate   IV		(*Popped)(pTHX_ PerlIO *f);
147*0Sstevel@tonic-gate   PerlIO *	(*Open)(pTHX_ PerlIO_funcs *tab,
148*0Sstevel@tonic-gate  			AV *layers, IV n,
149*0Sstevel@tonic-gate  			const char *mode,
150*0Sstevel@tonic-gate  			int fd, int imode, int perm,
151*0Sstevel@tonic-gate  			PerlIO *old,
152*0Sstevel@tonic-gate  			int narg, SV **args);
153*0Sstevel@tonic-gate   IV		(*Binmode)(pTHX_ PerlIO *f);
154*0Sstevel@tonic-gate   SV *		(*Getarg)(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
155*0Sstevel@tonic-gate   IV		(*Fileno)(pTHX_ PerlIO *f);
156*0Sstevel@tonic-gate   PerlIO *     (*Dup)(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
157*0Sstevel@tonic-gate   /* Unix-like functions - cf sfio line disciplines */
158*0Sstevel@tonic-gate   SSize_t	(*Read)(pTHX_ PerlIO *f, void *vbuf, Size_t count);
159*0Sstevel@tonic-gate   SSize_t	(*Unread)(pTHX_ PerlIO *f, const void *vbuf, Size_t count);
160*0Sstevel@tonic-gate   SSize_t	(*Write)(pTHX_ PerlIO *f, const void *vbuf, Size_t count);
161*0Sstevel@tonic-gate   IV		(*Seek)(pTHX_ PerlIO *f, Off_t offset, int whence);
162*0Sstevel@tonic-gate   Off_t	(*Tell)(pTHX_ PerlIO *f);
163*0Sstevel@tonic-gate   IV		(*Close)(pTHX_ PerlIO *f);
164*0Sstevel@tonic-gate   /* Stdio-like buffered IO functions */
165*0Sstevel@tonic-gate   IV		(*Flush)(pTHX_ PerlIO *f);
166*0Sstevel@tonic-gate   IV		(*Fill)(pTHX_ PerlIO *f);
167*0Sstevel@tonic-gate   IV		(*Eof)(pTHX_ PerlIO *f);
168*0Sstevel@tonic-gate   IV		(*Error)(pTHX_ PerlIO *f);
169*0Sstevel@tonic-gate   void		(*Clearerr)(pTHX_ PerlIO *f);
170*0Sstevel@tonic-gate   void		(*Setlinebuf)(pTHX_ PerlIO *f);
171*0Sstevel@tonic-gate   /* Perl's snooping functions */
172*0Sstevel@tonic-gate   STDCHAR *	(*Get_base)(pTHX_ PerlIO *f);
173*0Sstevel@tonic-gate   Size_t	(*Get_bufsiz)(pTHX_ PerlIO *f);
174*0Sstevel@tonic-gate   STDCHAR *	(*Get_ptr)(pTHX_ PerlIO *f);
175*0Sstevel@tonic-gate   SSize_t	(*Get_cnt)(pTHX_ PerlIO *f);
176*0Sstevel@tonic-gate   void		(*Set_ptrcnt)(pTHX_ PerlIO *f,STDCHAR *ptr,SSize_t cnt);
177*0Sstevel@tonic-gate  };
178*0Sstevel@tonic-gate
179*0Sstevel@tonic-gateThe first few members of the struct give a function table size for
180*0Sstevel@tonic-gatecompatibility check "name" for the layer, the  size to C<malloc> for the per-instance data,
181*0Sstevel@tonic-gateand some flags which are attributes of the class as whole (such as whether it is a buffering
182*0Sstevel@tonic-gatelayer), then follow the functions which fall into four basic groups:
183*0Sstevel@tonic-gate
184*0Sstevel@tonic-gate=over 4
185*0Sstevel@tonic-gate
186*0Sstevel@tonic-gate=item 1.
187*0Sstevel@tonic-gate
188*0Sstevel@tonic-gateOpening and setup functions
189*0Sstevel@tonic-gate
190*0Sstevel@tonic-gate=item 2.
191*0Sstevel@tonic-gate
192*0Sstevel@tonic-gateBasic IO operations
193*0Sstevel@tonic-gate
194*0Sstevel@tonic-gate=item 3.
195*0Sstevel@tonic-gate
196*0Sstevel@tonic-gateStdio class buffering options.
197*0Sstevel@tonic-gate
198*0Sstevel@tonic-gate=item 4.
199*0Sstevel@tonic-gate
200*0Sstevel@tonic-gateFunctions to support Perl's traditional "fast" access to the buffer.
201*0Sstevel@tonic-gate
202*0Sstevel@tonic-gate=back
203*0Sstevel@tonic-gate
204*0Sstevel@tonic-gateA layer does not have to implement all the functions, but the whole
205*0Sstevel@tonic-gatetable has to be present. Unimplemented slots can be NULL (which will
206*0Sstevel@tonic-gateresult in an error when called) or can be filled in with stubs to
207*0Sstevel@tonic-gate"inherit" behaviour from a "base class". This "inheritance" is fixed
208*0Sstevel@tonic-gatefor all instances of the layer, but as the layer chooses which stubs
209*0Sstevel@tonic-gateto populate the table, limited "multiple inheritance" is possible.
210*0Sstevel@tonic-gate
211*0Sstevel@tonic-gate=head2 Per-instance Data
212*0Sstevel@tonic-gate
213*0Sstevel@tonic-gateThe per-instance data are held in memory beyond the basic PerlIOl
214*0Sstevel@tonic-gatestruct, by making a PerlIOl the first member of the layer's struct
215*0Sstevel@tonic-gatethus:
216*0Sstevel@tonic-gate
217*0Sstevel@tonic-gate	typedef struct
218*0Sstevel@tonic-gate	{
219*0Sstevel@tonic-gate	 struct _PerlIO base;       /* Base "class" info */
220*0Sstevel@tonic-gate	 STDCHAR *	buf;        /* Start of buffer */
221*0Sstevel@tonic-gate	 STDCHAR *	end;        /* End of valid part of buffer */
222*0Sstevel@tonic-gate	 STDCHAR *	ptr;        /* Current position in buffer */
223*0Sstevel@tonic-gate	 Off_t		posn;       /* Offset of buf into the file */
224*0Sstevel@tonic-gate	 Size_t		bufsiz;     /* Real size of buffer */
225*0Sstevel@tonic-gate	 IV		oneword;    /* Emergency buffer */
226*0Sstevel@tonic-gate	} PerlIOBuf;
227*0Sstevel@tonic-gate
228*0Sstevel@tonic-gateIn this way (as for perl's scalars) a pointer to a PerlIOBuf can be
229*0Sstevel@tonic-gatetreated as a pointer to a PerlIOl.
230*0Sstevel@tonic-gate
231*0Sstevel@tonic-gate=head2 Layers in action.
232*0Sstevel@tonic-gate
233*0Sstevel@tonic-gate                table           perlio          unix
234*0Sstevel@tonic-gate            |           |
235*0Sstevel@tonic-gate            +-----------+    +----------+    +--------+
236*0Sstevel@tonic-gate   PerlIO ->|           |--->|  next    |--->|  NULL  |
237*0Sstevel@tonic-gate            +-----------+    +----------+    +--------+
238*0Sstevel@tonic-gate            |           |    |  buffer  |    |   fd   |
239*0Sstevel@tonic-gate            +-----------+    |          |    +--------+
240*0Sstevel@tonic-gate            |           |    +----------+
241*0Sstevel@tonic-gate
242*0Sstevel@tonic-gate
243*0Sstevel@tonic-gateThe above attempts to show how the layer scheme works in a simple case.
244*0Sstevel@tonic-gateThe application's C<PerlIO *> points to an entry in the table(s)
245*0Sstevel@tonic-gaterepresenting open (allocated) handles. For example the first three slots
246*0Sstevel@tonic-gatein the table correspond to C<stdin>,C<stdout> and C<stderr>. The table
247*0Sstevel@tonic-gatein turn points to the current "top" layer for the handle - in this case
248*0Sstevel@tonic-gatean instance of the generic buffering layer "perlio". That layer in turn
249*0Sstevel@tonic-gatepoints to the next layer down - in this case the lowlevel "unix" layer.
250*0Sstevel@tonic-gate
251*0Sstevel@tonic-gateThe above is roughly equivalent to a "stdio" buffered stream, but with
252*0Sstevel@tonic-gatemuch more flexibility:
253*0Sstevel@tonic-gate
254*0Sstevel@tonic-gate=over 4
255*0Sstevel@tonic-gate
256*0Sstevel@tonic-gate=item *
257*0Sstevel@tonic-gate
258*0Sstevel@tonic-gateIf Unix level C<read>/C<write>/C<lseek> is not appropriate for (say)
259*0Sstevel@tonic-gatesockets then the "unix" layer can be replaced (at open time or even
260*0Sstevel@tonic-gatedynamically) with a "socket" layer.
261*0Sstevel@tonic-gate
262*0Sstevel@tonic-gate=item *
263*0Sstevel@tonic-gate
264*0Sstevel@tonic-gateDifferent handles can have different buffering schemes. The "top"
265*0Sstevel@tonic-gatelayer could be the "mmap" layer if reading disk files was quicker
266*0Sstevel@tonic-gateusing C<mmap> than C<read>. An "unbuffered" stream can be implemented
267*0Sstevel@tonic-gatesimply by not having a buffer layer.
268*0Sstevel@tonic-gate
269*0Sstevel@tonic-gate=item *
270*0Sstevel@tonic-gate
271*0Sstevel@tonic-gateExtra layers can be inserted to process the data as it flows through.
272*0Sstevel@tonic-gateThis was the driving need for including the scheme in perl 5.7.0+ - we
273*0Sstevel@tonic-gateneeded a mechanism to allow data to be translated between perl's
274*0Sstevel@tonic-gateinternal encoding (conceptually at least Unicode as UTF-8), and the
275*0Sstevel@tonic-gate"native" format used by the system. This is provided by the
276*0Sstevel@tonic-gate":encoding(xxxx)" layer which typically sits above the buffering layer.
277*0Sstevel@tonic-gate
278*0Sstevel@tonic-gate=item *
279*0Sstevel@tonic-gate
280*0Sstevel@tonic-gateA layer can be added that does "\n" to CRLF translation. This layer
281*0Sstevel@tonic-gatecan be used on any platform, not just those that normally do such
282*0Sstevel@tonic-gatethings.
283*0Sstevel@tonic-gate
284*0Sstevel@tonic-gate=back
285*0Sstevel@tonic-gate
286*0Sstevel@tonic-gate=head2 Per-instance flag bits
287*0Sstevel@tonic-gate
288*0Sstevel@tonic-gateThe generic flag bits are a hybrid of C<O_XXXXX> style flags deduced
289*0Sstevel@tonic-gatefrom the mode string passed to C<PerlIO_open()>, and state bits for
290*0Sstevel@tonic-gatetypical buffer layers.
291*0Sstevel@tonic-gate
292*0Sstevel@tonic-gate=over 4
293*0Sstevel@tonic-gate
294*0Sstevel@tonic-gate=item PERLIO_F_EOF
295*0Sstevel@tonic-gate
296*0Sstevel@tonic-gateEnd of file.
297*0Sstevel@tonic-gate
298*0Sstevel@tonic-gate=item PERLIO_F_CANWRITE
299*0Sstevel@tonic-gate
300*0Sstevel@tonic-gateWrites are permitted, i.e. opened as "w" or "r+" or "a", etc.
301*0Sstevel@tonic-gate
302*0Sstevel@tonic-gate=item  PERLIO_F_CANREAD
303*0Sstevel@tonic-gate
304*0Sstevel@tonic-gateReads are permitted i.e. opened "r" or "w+" (or even "a+" - ick).
305*0Sstevel@tonic-gate
306*0Sstevel@tonic-gate=item PERLIO_F_ERROR
307*0Sstevel@tonic-gate
308*0Sstevel@tonic-gateAn error has occurred (for C<PerlIO_error()>).
309*0Sstevel@tonic-gate
310*0Sstevel@tonic-gate=item PERLIO_F_TRUNCATE
311*0Sstevel@tonic-gate
312*0Sstevel@tonic-gateTruncate file suggested by open mode.
313*0Sstevel@tonic-gate
314*0Sstevel@tonic-gate=item PERLIO_F_APPEND
315*0Sstevel@tonic-gate
316*0Sstevel@tonic-gateAll writes should be appends.
317*0Sstevel@tonic-gate
318*0Sstevel@tonic-gate=item PERLIO_F_CRLF
319*0Sstevel@tonic-gate
320*0Sstevel@tonic-gateLayer is performing Win32-like "\n" mapped to CR,LF for output and CR,LF
321*0Sstevel@tonic-gatemapped to "\n" for input. Normally the provided "crlf" layer is the only
322*0Sstevel@tonic-gatelayer that need bother about this. C<PerlIO_binmode()> will mess with this
323*0Sstevel@tonic-gateflag rather than add/remove layers if the C<PERLIO_K_CANCRLF> bit is set
324*0Sstevel@tonic-gatefor the layers class.
325*0Sstevel@tonic-gate
326*0Sstevel@tonic-gate=item PERLIO_F_UTF8
327*0Sstevel@tonic-gate
328*0Sstevel@tonic-gateData written to this layer should be UTF-8 encoded; data provided
329*0Sstevel@tonic-gateby this layer should be considered UTF-8 encoded. Can be set on any layer
330*0Sstevel@tonic-gateby ":utf8" dummy layer. Also set on ":encoding" layer.
331*0Sstevel@tonic-gate
332*0Sstevel@tonic-gate=item PERLIO_F_UNBUF
333*0Sstevel@tonic-gate
334*0Sstevel@tonic-gateLayer is unbuffered - i.e. write to next layer down should occur for
335*0Sstevel@tonic-gateeach write to this layer.
336*0Sstevel@tonic-gate
337*0Sstevel@tonic-gate=item PERLIO_F_WRBUF
338*0Sstevel@tonic-gate
339*0Sstevel@tonic-gateThe buffer for this layer currently holds data written to it but not sent
340*0Sstevel@tonic-gateto next layer.
341*0Sstevel@tonic-gate
342*0Sstevel@tonic-gate=item PERLIO_F_RDBUF
343*0Sstevel@tonic-gate
344*0Sstevel@tonic-gateThe buffer for this layer currently holds unconsumed data read from
345*0Sstevel@tonic-gatelayer below.
346*0Sstevel@tonic-gate
347*0Sstevel@tonic-gate=item PERLIO_F_LINEBUF
348*0Sstevel@tonic-gate
349*0Sstevel@tonic-gateLayer is line buffered. Write data should be passed to next layer down
350*0Sstevel@tonic-gatewhenever a "\n" is seen. Any data beyond the "\n" should then be
351*0Sstevel@tonic-gateprocessed.
352*0Sstevel@tonic-gate
353*0Sstevel@tonic-gate=item PERLIO_F_TEMP
354*0Sstevel@tonic-gate
355*0Sstevel@tonic-gateFile has been C<unlink()>ed, or should be deleted on C<close()>.
356*0Sstevel@tonic-gate
357*0Sstevel@tonic-gate=item PERLIO_F_OPEN
358*0Sstevel@tonic-gate
359*0Sstevel@tonic-gateHandle is open.
360*0Sstevel@tonic-gate
361*0Sstevel@tonic-gate=item PERLIO_F_FASTGETS
362*0Sstevel@tonic-gate
363*0Sstevel@tonic-gateThis instance of this layer supports the "fast C<gets>" interface.
364*0Sstevel@tonic-gateNormally set based on C<PERLIO_K_FASTGETS> for the class and by the
365*0Sstevel@tonic-gateexistence of the function(s) in the table. However a class that
366*0Sstevel@tonic-gatenormally provides that interface may need to avoid it on a
367*0Sstevel@tonic-gateparticular instance. The "pending" layer needs to do this when
368*0Sstevel@tonic-gateit is pushed above a layer which does not support the interface.
369*0Sstevel@tonic-gate(Perl's C<sv_gets()> does not expect the streams fast C<gets> behaviour
370*0Sstevel@tonic-gateto change during one "get".)
371*0Sstevel@tonic-gate
372*0Sstevel@tonic-gate=back
373*0Sstevel@tonic-gate
374*0Sstevel@tonic-gate=head2 Methods in Detail
375*0Sstevel@tonic-gate
376*0Sstevel@tonic-gate=over 4
377*0Sstevel@tonic-gate
378*0Sstevel@tonic-gate=item fsize
379*0Sstevel@tonic-gate
380*0Sstevel@tonic-gate	Size_t fsize;
381*0Sstevel@tonic-gate
382*0Sstevel@tonic-gateSize of the function table. This is compared against the value PerlIO
383*0Sstevel@tonic-gatecode "knows" as a compatibility check. Future versions I<may> be able
384*0Sstevel@tonic-gateto tolerate layers compiled against an old version of the headers.
385*0Sstevel@tonic-gate
386*0Sstevel@tonic-gate=item name
387*0Sstevel@tonic-gate
388*0Sstevel@tonic-gate	char * name;
389*0Sstevel@tonic-gate
390*0Sstevel@tonic-gateThe name of the layer whose open() method Perl should invoke on
391*0Sstevel@tonic-gateopen().  For example if the layer is called APR, you will call:
392*0Sstevel@tonic-gate
393*0Sstevel@tonic-gate  open $fh, ">:APR", ...
394*0Sstevel@tonic-gate
395*0Sstevel@tonic-gateand Perl knows that it has to invoke the PerlIOAPR_open() method
396*0Sstevel@tonic-gateimplemented by the APR layer.
397*0Sstevel@tonic-gate
398*0Sstevel@tonic-gate=item size
399*0Sstevel@tonic-gate
400*0Sstevel@tonic-gate	Size_t size;
401*0Sstevel@tonic-gate
402*0Sstevel@tonic-gateThe size of the per-instance data structure, e.g.:
403*0Sstevel@tonic-gate
404*0Sstevel@tonic-gate  sizeof(PerlIOAPR)
405*0Sstevel@tonic-gate
406*0Sstevel@tonic-gateIf this field is zero then C<PerlIO_pushed> does not malloc anything
407*0Sstevel@tonic-gateand assumes layer's Pushed function will do any required layer stack
408*0Sstevel@tonic-gatemanipulation - used to avoid malloc/free overhead for dummy layers.
409*0Sstevel@tonic-gateIf the field is non-zero it must be at least the size of C<PerlIOl>,
410*0Sstevel@tonic-gateC<PerlIO_pushed> will allocate memory for the layer's data structures
411*0Sstevel@tonic-gateand link new layer onto the stream's stack. (If the layer's Pushed
412*0Sstevel@tonic-gatemethod returns an error indication the layer is popped again.)
413*0Sstevel@tonic-gate
414*0Sstevel@tonic-gate=item kind
415*0Sstevel@tonic-gate
416*0Sstevel@tonic-gate	IV kind;
417*0Sstevel@tonic-gate
418*0Sstevel@tonic-gate=over 4
419*0Sstevel@tonic-gate
420*0Sstevel@tonic-gate=item * PERLIO_K_BUFFERED
421*0Sstevel@tonic-gate
422*0Sstevel@tonic-gateThe layer is buffered.
423*0Sstevel@tonic-gate
424*0Sstevel@tonic-gate=item * PERLIO_K_RAW
425*0Sstevel@tonic-gate
426*0Sstevel@tonic-gateThe layer is acceptable to have in a binmode(FH) stack - i.e. it does not
427*0Sstevel@tonic-gate(or will configure itself not to) transform bytes passing through it.
428*0Sstevel@tonic-gate
429*0Sstevel@tonic-gate=item * PERLIO_K_CANCRLF
430*0Sstevel@tonic-gate
431*0Sstevel@tonic-gateLayer can translate between "\n" and CRLF line ends.
432*0Sstevel@tonic-gate
433*0Sstevel@tonic-gate=item * PERLIO_K_FASTGETS
434*0Sstevel@tonic-gate
435*0Sstevel@tonic-gateLayer allows buffer snooping.
436*0Sstevel@tonic-gate
437*0Sstevel@tonic-gate=item * PERLIO_K_MULTIARG
438*0Sstevel@tonic-gate
439*0Sstevel@tonic-gateUsed when the layer's open() accepts more arguments than usual. The
440*0Sstevel@tonic-gateextra arguments should come not before the C<MODE> argument. When this
441*0Sstevel@tonic-gateflag is used it's up to the layer to validate the args.
442*0Sstevel@tonic-gate
443*0Sstevel@tonic-gate=back
444*0Sstevel@tonic-gate
445*0Sstevel@tonic-gate=item Pushed
446*0Sstevel@tonic-gate
447*0Sstevel@tonic-gate	IV	(*Pushed)(pTHX_ PerlIO *f,const char *mode, SV *arg);
448*0Sstevel@tonic-gate
449*0Sstevel@tonic-gateThe only absolutely mandatory method. Called when the layer is pushed
450*0Sstevel@tonic-gateonto the stack.  The C<mode> argument may be NULL if this occurs
451*0Sstevel@tonic-gatepost-open. The C<arg> will be non-C<NULL> if an argument string was
452*0Sstevel@tonic-gatepassed. In most cases this should call C<PerlIOBase_pushed()> to
453*0Sstevel@tonic-gateconvert C<mode> into the appropriate C<PERLIO_F_XXXXX> flags in
454*0Sstevel@tonic-gateaddition to any actions the layer itself takes.  If a layer is not
455*0Sstevel@tonic-gateexpecting an argument it need neither save the one passed to it, nor
456*0Sstevel@tonic-gateprovide C<Getarg()> (it could perhaps C<Perl_warn> that the argument
457*0Sstevel@tonic-gatewas un-expected).
458*0Sstevel@tonic-gate
459*0Sstevel@tonic-gateReturns 0 on success. On failure returns -1 and should set errno.
460*0Sstevel@tonic-gate
461*0Sstevel@tonic-gate=item Popped
462*0Sstevel@tonic-gate
463*0Sstevel@tonic-gate	IV	(*Popped)(pTHX_ PerlIO *f);
464*0Sstevel@tonic-gate
465*0Sstevel@tonic-gateCalled when the layer is popped from the stack. A layer will normally
466*0Sstevel@tonic-gatebe popped after C<Close()> is called. But a layer can be popped
467*0Sstevel@tonic-gatewithout being closed if the program is dynamically managing layers on
468*0Sstevel@tonic-gatethe stream. In such cases C<Popped()> should free any resources
469*0Sstevel@tonic-gate(buffers, translation tables, ...) not held directly in the layer's
470*0Sstevel@tonic-gatestruct.  It should also C<Unread()> any unconsumed data that has been
471*0Sstevel@tonic-gateread and buffered from the layer below back to that layer, so that it
472*0Sstevel@tonic-gatecan be re-provided to what ever is now above.
473*0Sstevel@tonic-gate
474*0Sstevel@tonic-gateReturns 0 on success and failure.  If C<Popped()> returns I<true> then
475*0Sstevel@tonic-gateI<perlio.c> assumes that either the layer has popped itself, or the
476*0Sstevel@tonic-gatelayer is super special and needs to be retained for other reasons.
477*0Sstevel@tonic-gateIn most cases it should return I<false>.
478*0Sstevel@tonic-gate
479*0Sstevel@tonic-gate=item Open
480*0Sstevel@tonic-gate
481*0Sstevel@tonic-gate	PerlIO *	(*Open)(...);
482*0Sstevel@tonic-gate
483*0Sstevel@tonic-gateThe C<Open()> method has lots of arguments because it combines the
484*0Sstevel@tonic-gatefunctions of perl's C<open>, C<PerlIO_open>, perl's C<sysopen>,
485*0Sstevel@tonic-gateC<PerlIO_fdopen> and C<PerlIO_reopen>.  The full prototype is as
486*0Sstevel@tonic-gatefollows:
487*0Sstevel@tonic-gate
488*0Sstevel@tonic-gate PerlIO *	(*Open)(pTHX_ PerlIO_funcs *tab,
489*0Sstevel@tonic-gate			AV *layers, IV n,
490*0Sstevel@tonic-gate			const char *mode,
491*0Sstevel@tonic-gate			int fd, int imode, int perm,
492*0Sstevel@tonic-gate			PerlIO *old,
493*0Sstevel@tonic-gate			int narg, SV **args);
494*0Sstevel@tonic-gate
495*0Sstevel@tonic-gateOpen should (perhaps indirectly) call C<PerlIO_allocate()> to allocate
496*0Sstevel@tonic-gatea slot in the table and associate it with the layers information for
497*0Sstevel@tonic-gatethe opened file, by calling C<PerlIO_push>.  The I<layers> AV is an
498*0Sstevel@tonic-gatearray of all the layers destined for the C<PerlIO *>, and any
499*0Sstevel@tonic-gatearguments passed to them, I<n> is the index into that array of the
500*0Sstevel@tonic-gatelayer being called. The macro C<PerlIOArg> will return a (possibly
501*0Sstevel@tonic-gateC<NULL>) SV * for the argument passed to the layer.
502*0Sstevel@tonic-gate
503*0Sstevel@tonic-gateThe I<mode> string is an "C<fopen()>-like" string which would match
504*0Sstevel@tonic-gatethe regular expression C</^[I#]?[rwa]\+?[bt]?$/>.
505*0Sstevel@tonic-gate
506*0Sstevel@tonic-gateThe C<'I'> prefix is used during creation of C<stdin>..C<stderr> via
507*0Sstevel@tonic-gatespecial C<PerlIO_fdopen> calls; the C<'#'> prefix means that this is
508*0Sstevel@tonic-gateC<sysopen> and that I<imode> and I<perm> should be passed to
509*0Sstevel@tonic-gateC<PerlLIO_open3>; C<'r'> means B<r>ead, C<'w'> means B<w>rite and
510*0Sstevel@tonic-gateC<'a'> means B<a>ppend. The C<'+'> suffix means that both reading and
511*0Sstevel@tonic-gatewriting/appending are permitted.  The C<'b'> suffix means file should
512*0Sstevel@tonic-gatebe binary, and C<'t'> means it is text. (Almost all layers should do
513*0Sstevel@tonic-gatethe IO in binary mode, and ignore the b/t bits. The C<:crlf> layer
514*0Sstevel@tonic-gateshould be pushed to handle the distinction.)
515*0Sstevel@tonic-gate
516*0Sstevel@tonic-gateIf I<old> is not C<NULL> then this is a C<PerlIO_reopen>. Perl itself
517*0Sstevel@tonic-gatedoes not use this (yet?) and semantics are a little vague.
518*0Sstevel@tonic-gate
519*0Sstevel@tonic-gateIf I<fd> not negative then it is the numeric file descriptor I<fd>,
520*0Sstevel@tonic-gatewhich will be open in a manner compatible with the supplied mode
521*0Sstevel@tonic-gatestring, the call is thus equivalent to C<PerlIO_fdopen>. In this case
522*0Sstevel@tonic-gateI<nargs> will be zero.
523*0Sstevel@tonic-gate
524*0Sstevel@tonic-gateIf I<nargs> is greater than zero then it gives the number of arguments
525*0Sstevel@tonic-gatepassed to C<open>, otherwise it will be 1 if for example
526*0Sstevel@tonic-gateC<PerlIO_open> was called.  In simple cases SvPV_nolen(*args) is the
527*0Sstevel@tonic-gatepathname to open.
528*0Sstevel@tonic-gate
529*0Sstevel@tonic-gateHaving said all that translation-only layers do not need to provide
530*0Sstevel@tonic-gateC<Open()> at all, but rather leave the opening to a lower level layer
531*0Sstevel@tonic-gateand wait to be "pushed".  If a layer does provide C<Open()> it should
532*0Sstevel@tonic-gatenormally call the C<Open()> method of next layer down (if any) and
533*0Sstevel@tonic-gatethen push itself on top if that succeeds.
534*0Sstevel@tonic-gate
535*0Sstevel@tonic-gateIf C<PerlIO_push> was performed and open has failed, it must
536*0Sstevel@tonic-gateC<PerlIO_pop> itself, since if it's not, the layer won't be removed
537*0Sstevel@tonic-gateand may cause bad problems.
538*0Sstevel@tonic-gate
539*0Sstevel@tonic-gateReturns C<NULL> on failure.
540*0Sstevel@tonic-gate
541*0Sstevel@tonic-gate=item Binmode
542*0Sstevel@tonic-gate
543*0Sstevel@tonic-gate	IV        (*Binmode)(pTHX_ PerlIO *f);
544*0Sstevel@tonic-gate
545*0Sstevel@tonic-gateOptional. Used when C<:raw> layer is pushed (explicitly or as a result
546*0Sstevel@tonic-gateof binmode(FH)). If not present layer will be popped. If present
547*0Sstevel@tonic-gateshould configure layer as binary (or pop itself) and return 0.
548*0Sstevel@tonic-gateIf it returns -1 for error C<binmode> will fail with layer
549*0Sstevel@tonic-gatestill on the stack.
550*0Sstevel@tonic-gate
551*0Sstevel@tonic-gate=item Getarg
552*0Sstevel@tonic-gate
553*0Sstevel@tonic-gate	SV *      (*Getarg)(pTHX_ PerlIO *f,
554*0Sstevel@tonic-gate			    CLONE_PARAMS *param, int flags);
555*0Sstevel@tonic-gate
556*0Sstevel@tonic-gateOptional. If present should return an SV * representing the string
557*0Sstevel@tonic-gateargument passed to the layer when it was
558*0Sstevel@tonic-gatepushed. e.g. ":encoding(ascii)" would return an SvPV with value
559*0Sstevel@tonic-gate"ascii". (I<param> and I<flags> arguments can be ignored in most
560*0Sstevel@tonic-gatecases)
561*0Sstevel@tonic-gate
562*0Sstevel@tonic-gateC<Dup> uses C<Getarg> to retrieve the argument originally passed to
563*0Sstevel@tonic-gateC<Pushed>, so you must implement this function if your layer has an
564*0Sstevel@tonic-gateextra argument to C<Pushed> and will ever be C<Dup>ed.
565*0Sstevel@tonic-gate
566*0Sstevel@tonic-gate=item Fileno
567*0Sstevel@tonic-gate
568*0Sstevel@tonic-gate	IV        (*Fileno)(pTHX_ PerlIO *f);
569*0Sstevel@tonic-gate
570*0Sstevel@tonic-gateReturns the Unix/Posix numeric file descriptor for the handle. Normally
571*0Sstevel@tonic-gateC<PerlIOBase_fileno()> (which just asks next layer down) will suffice
572*0Sstevel@tonic-gatefor this.
573*0Sstevel@tonic-gate
574*0Sstevel@tonic-gateReturns -1 on error, which is considered to include the case where the
575*0Sstevel@tonic-gatelayer cannot provide such a file descriptor.
576*0Sstevel@tonic-gate
577*0Sstevel@tonic-gate=item Dup
578*0Sstevel@tonic-gate
579*0Sstevel@tonic-gate	PerlIO * (*Dup)(pTHX_ PerlIO *f, PerlIO *o,
580*0Sstevel@tonic-gate			CLONE_PARAMS *param, int flags);
581*0Sstevel@tonic-gate
582*0Sstevel@tonic-gateXXX: Needs more docs.
583*0Sstevel@tonic-gate
584*0Sstevel@tonic-gateUsed as part of the "clone" process when a thread is spawned (in which
585*0Sstevel@tonic-gatecase param will be non-NULL) and when a stream is being duplicated via
586*0Sstevel@tonic-gate'&' in the C<open>.
587*0Sstevel@tonic-gate
588*0Sstevel@tonic-gateSimilar to C<Open>, returns PerlIO* on success, C<NULL> on failure.
589*0Sstevel@tonic-gate
590*0Sstevel@tonic-gate=item Read
591*0Sstevel@tonic-gate
592*0Sstevel@tonic-gate	SSize_t	(*Read)(pTHX_ PerlIO *f, void *vbuf, Size_t count);
593*0Sstevel@tonic-gate
594*0Sstevel@tonic-gateBasic read operation.
595*0Sstevel@tonic-gate
596*0Sstevel@tonic-gateTypically will call C<Fill> and manipulate pointers (possibly via the
597*0Sstevel@tonic-gateAPI).  C<PerlIOBuf_read()> may be suitable for derived classes which
598*0Sstevel@tonic-gateprovide "fast gets" methods.
599*0Sstevel@tonic-gate
600*0Sstevel@tonic-gateReturns actual bytes read, or -1 on an error.
601*0Sstevel@tonic-gate
602*0Sstevel@tonic-gate=item	Unread
603*0Sstevel@tonic-gate
604*0Sstevel@tonic-gate	SSize_t	(*Unread)(pTHX_ PerlIO *f,
605*0Sstevel@tonic-gate			  const void *vbuf, Size_t count);
606*0Sstevel@tonic-gate
607*0Sstevel@tonic-gateA superset of stdio's C<ungetc()>. Should arrange for future reads to
608*0Sstevel@tonic-gatesee the bytes in C<vbuf>. If there is no obviously better implementation
609*0Sstevel@tonic-gatethen C<PerlIOBase_unread()> provides the function by pushing a "fake"
610*0Sstevel@tonic-gate"pending" layer above the calling layer.
611*0Sstevel@tonic-gate
612*0Sstevel@tonic-gateReturns the number of unread chars.
613*0Sstevel@tonic-gate
614*0Sstevel@tonic-gate=item Write
615*0Sstevel@tonic-gate
616*0Sstevel@tonic-gate	SSize_t	(*Write)(PerlIO *f, const void *vbuf, Size_t count);
617*0Sstevel@tonic-gate
618*0Sstevel@tonic-gateBasic write operation.
619*0Sstevel@tonic-gate
620*0Sstevel@tonic-gateReturns bytes written or -1 on an error.
621*0Sstevel@tonic-gate
622*0Sstevel@tonic-gate=item Seek
623*0Sstevel@tonic-gate
624*0Sstevel@tonic-gate	IV	(*Seek)(pTHX_ PerlIO *f, Off_t offset, int whence);
625*0Sstevel@tonic-gate
626*0Sstevel@tonic-gatePosition the file pointer. Should normally call its own C<Flush>
627*0Sstevel@tonic-gatemethod and then the C<Seek> method of next layer down.
628*0Sstevel@tonic-gate
629*0Sstevel@tonic-gateReturns 0 on success, -1 on failure.
630*0Sstevel@tonic-gate
631*0Sstevel@tonic-gate=item Tell
632*0Sstevel@tonic-gate
633*0Sstevel@tonic-gate	Off_t	(*Tell)(pTHX_ PerlIO *f);
634*0Sstevel@tonic-gate
635*0Sstevel@tonic-gateReturn the file pointer. May be based on layers cached concept of
636*0Sstevel@tonic-gateposition to avoid overhead.
637*0Sstevel@tonic-gate
638*0Sstevel@tonic-gateReturns -1 on failure to get the file pointer.
639*0Sstevel@tonic-gate
640*0Sstevel@tonic-gate=item Close
641*0Sstevel@tonic-gate
642*0Sstevel@tonic-gate	IV	(*Close)(pTHX_ PerlIO *f);
643*0Sstevel@tonic-gate
644*0Sstevel@tonic-gateClose the stream. Should normally call C<PerlIOBase_close()> to flush
645*0Sstevel@tonic-gateitself and close layers below, and then deallocate any data structures
646*0Sstevel@tonic-gate(buffers, translation tables, ...) not  held directly in the data
647*0Sstevel@tonic-gatestructure.
648*0Sstevel@tonic-gate
649*0Sstevel@tonic-gateReturns 0 on success, -1 on failure.
650*0Sstevel@tonic-gate
651*0Sstevel@tonic-gate=item Flush
652*0Sstevel@tonic-gate
653*0Sstevel@tonic-gate	IV	(*Flush)(pTHX_ PerlIO *f);
654*0Sstevel@tonic-gate
655*0Sstevel@tonic-gateShould make stream's state consistent with layers below. That is, any
656*0Sstevel@tonic-gatebuffered write data should be written, and file position of lower layers
657*0Sstevel@tonic-gateadjusted for data read from below but not actually consumed.
658*0Sstevel@tonic-gate(Should perhaps C<Unread()> such data to the lower layer.)
659*0Sstevel@tonic-gate
660*0Sstevel@tonic-gateReturns 0 on success, -1 on failure.
661*0Sstevel@tonic-gate
662*0Sstevel@tonic-gate=item Fill
663*0Sstevel@tonic-gate
664*0Sstevel@tonic-gate	IV	(*Fill)(pTHX_ PerlIO *f);
665*0Sstevel@tonic-gate
666*0Sstevel@tonic-gateThe buffer for this layer should be filled (for read) from layer
667*0Sstevel@tonic-gatebelow.  When you "subclass" PerlIOBuf layer, you want to use its
668*0Sstevel@tonic-gateI<_read> method and to supply your own fill method, which fills the
669*0Sstevel@tonic-gatePerlIOBuf's buffer.
670*0Sstevel@tonic-gate
671*0Sstevel@tonic-gateReturns 0 on success, -1 on failure.
672*0Sstevel@tonic-gate
673*0Sstevel@tonic-gate=item Eof
674*0Sstevel@tonic-gate
675*0Sstevel@tonic-gate	IV	(*Eof)(pTHX_ PerlIO *f);
676*0Sstevel@tonic-gate
677*0Sstevel@tonic-gateReturn end-of-file indicator. C<PerlIOBase_eof()> is normally sufficient.
678*0Sstevel@tonic-gate
679*0Sstevel@tonic-gateReturns 0 on end-of-file, 1 if not end-of-file, -1 on error.
680*0Sstevel@tonic-gate
681*0Sstevel@tonic-gate=item Error
682*0Sstevel@tonic-gate
683*0Sstevel@tonic-gate	IV	(*Error)(pTHX_ PerlIO *f);
684*0Sstevel@tonic-gate
685*0Sstevel@tonic-gateReturn error indicator. C<PerlIOBase_error()> is normally sufficient.
686*0Sstevel@tonic-gate
687*0Sstevel@tonic-gateReturns 1 if there is an error (usually when C<PERLIO_F_ERROR> is set,
688*0Sstevel@tonic-gate0 otherwise.
689*0Sstevel@tonic-gate
690*0Sstevel@tonic-gate=item  Clearerr
691*0Sstevel@tonic-gate
692*0Sstevel@tonic-gate	void	(*Clearerr)(pTHX_ PerlIO *f);
693*0Sstevel@tonic-gate
694*0Sstevel@tonic-gateClear end-of-file and error indicators. Should call C<PerlIOBase_clearerr()>
695*0Sstevel@tonic-gateto set the C<PERLIO_F_XXXXX> flags, which may suffice.
696*0Sstevel@tonic-gate
697*0Sstevel@tonic-gate=item Setlinebuf
698*0Sstevel@tonic-gate
699*0Sstevel@tonic-gate	void	(*Setlinebuf)(pTHX_ PerlIO *f);
700*0Sstevel@tonic-gate
701*0Sstevel@tonic-gateMark the stream as line buffered. C<PerlIOBase_setlinebuf()> sets the
702*0Sstevel@tonic-gatePERLIO_F_LINEBUF flag and is normally sufficient.
703*0Sstevel@tonic-gate
704*0Sstevel@tonic-gate=item Get_base
705*0Sstevel@tonic-gate
706*0Sstevel@tonic-gate	STDCHAR *	(*Get_base)(pTHX_ PerlIO *f);
707*0Sstevel@tonic-gate
708*0Sstevel@tonic-gateAllocate (if not already done so) the read buffer for this layer and
709*0Sstevel@tonic-gatereturn pointer to it. Return NULL on failure.
710*0Sstevel@tonic-gate
711*0Sstevel@tonic-gate=item Get_bufsiz
712*0Sstevel@tonic-gate
713*0Sstevel@tonic-gate	Size_t	(*Get_bufsiz)(pTHX_ PerlIO *f);
714*0Sstevel@tonic-gate
715*0Sstevel@tonic-gateReturn the number of bytes that last C<Fill()> put in the buffer.
716*0Sstevel@tonic-gate
717*0Sstevel@tonic-gate=item Get_ptr
718*0Sstevel@tonic-gate
719*0Sstevel@tonic-gate	STDCHAR *	(*Get_ptr)(pTHX_ PerlIO *f);
720*0Sstevel@tonic-gate
721*0Sstevel@tonic-gateReturn the current read pointer relative to this layer's buffer.
722*0Sstevel@tonic-gate
723*0Sstevel@tonic-gate=item Get_cnt
724*0Sstevel@tonic-gate
725*0Sstevel@tonic-gate	SSize_t	(*Get_cnt)(pTHX_ PerlIO *f);
726*0Sstevel@tonic-gate
727*0Sstevel@tonic-gateReturn the number of bytes left to be read in the current buffer.
728*0Sstevel@tonic-gate
729*0Sstevel@tonic-gate=item Set_ptrcnt
730*0Sstevel@tonic-gate
731*0Sstevel@tonic-gate	void	(*Set_ptrcnt)(pTHX_ PerlIO *f,
732*0Sstevel@tonic-gate			      STDCHAR *ptr, SSize_t cnt);
733*0Sstevel@tonic-gate
734*0Sstevel@tonic-gateAdjust the read pointer and count of bytes to match C<ptr> and/or C<cnt>.
735*0Sstevel@tonic-gateThe application (or layer above) must ensure they are consistent.
736*0Sstevel@tonic-gate(Checking is allowed by the paranoid.)
737*0Sstevel@tonic-gate
738*0Sstevel@tonic-gate=back
739*0Sstevel@tonic-gate
740*0Sstevel@tonic-gate=head2 Utilities
741*0Sstevel@tonic-gate
742*0Sstevel@tonic-gateTo ask for the next layer down use PerlIONext(PerlIO *f).
743*0Sstevel@tonic-gate
744*0Sstevel@tonic-gateTo check that a PerlIO* is valid use PerlIOValid(PerlIO *f).  (All
745*0Sstevel@tonic-gatethis does is really just to check that the pointer is non-NULL and
746*0Sstevel@tonic-gatethat the pointer behind that is non-NULL.)
747*0Sstevel@tonic-gate
748*0Sstevel@tonic-gatePerlIOBase(PerlIO *f) returns the "Base" pointer, or in other words,
749*0Sstevel@tonic-gatethe C<PerlIOl*> pointer.
750*0Sstevel@tonic-gate
751*0Sstevel@tonic-gatePerlIOSelf(PerlIO* f, type) return the PerlIOBase cast to a type.
752*0Sstevel@tonic-gate
753*0Sstevel@tonic-gatePerl_PerlIO_or_Base(PerlIO* f, callback, base, failure, args) either
754*0Sstevel@tonic-gatecalls the I<callback> from the functions of the layer I<f> (just by
755*0Sstevel@tonic-gatethe name of the IO function, like "Read") with the I<args>, or if
756*0Sstevel@tonic-gatethere is no such callback, calls the I<base> version of the callback
757*0Sstevel@tonic-gatewith the same args, or if the f is invalid, set errno to EBADF and
758*0Sstevel@tonic-gatereturn I<failure>.
759*0Sstevel@tonic-gate
760*0Sstevel@tonic-gatePerl_PerlIO_or_fail(PerlIO* f, callback, failure, args) either calls
761*0Sstevel@tonic-gatethe I<callback> of the functions of the layer I<f> with the I<args>,
762*0Sstevel@tonic-gateor if there is no such callback, set errno to EINVAL.  Or if the f is
763*0Sstevel@tonic-gateinvalid, set errno to EBADF and return I<failure>.
764*0Sstevel@tonic-gate
765*0Sstevel@tonic-gatePerl_PerlIO_or_Base_void(PerlIO* f, callback, base, args) either calls
766*0Sstevel@tonic-gatethe I<callback> of the functions of the layer I<f> with the I<args>,
767*0Sstevel@tonic-gateor if there is no such callback, calls the I<base> version of the
768*0Sstevel@tonic-gatecallback with the same args, or if the f is invalid, set errno to
769*0Sstevel@tonic-gateEBADF.
770*0Sstevel@tonic-gate
771*0Sstevel@tonic-gatePerl_PerlIO_or_fail_void(PerlIO* f, callback, args) either calls the
772*0Sstevel@tonic-gateI<callback> of the functions of the layer I<f> with the I<args>, or if
773*0Sstevel@tonic-gatethere is no such callback, set errno to EINVAL.  Or if the f is
774*0Sstevel@tonic-gateinvalid, set errno to EBADF.
775*0Sstevel@tonic-gate
776*0Sstevel@tonic-gate=head2 Implementing PerlIO Layers
777*0Sstevel@tonic-gate
778*0Sstevel@tonic-gateIf you find the implementation document unclear or not sufficient,
779*0Sstevel@tonic-gatelook at the existing PerlIO layer implementations, which include:
780*0Sstevel@tonic-gate
781*0Sstevel@tonic-gate=over
782*0Sstevel@tonic-gate
783*0Sstevel@tonic-gate=item * C implementations
784*0Sstevel@tonic-gate
785*0Sstevel@tonic-gateThe F<perlio.c> and F<perliol.h> in the Perl core implement the
786*0Sstevel@tonic-gate"unix", "perlio", "stdio", "crlf", "utf8", "byte", "raw", "pending"
787*0Sstevel@tonic-gatelayers, and also the "mmap" and "win32" layers if applicable.
788*0Sstevel@tonic-gate(The "win32" is currently unfinished and unused, to see what is used
789*0Sstevel@tonic-gateinstead in Win32, see L<PerlIO/"Querying the layers of filehandles"> .)
790*0Sstevel@tonic-gate
791*0Sstevel@tonic-gatePerlIO::encoding, PerlIO::scalar, PerlIO::via in the Perl core.
792*0Sstevel@tonic-gate
793*0Sstevel@tonic-gatePerlIO::gzip and APR::PerlIO (mod_perl 2.0) on CPAN.
794*0Sstevel@tonic-gate
795*0Sstevel@tonic-gate=item * Perl implementations
796*0Sstevel@tonic-gate
797*0Sstevel@tonic-gatePerlIO::via::QuotedPrint in the Perl core and PerlIO::via::* on CPAN.
798*0Sstevel@tonic-gate
799*0Sstevel@tonic-gate=back
800*0Sstevel@tonic-gate
801*0Sstevel@tonic-gateIf you are creating a PerlIO layer, you may want to be lazy, in other
802*0Sstevel@tonic-gatewords, implement only the methods that interest you.  The other methods
803*0Sstevel@tonic-gateyou can either replace with the "blank" methods
804*0Sstevel@tonic-gate
805*0Sstevel@tonic-gate    PerlIOBase_noop_ok
806*0Sstevel@tonic-gate    PerlIOBase_noop_fail
807*0Sstevel@tonic-gate
808*0Sstevel@tonic-gate(which do nothing, and return zero and -1, respectively) or for
809*0Sstevel@tonic-gatecertain methods you may assume a default behaviour by using a NULL
810*0Sstevel@tonic-gatemethod.  The Open method looks for help in the 'parent' layer.
811*0Sstevel@tonic-gateThe following table summarizes the behaviour:
812*0Sstevel@tonic-gate
813*0Sstevel@tonic-gate    method      behaviour with NULL
814*0Sstevel@tonic-gate
815*0Sstevel@tonic-gate    Clearerr    PerlIOBase_clearerr
816*0Sstevel@tonic-gate    Close       PerlIOBase_close
817*0Sstevel@tonic-gate    Dup         PerlIOBase_dup
818*0Sstevel@tonic-gate    Eof         PerlIOBase_eof
819*0Sstevel@tonic-gate    Error       PerlIOBase_error
820*0Sstevel@tonic-gate    Fileno      PerlIOBase_fileno
821*0Sstevel@tonic-gate    Fill        FAILURE
822*0Sstevel@tonic-gate    Flush       SUCCESS
823*0Sstevel@tonic-gate    Getarg      SUCCESS
824*0Sstevel@tonic-gate    Get_base    FAILURE
825*0Sstevel@tonic-gate    Get_bufsiz  FAILURE
826*0Sstevel@tonic-gate    Get_cnt     FAILURE
827*0Sstevel@tonic-gate    Get_ptr     FAILURE
828*0Sstevel@tonic-gate    Open        INHERITED
829*0Sstevel@tonic-gate    Popped      SUCCESS
830*0Sstevel@tonic-gate    Pushed      SUCCESS
831*0Sstevel@tonic-gate    Read        PerlIOBase_read
832*0Sstevel@tonic-gate    Seek        FAILURE
833*0Sstevel@tonic-gate    Set_cnt     FAILURE
834*0Sstevel@tonic-gate    Set_ptrcnt  FAILURE
835*0Sstevel@tonic-gate    Setlinebuf  PerlIOBase_setlinebuf
836*0Sstevel@tonic-gate    Tell        FAILURE
837*0Sstevel@tonic-gate    Unread      PerlIOBase_unread
838*0Sstevel@tonic-gate    Write       FAILURE
839*0Sstevel@tonic-gate
840*0Sstevel@tonic-gate FAILURE        Set errno (to EINVAL in UNIXish, to LIB$_INVARG in VMS) and
841*0Sstevel@tonic-gate                return -1 (for numeric return values) or NULL (for pointers)
842*0Sstevel@tonic-gate INHERITED      Inherited from the layer below
843*0Sstevel@tonic-gate SUCCESS        Return 0 (for numeric return values) or a pointer
844*0Sstevel@tonic-gate
845*0Sstevel@tonic-gate=head2 Core Layers
846*0Sstevel@tonic-gate
847*0Sstevel@tonic-gateThe file C<perlio.c> provides the following layers:
848*0Sstevel@tonic-gate
849*0Sstevel@tonic-gate=over 4
850*0Sstevel@tonic-gate
851*0Sstevel@tonic-gate=item "unix"
852*0Sstevel@tonic-gate
853*0Sstevel@tonic-gateA basic non-buffered layer which calls Unix/POSIX C<read()>, C<write()>,
854*0Sstevel@tonic-gateC<lseek()>, C<close()>. No buffering. Even on platforms that distinguish
855*0Sstevel@tonic-gatebetween O_TEXT and O_BINARY this layer is always O_BINARY.
856*0Sstevel@tonic-gate
857*0Sstevel@tonic-gate=item "perlio"
858*0Sstevel@tonic-gate
859*0Sstevel@tonic-gateA very complete generic buffering layer which provides the whole of
860*0Sstevel@tonic-gatePerlIO API. It is also intended to be used as a "base class" for other
861*0Sstevel@tonic-gatelayers. (For example its C<Read()> method is implemented in terms of
862*0Sstevel@tonic-gatethe C<Get_cnt()>/C<Get_ptr()>/C<Set_ptrcnt()> methods).
863*0Sstevel@tonic-gate
864*0Sstevel@tonic-gate"perlio" over "unix" provides a complete replacement for stdio as seen
865*0Sstevel@tonic-gatevia PerlIO API. This is the default for USE_PERLIO when system's stdio
866*0Sstevel@tonic-gatedoes not permit perl's "fast gets" access, and which do not
867*0Sstevel@tonic-gatedistinguish between C<O_TEXT> and C<O_BINARY>.
868*0Sstevel@tonic-gate
869*0Sstevel@tonic-gate=item "stdio"
870*0Sstevel@tonic-gate
871*0Sstevel@tonic-gateA layer which provides the PerlIO API via the layer scheme, but
872*0Sstevel@tonic-gateimplements it by calling system's stdio. This is (currently) the default
873*0Sstevel@tonic-gateif system's stdio provides sufficient access to allow perl's "fast gets"
874*0Sstevel@tonic-gateaccess and which do not distinguish between C<O_TEXT> and C<O_BINARY>.
875*0Sstevel@tonic-gate
876*0Sstevel@tonic-gate=item "crlf"
877*0Sstevel@tonic-gate
878*0Sstevel@tonic-gateA layer derived using "perlio" as a base class. It provides Win32-like
879*0Sstevel@tonic-gate"\n" to CR,LF translation. Can either be applied above "perlio" or serve
880*0Sstevel@tonic-gateas the buffer layer itself. "crlf" over "unix" is the default if system
881*0Sstevel@tonic-gatedistinguishes between C<O_TEXT> and C<O_BINARY> opens. (At some point
882*0Sstevel@tonic-gate"unix" will be replaced by a "native" Win32 IO layer on that platform,
883*0Sstevel@tonic-gateas Win32's read/write layer has various drawbacks.) The "crlf" layer is
884*0Sstevel@tonic-gatea reasonable model for a layer which transforms data in some way.
885*0Sstevel@tonic-gate
886*0Sstevel@tonic-gate=item "mmap"
887*0Sstevel@tonic-gate
888*0Sstevel@tonic-gateIf Configure detects C<mmap()> functions this layer is provided (with
889*0Sstevel@tonic-gate"perlio" as a "base") which does "read" operations by mmap()ing the
890*0Sstevel@tonic-gatefile. Performance improvement is marginal on modern systems, so it is
891*0Sstevel@tonic-gatemainly there as a proof of concept. It is likely to be unbundled from
892*0Sstevel@tonic-gatethe core at some point. The "mmap" layer is a reasonable model for a
893*0Sstevel@tonic-gateminimalist "derived" layer.
894*0Sstevel@tonic-gate
895*0Sstevel@tonic-gate=item "pending"
896*0Sstevel@tonic-gate
897*0Sstevel@tonic-gateAn "internal" derivative of "perlio" which can be used to provide
898*0Sstevel@tonic-gateUnread() function for layers which have no buffer or cannot be
899*0Sstevel@tonic-gatebothered.  (Basically this layer's C<Fill()> pops itself off the stack
900*0Sstevel@tonic-gateand so resumes reading from layer below.)
901*0Sstevel@tonic-gate
902*0Sstevel@tonic-gate=item "raw"
903*0Sstevel@tonic-gate
904*0Sstevel@tonic-gateA dummy layer which never exists on the layer stack. Instead when
905*0Sstevel@tonic-gate"pushed" it actually pops the stack removing itself, it then calls
906*0Sstevel@tonic-gateBinmode function table entry on all the layers in the stack - normally
907*0Sstevel@tonic-gatethis (via PerlIOBase_binmode) removes any layers which do not have
908*0Sstevel@tonic-gateC<PERLIO_K_RAW> bit set. Layers can modify that behaviour by defining
909*0Sstevel@tonic-gatetheir own Binmode entry.
910*0Sstevel@tonic-gate
911*0Sstevel@tonic-gate=item "utf8"
912*0Sstevel@tonic-gate
913*0Sstevel@tonic-gateAnother dummy layer. When pushed it pops itself and sets the
914*0Sstevel@tonic-gateC<PERLIO_F_UTF8> flag on the layer which was (and now is once more)
915*0Sstevel@tonic-gatethe top of the stack.
916*0Sstevel@tonic-gate
917*0Sstevel@tonic-gate=back
918*0Sstevel@tonic-gate
919*0Sstevel@tonic-gateIn addition F<perlio.c> also provides a number of C<PerlIOBase_xxxx()>
920*0Sstevel@tonic-gatefunctions which are intended to be used in the table slots of classes
921*0Sstevel@tonic-gatewhich do not need to do anything special for a particular method.
922*0Sstevel@tonic-gate
923*0Sstevel@tonic-gate=head2 Extension Layers
924*0Sstevel@tonic-gate
925*0Sstevel@tonic-gateLayers can made available by extension modules. When an unknown layer
926*0Sstevel@tonic-gateis encountered the PerlIO code will perform the equivalent of :
927*0Sstevel@tonic-gate
928*0Sstevel@tonic-gate   use PerlIO 'layer';
929*0Sstevel@tonic-gate
930*0Sstevel@tonic-gateWhere I<layer> is the unknown layer. F<PerlIO.pm> will then attempt to:
931*0Sstevel@tonic-gate
932*0Sstevel@tonic-gate   require PerlIO::layer;
933*0Sstevel@tonic-gate
934*0Sstevel@tonic-gateIf after that process the layer is still not defined then the C<open>
935*0Sstevel@tonic-gatewill fail.
936*0Sstevel@tonic-gate
937*0Sstevel@tonic-gateThe following extension layers are bundled with perl:
938*0Sstevel@tonic-gate
939*0Sstevel@tonic-gate=over 4
940*0Sstevel@tonic-gate
941*0Sstevel@tonic-gate=item ":encoding"
942*0Sstevel@tonic-gate
943*0Sstevel@tonic-gate   use Encoding;
944*0Sstevel@tonic-gate
945*0Sstevel@tonic-gatemakes this layer available, although F<PerlIO.pm> "knows" where to
946*0Sstevel@tonic-gatefind it.  It is an example of a layer which takes an argument as it is
947*0Sstevel@tonic-gatecalled thus:
948*0Sstevel@tonic-gate
949*0Sstevel@tonic-gate   open( $fh, "<:encoding(iso-8859-7)", $pathname );
950*0Sstevel@tonic-gate
951*0Sstevel@tonic-gate=item ":scalar"
952*0Sstevel@tonic-gate
953*0Sstevel@tonic-gateProvides support for reading data from and writing data to a scalar.
954*0Sstevel@tonic-gate
955*0Sstevel@tonic-gate   open( $fh, "+<:scalar", \$scalar );
956*0Sstevel@tonic-gate
957*0Sstevel@tonic-gateWhen a handle is so opened, then reads get bytes from the string value
958*0Sstevel@tonic-gateof I<$scalar>, and writes change the value. In both cases the position
959*0Sstevel@tonic-gatein I<$scalar> starts as zero but can be altered via C<seek>, and
960*0Sstevel@tonic-gatedetermined via C<tell>.
961*0Sstevel@tonic-gate
962*0Sstevel@tonic-gatePlease note that this layer is implied when calling open() thus:
963*0Sstevel@tonic-gate
964*0Sstevel@tonic-gate   open( $fh, "+<", \$scalar );
965*0Sstevel@tonic-gate
966*0Sstevel@tonic-gate=item ":via"
967*0Sstevel@tonic-gate
968*0Sstevel@tonic-gateProvided to allow layers to be implemented as Perl code.  For instance:
969*0Sstevel@tonic-gate
970*0Sstevel@tonic-gate   use PerlIO::via::StripHTML;
971*0Sstevel@tonic-gate   open( my $fh, "<:via(StripHTML)", "index.html" );
972*0Sstevel@tonic-gate
973*0Sstevel@tonic-gateSee L<PerlIO::via> for details.
974*0Sstevel@tonic-gate
975*0Sstevel@tonic-gate=back
976*0Sstevel@tonic-gate
977*0Sstevel@tonic-gate=head1 TODO
978*0Sstevel@tonic-gate
979*0Sstevel@tonic-gateThings that need to be done to improve this document.
980*0Sstevel@tonic-gate
981*0Sstevel@tonic-gate=over
982*0Sstevel@tonic-gate
983*0Sstevel@tonic-gate=item *
984*0Sstevel@tonic-gate
985*0Sstevel@tonic-gateExplain how to make a valid fh without going through open()(i.e. apply
986*0Sstevel@tonic-gatea layer). For example if the file is not opened through perl, but we
987*0Sstevel@tonic-gatewant to get back a fh, like it was opened by Perl.
988*0Sstevel@tonic-gate
989*0Sstevel@tonic-gateHow PerlIO_apply_layera fits in, where its docs, was it made public?
990*0Sstevel@tonic-gate
991*0Sstevel@tonic-gateCurrently the example could be something like this:
992*0Sstevel@tonic-gate
993*0Sstevel@tonic-gate  PerlIO *foo_to_PerlIO(pTHX_ char *mode, ...)
994*0Sstevel@tonic-gate  {
995*0Sstevel@tonic-gate      char *mode; /* "w", "r", etc */
996*0Sstevel@tonic-gate      const char *layers = ":APR"; /* the layer name */
997*0Sstevel@tonic-gate      PerlIO *f = PerlIO_allocate(aTHX);
998*0Sstevel@tonic-gate      if (!f) {
999*0Sstevel@tonic-gate          return NULL;
1000*0Sstevel@tonic-gate      }
1001*0Sstevel@tonic-gate
1002*0Sstevel@tonic-gate      PerlIO_apply_layers(aTHX_ f, mode, layers);
1003*0Sstevel@tonic-gate
1004*0Sstevel@tonic-gate      if (f) {
1005*0Sstevel@tonic-gate          PerlIOAPR *st = PerlIOSelf(f, PerlIOAPR);
1006*0Sstevel@tonic-gate          /* fill in the st struct, as in _open() */
1007*0Sstevel@tonic-gate          st->file = file;
1008*0Sstevel@tonic-gate          PerlIOBase(f)->flags |= PERLIO_F_OPEN;
1009*0Sstevel@tonic-gate
1010*0Sstevel@tonic-gate          return f;
1011*0Sstevel@tonic-gate      }
1012*0Sstevel@tonic-gate      return NULL;
1013*0Sstevel@tonic-gate  }
1014*0Sstevel@tonic-gate
1015*0Sstevel@tonic-gate=item *
1016*0Sstevel@tonic-gate
1017*0Sstevel@tonic-gatefix/add the documentation in places marked as XXX.
1018*0Sstevel@tonic-gate
1019*0Sstevel@tonic-gate=item *
1020*0Sstevel@tonic-gate
1021*0Sstevel@tonic-gateThe handling of errors by the layer is not specified. e.g. when $!
1022*0Sstevel@tonic-gateshould be set explicitly, when the error handling should be just
1023*0Sstevel@tonic-gatedelegated to the top layer.
1024*0Sstevel@tonic-gate
1025*0Sstevel@tonic-gateProbably give some hints on using SETERRNO() or pointers to where they
1026*0Sstevel@tonic-gatecan be found.
1027*0Sstevel@tonic-gate
1028*0Sstevel@tonic-gate=item *
1029*0Sstevel@tonic-gate
1030*0Sstevel@tonic-gateI think it would help to give some concrete examples to make it easier
1031*0Sstevel@tonic-gateto understand the API. Of course I agree that the API has to be
1032*0Sstevel@tonic-gateconcise, but since there is no second document that is more of a
1033*0Sstevel@tonic-gateguide, I think that it'd make it easier to start with the doc which is
1034*0Sstevel@tonic-gatean API, but has examples in it in places where things are unclear, to
1035*0Sstevel@tonic-gatea person who is not a PerlIO guru (yet).
1036*0Sstevel@tonic-gate
1037*0Sstevel@tonic-gate=back
1038*0Sstevel@tonic-gate
1039*0Sstevel@tonic-gate=cut
1040