1 /* Generic stream implementation -- header file.
2 Copyright (C) 1995 Free Software Foundation, Inc.
3 Copyright (C) 1996 Ben Wing.
5 This file is part of XEmacs.
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Synched up with: Not in FSF. */
24 /* Written by Ben Wing. */
26 #ifndef INCLUDED_lstream_h_
27 #define INCLUDED_lstream_h_
29 /************************************************************************/
30 /* definition of Lstream object */
31 /************************************************************************/
33 DECLARE_LRECORD (lstream, struct lstream);
34 #define XLSTREAM(x) XRECORD (x, lstream, struct lstream)
35 #define XSETLSTREAM(x, p) XSETRECORD (x, p, lstream)
36 #define LSTREAMP(x) RECORDP (x, lstream)
37 /* #define CHECK_LSTREAM(x) CHECK_RECORD (x, lstream)
38 Lstream pointers should never escape to the Lisp level, so
39 functions should not be doing this. */
45 typedef enum lstream_buffering
49 /* Buffer until a '\n' character is reached. */
50 LSTREAM_LINE_BUFFERED,
51 /* Buffer in standard-size (i.e. 512-byte) blocks. */
52 LSTREAM_BLOCK_BUFFERED,
53 /* Buffer in blocks of a specified size. */
54 LSTREAM_BLOCKN_BUFFERED,
55 /* Buffer until the stream is closed (only applies to write-only
56 streams). Only one call to the stream writer will be made,
57 and that is when the stream is closed. */
61 /* Methods defining how this stream works. Some may be undefined. */
63 /* We do not implement the seek/tell paradigm. I tried to do that,
64 but getting the semantics right in the presence of buffering is
65 extremely tricky and very error-prone and basically not worth it.
66 This is especially the case with complicated streams like
67 decoding streams -- the seek pointer in this case can't be a single
68 integer but has to be a whole complicated structure that records
69 all of the stream's state at the time.
71 Rewind semantics are generally easy to implement, so we do provide
72 a rewind method. Even rewind() may not be available on a stream,
73 however -- e.g. on process output. */
75 typedef struct lstream_implementation
78 size_t size; /* Number of additional bytes to be allocated with this
79 stream. Access this data using Lstream_data(). */
80 /* Read some data from the stream's end and store it into DATA, which
81 can hold SIZE bytes. Return the number of bytes read. A return
82 value of 0 means no bytes can be read at this time. This may
83 be because of an EOF, or because there is a granularity greater
84 than one byte that the stream imposes on the returned data, and
85 SIZE is less than this granularity. (This will happen frequently
86 for streams that need to return whole characters, because
87 Lstream_read() calls the reader function repeatedly until it
88 has the number of bytes it wants or until 0 is returned.)
89 The lstream functions do not treat a 0 return as EOF or do
90 anything special; however, the calling function will interpret
91 any 0 it gets back as EOF. This will normally not happen unless
92 the caller calls Lstream_read() with a very small size.
94 This function can be NULL if the stream is output-only. */
95 /* The omniscient mly, blinded by the irresistible thrall of Common
96 Lisp, thinks that it is bogus that the types and implementations
97 of input and output streams are the same. */
98 ssize_t (*reader) (Lstream *stream, unsigned char *data, size_t size);
99 /* Send some data to the stream's end. Data to be sent is in DATA
100 and is SIZE bytes. Return the number of bytes sent. This
101 function can send and return fewer bytes than is passed in; in
102 that case, the function will just be called again until there is
103 no data left or 0 is returned. A return value of 0 means that no
104 more data can be currently stored, but there is no error; the
105 data will be squirrelled away until the writer can accept
106 data. (This is useful, e.g., of you're dealing with a
107 non-blocking file descriptor and are getting EWOULDBLOCK errors.)
108 This function can be NULL if the stream is input-only. */
109 ssize_t (*writer) (Lstream *stream, const unsigned char *data, size_t size);
110 /* Return non-zero if the last write operation on the stream resulted
111 in an attempt to block (EWOULDBLOCK). If this method does not
112 exists, the implementation returns 0 */
113 int (*was_blocked_p) (Lstream *stream);
114 /* Rewind the stream. If this is NULL, the stream is not seekable. */
115 int (*rewinder) (Lstream *stream);
116 /* Indicate whether this stream is seekable -- i.e. it can be rewound.
117 This method is ignored if the stream does not have a rewind
118 method. If this method is not present, the result is determined
119 by whether a rewind method is present. */
120 int (*seekable_p) (Lstream *stream);
121 /* Perform any additional operations necessary to flush the
122 data in this stream. */
123 int (*flusher) (Lstream *stream);
124 /* Perform any additional operations necessary to close this
125 stream down. May be NULL. This function is called when
126 Lstream_close() is called or when the stream is garbage-
127 collected. When this function is called, all pending data
128 in the stream will already have been written out. */
129 int (*closer) (Lstream *stream);
130 /* Mark this object for garbage collection. Same semantics as
131 a standard Lisp_Object marker. This function can be NULL. */
132 Lisp_Object (*marker) (Lisp_Object lstream);
133 } Lstream_implementation;
135 #define DEFINE_LSTREAM_IMPLEMENTATION(name,c_name,size) \
136 Lstream_implementation c_name[1] = \
137 { { (name), (size) } }
139 #define LSTREAM_FL_IS_OPEN 1
140 #define LSTREAM_FL_READ 2
141 #define LSTREAM_FL_WRITE 4
142 #define LSTREAM_FL_NO_PARTIAL_CHARS 8
143 #define LSTREAM_FL_CLOSE_AT_DISKSAVE 16
147 struct lcrecord_header header;
148 const Lstream_implementation *imp; /* methods for this stream */
149 Lstream_buffering buffering; /* type of buffering in use */
150 size_t buffering_size; /* number of bytes buffered */
152 unsigned char *in_buffer; /* holds characters read from stream end */
153 size_t in_buffer_size; /* allocated size of buffer */
154 size_t in_buffer_current; /* number of characters in buffer */
155 size_t in_buffer_ind; /* pointer to next character to take from buffer */
157 unsigned char *out_buffer; /* holds characters to write to stream end */
158 size_t out_buffer_size; /* allocated size of buffer */
159 size_t out_buffer_ind; /* pointer to next buffer spot to write a character */
161 /* The unget buffer is more or less a stack -- things get pushed
162 onto the end and read back from the end. Lstream_read()
163 basically reads backwards from the end to get stuff; Lstream_unread()
164 similarly has to push the data on backwards. */
165 unsigned char *unget_buffer; /* holds characters pushed back onto input */
166 size_t unget_buffer_size; /* allocated size of buffer */
167 size_t unget_buffer_ind; /* pointer to next buffer spot to write a character */
170 long flags; /* Align pointer for 64 bit machines (kny) */
174 #define LSTREAM_TYPE_P(lstr, type) \
175 ((lstr)->imp == lstream_##type)
177 #ifdef ERROR_CHECK_TYPECHECK
178 INLINE_HEADER struct lstream *
179 error_check_lstream_type (struct lstream *stream,
180 const Lstream_implementation *imp);
181 INLINE_HEADER struct lstream *
182 error_check_lstream_type (struct lstream *stream,
183 const Lstream_implementation *imp)
185 assert (stream->imp == imp);
188 # define LSTREAM_TYPE_DATA(lstr, type) \
189 ((struct type##_stream *) \
190 Lstream_data (error_check_lstream_type(lstr, lstream_##type)))
192 # define LSTREAM_TYPE_DATA(lstr, type) \
193 ((struct type##_stream *) Lstream_data (lstr))
196 /* Declare that lstream-type TYPE has method M; used in
197 initialization routines */
198 #define LSTREAM_HAS_METHOD(type, m) \
199 (lstream_##type->m = type##_##m)
202 Lstream *Lstream_new (const Lstream_implementation *imp,
204 void Lstream_reopen (Lstream *lstr);
205 void Lstream_set_buffering (Lstream *lstr, Lstream_buffering buffering,
207 int Lstream_flush (Lstream *lstr);
208 int Lstream_flush_out (Lstream *lstr);
209 int Lstream_fputc (Lstream *lstr, int c);
210 int Lstream_fgetc (Lstream *lstr);
211 void Lstream_fungetc (Lstream *lstr, int c);
212 ssize_t Lstream_read (Lstream *lstr, void *data, size_t size);
213 ssize_t Lstream_write (Lstream *lstr, const void *data, size_t size);
214 int Lstream_was_blocked_p (Lstream *lstr);
215 void Lstream_unread (Lstream *lstr, const void *data, size_t size);
216 int Lstream_rewind (Lstream *lstr);
217 int Lstream_seekable_p (Lstream *lstr);
218 int Lstream_close (Lstream *lstr);
219 void Lstream_delete (Lstream *lstr);
220 void Lstream_set_character_mode (Lstream *str);
222 /* Call the function equivalent if the out buffer is full. Otherwise,
223 add to the end of the out buffer and, if line buffering is called for
224 and the character marks the end of a line, write out the buffer. */
226 #define Lstream_putc(stream, c) \
227 ((stream)->out_buffer_ind >= (stream)->out_buffer_size ? \
228 Lstream_fputc (stream, c) : \
229 ((stream)->out_buffer[(stream)->out_buffer_ind++] = \
230 (unsigned char) (c), \
231 (stream)->byte_count++, \
232 (stream)->buffering == LSTREAM_LINE_BUFFERED && \
233 (stream)->out_buffer[(stream)->out_buffer_ind - 1] == '\n' ? \
234 Lstream_flush_out (stream) : 0))
236 /* Retrieve from unget buffer if there are any characters there;
237 else retrieve from in buffer if there's anything there;
238 else call the function equivalent */
239 #define Lstream_getc(stream) \
240 ((stream)->unget_buffer_ind > 0 ? \
241 ((stream)->byte_count++, \
242 (stream)->unget_buffer[--(stream)->unget_buffer_ind]) : \
243 (stream)->in_buffer_ind < (stream)->in_buffer_current ? \
244 ((stream)->byte_count++, \
245 (stream)->in_buffer[(stream)->in_buffer_ind++]) : \
246 Lstream_fgetc (stream))
248 /* Add to the end if it won't overflow buffer; otherwise call the
249 function equivalent */
250 #define Lstream_ungetc(stream, c) \
251 ((stream)->unget_buffer_ind >= (stream)->unget_buffer_size ? \
252 Lstream_fungetc (stream, c) : \
253 (void) ((stream)->byte_count--, \
254 ((stream)->unget_buffer[(stream)->unget_buffer_ind++] = \
255 (unsigned char) (c))))
257 #define Lstream_data(stream) ((void *) ((stream)->data))
258 #define Lstream_byte_count(stream) ((stream)->byte_count)
261 /************************************************************************/
262 /* working with an Lstream as a stream of Emchars */
263 /************************************************************************/
268 #include "mule-charset.h"
271 INLINE_HEADER Emchar Lstream_get_emchar (Lstream *stream);
273 Lstream_get_emchar (Lstream *stream)
275 int c = Lstream_getc (stream);
276 return (c < 0x80 /* c == EOF || BYTE_ASCII_P (c) */
278 : Lstream_get_emchar_1 (stream, c));
281 INLINE_HEADER int Lstream_put_emchar (Lstream *stream, Emchar ch);
283 Lstream_put_emchar (Lstream *stream, Emchar ch)
285 return CHAR_ASCII_P (ch) ?
286 Lstream_putc (stream, ch) :
287 Lstream_fput_emchar (stream, ch);
290 INLINE_HEADER void Lstream_unget_emchar (Lstream *stream, Emchar ch);
292 Lstream_unget_emchar (Lstream *stream, Emchar ch)
294 if (CHAR_ASCII_P (ch))
295 Lstream_ungetc (stream, ch);
297 Lstream_funget_emchar (stream, ch);
301 # define Lstream_get_emchar(stream) Lstream_getc (stream)
302 # define Lstream_put_emchar(stream, ch) Lstream_putc (stream, ch)
303 # define Lstream_unget_emchar(stream, ch) Lstream_ungetc (stream, ch)
305 #endif /* not MULE */
308 /************************************************************************/
309 /* Lstream implementations */
310 /************************************************************************/
312 /* Flags we can pass to the filedesc and stdio streams. */
314 /* If set, close the descriptor or FILE * when the stream is closed. */
315 #define LSTR_CLOSING 1
317 /* If set, allow quitting out of the actual I/O. */
318 #define LSTR_ALLOW_QUIT 2
320 /* If set and filedesc_stream_set_pty_flushing() has been called
321 on the stream, do not send more than pty_max_bytes on a single
322 line without flushing the data out using the eof_char. */
323 #define LSTR_PTY_FLUSHING 4
325 /* If set, an EWOULDBLOCK error is not treated as an error but
326 simply causes the write function to return 0 as the number
327 of bytes written out. */
328 #define LSTR_BLOCKED_OK 8
330 Lisp_Object make_stdio_input_stream (FILE *stream, int flags);
331 Lisp_Object make_stdio_output_stream (FILE *stream, int flags);
332 Lisp_Object make_filedesc_input_stream (int filedesc, int offset, int count,
334 Lisp_Object make_filedesc_output_stream (int filedesc, int offset, int count,
336 void filedesc_stream_set_pty_flushing (Lstream *stream,
339 int filedesc_stream_fd (Lstream *stream);
340 Lisp_Object make_lisp_string_input_stream (Lisp_Object string,
343 Lisp_Object make_fixed_buffer_input_stream (const void *buf, size_t size);
344 Lisp_Object make_fixed_buffer_output_stream (void *buf, size_t size);
345 const unsigned char *fixed_buffer_input_stream_ptr (Lstream *stream);
346 unsigned char *fixed_buffer_output_stream_ptr (Lstream *stream);
347 Lisp_Object make_resizing_buffer_output_stream (void);
348 unsigned char *resizing_buffer_stream_ptr (Lstream *stream);
349 Lisp_Object make_dynarr_output_stream (unsigned_char_dynarr *dyn);
350 #define LSTR_SELECTIVE 1
351 #define LSTR_IGNORE_ACCESSIBLE 2
352 Lisp_Object make_lisp_buffer_input_stream (struct buffer *buf, Bufpos start,
353 Bufpos end, int flags);
354 Lisp_Object make_lisp_buffer_output_stream (struct buffer *buf, Bufpos pos,
356 Bufpos lisp_buffer_stream_startpos (Lstream *stream);
358 #endif /* INCLUDED_lstream_h_ */