428
|
1 /* Generic stream implementation -- header file.
|
|
2 Copyright (C) 1995 Free Software Foundation, Inc.
|
|
3 Copyright (C) 1996 Ben Wing.
|
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
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
|
|
10 later version.
|
|
11
|
|
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
|
|
15 for more details.
|
|
16
|
|
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. */
|
|
21
|
|
22 /* Synched up with: Not in FSF. */
|
|
23
|
|
24 /* Written by Ben Wing. */
|
|
25
|
440
|
26 #ifndef INCLUDED_lstream_h_
|
|
27 #define INCLUDED_lstream_h_
|
428
|
28
|
|
29 /************************************************************************/
|
|
30 /* definition of Lstream object */
|
|
31 /************************************************************************/
|
|
32
|
|
33 DECLARE_LRECORD (lstream, struct lstream);
|
|
34 #define XLSTREAM(x) XRECORD (x, lstream, struct lstream)
|
|
35 #define XSETLSTREAM(x, p) XSETRECORD (x, p, lstream)
|
563
|
36 #define wrap_lstream(p) wrap_object (p)
|
428
|
37 #define LSTREAMP(x) RECORDP (x, lstream)
|
|
38 /* #define CHECK_LSTREAM(x) CHECK_RECORD (x, lstream)
|
|
39 Lstream pointers should never escape to the Lisp level, so
|
|
40 functions should not be doing this. */
|
|
41
|
|
42 #ifndef EOF
|
|
43 #define EOF (-1)
|
|
44 #endif
|
|
45
|
462
|
46 /* Typedef specifying a count of bytes in a data block to be written
|
|
47 out or read in, using Lstream_read(), Lstream_write(), and related
|
|
48 functions. This MUST BE SIGNED, since it also is used in functions
|
|
49 that return the number of bytes actually read to or written from in
|
|
50 an operation, and these functions can return -1 to signal error.
|
|
51
|
|
52 Note that the standard Unix read() and write() functions define the
|
|
53 count going in as a size_t, which is UNSIGNED, and the count going
|
|
54 out as an ssize_t, which is SIGNED. This is a horrible design
|
|
55 flaw. Not only is it highly likely to lead to logic errors when a
|
|
56 -1 gets interpreted as a large positive number, but operations are
|
|
57 bound to fail in all sorts of horrible ways when a number in the
|
|
58 upper-half of the size_t range is passed in -- this number is
|
|
59 unrepresentable as an ssize_t, so code that checks to see how many
|
|
60 bytes are actually written (which is mandatory if you are dealing
|
|
61 with certain types of devices) will get completely screwed up.
|
|
62 */
|
|
63
|
|
64 typedef EMACS_INT Lstream_data_count;
|
|
65
|
428
|
66 typedef enum lstream_buffering
|
|
67 {
|
|
68 /* No buffering. */
|
|
69 LSTREAM_UNBUFFERED,
|
|
70 /* Buffer until a '\n' character is reached. */
|
|
71 LSTREAM_LINE_BUFFERED,
|
|
72 /* Buffer in standard-size (i.e. 512-byte) blocks. */
|
|
73 LSTREAM_BLOCK_BUFFERED,
|
|
74 /* Buffer in blocks of a specified size. */
|
|
75 LSTREAM_BLOCKN_BUFFERED,
|
|
76 /* Buffer until the stream is closed (only applies to write-only
|
|
77 streams). Only one call to the stream writer will be made,
|
|
78 and that is when the stream is closed. */
|
|
79 LSTREAM_UNLIMITED
|
|
80 } Lstream_buffering;
|
|
81
|
|
82 /* Methods defining how this stream works. Some may be undefined. */
|
|
83
|
|
84 /* We do not implement the seek/tell paradigm. I tried to do that,
|
|
85 but getting the semantics right in the presence of buffering is
|
|
86 extremely tricky and very error-prone and basically not worth it.
|
|
87 This is especially the case with complicated streams like
|
|
88 decoding streams -- the seek pointer in this case can't be a single
|
|
89 integer but has to be a whole complicated structure that records
|
|
90 all of the stream's state at the time.
|
|
91
|
|
92 Rewind semantics are generally easy to implement, so we do provide
|
|
93 a rewind method. Even rewind() may not be available on a stream,
|
|
94 however -- e.g. on process output. */
|
|
95
|
|
96 typedef struct lstream_implementation
|
|
97 {
|
442
|
98 const char *name;
|
462
|
99 Lstream_data_count size; /* Number of additional bytes to be
|
|
100 allocated with this stream. Access this
|
|
101 data using Lstream_data(). */
|
428
|
102 /* Read some data from the stream's end and store it into DATA, which
|
|
103 can hold SIZE bytes. Return the number of bytes read. A return
|
|
104 value of 0 means no bytes can be read at this time. This may
|
|
105 be because of an EOF, or because there is a granularity greater
|
|
106 than one byte that the stream imposes on the returned data, and
|
|
107 SIZE is less than this granularity. (This will happen frequently
|
|
108 for streams that need to return whole characters, because
|
|
109 Lstream_read() calls the reader function repeatedly until it
|
|
110 has the number of bytes it wants or until 0 is returned.)
|
|
111 The lstream functions do not treat a 0 return as EOF or do
|
|
112 anything special; however, the calling function will interpret
|
|
113 any 0 it gets back as EOF. This will normally not happen unless
|
|
114 the caller calls Lstream_read() with a very small size.
|
|
115
|
|
116 This function can be NULL if the stream is output-only. */
|
442
|
117 /* The omniscient mly, blinded by the irresistible thrall of Common
|
428
|
118 Lisp, thinks that it is bogus that the types and implementations
|
|
119 of input and output streams are the same. */
|
462
|
120 Lstream_data_count (*reader) (Lstream *stream, unsigned char *data,
|
|
121 Lstream_data_count size);
|
428
|
122 /* Send some data to the stream's end. Data to be sent is in DATA
|
|
123 and is SIZE bytes. Return the number of bytes sent. This
|
|
124 function can send and return fewer bytes than is passed in; in
|
|
125 that case, the function will just be called again until there is
|
|
126 no data left or 0 is returned. A return value of 0 means that no
|
|
127 more data can be currently stored, but there is no error; the
|
|
128 data will be squirrelled away until the writer can accept
|
|
129 data. (This is useful, e.g., of you're dealing with a
|
|
130 non-blocking file descriptor and are getting EWOULDBLOCK errors.)
|
|
131 This function can be NULL if the stream is input-only. */
|
462
|
132 Lstream_data_count (*writer) (Lstream *stream, const unsigned char *data,
|
|
133 Lstream_data_count size);
|
428
|
134 /* Return non-zero if the last write operation on the stream resulted
|
|
135 in an attempt to block (EWOULDBLOCK). If this method does not
|
|
136 exists, the implementation returns 0 */
|
|
137 int (*was_blocked_p) (Lstream *stream);
|
|
138 /* Rewind the stream. If this is NULL, the stream is not seekable. */
|
|
139 int (*rewinder) (Lstream *stream);
|
|
140 /* Indicate whether this stream is seekable -- i.e. it can be rewound.
|
|
141 This method is ignored if the stream does not have a rewind
|
|
142 method. If this method is not present, the result is determined
|
|
143 by whether a rewind method is present. */
|
|
144 int (*seekable_p) (Lstream *stream);
|
|
145 /* Perform any additional operations necessary to flush the
|
|
146 data in this stream. */
|
|
147 int (*flusher) (Lstream *stream);
|
|
148 /* Perform any additional operations necessary to close this
|
|
149 stream down. May be NULL. This function is called when
|
|
150 Lstream_close() is called or when the stream is garbage-
|
|
151 collected. When this function is called, all pending data
|
|
152 in the stream will already have been written out. */
|
|
153 int (*closer) (Lstream *stream);
|
|
154 /* Mark this object for garbage collection. Same semantics as
|
|
155 a standard Lisp_Object marker. This function can be NULL. */
|
|
156 Lisp_Object (*marker) (Lisp_Object lstream);
|
|
157 } Lstream_implementation;
|
|
158
|
|
159 #define DEFINE_LSTREAM_IMPLEMENTATION(name,c_name,size) \
|
|
160 Lstream_implementation c_name[1] = \
|
|
161 { { (name), (size) } }
|
|
162
|
|
163 #define LSTREAM_FL_IS_OPEN 1
|
|
164 #define LSTREAM_FL_READ 2
|
|
165 #define LSTREAM_FL_WRITE 4
|
|
166 #define LSTREAM_FL_NO_PARTIAL_CHARS 8
|
|
167 #define LSTREAM_FL_CLOSE_AT_DISKSAVE 16
|
|
168
|
|
169 struct lstream
|
|
170 {
|
|
171 struct lcrecord_header header;
|
442
|
172 const Lstream_implementation *imp; /* methods for this stream */
|
428
|
173 Lstream_buffering buffering; /* type of buffering in use */
|
462
|
174 Lstream_data_count buffering_size; /* number of bytes buffered */
|
428
|
175
|
|
176 unsigned char *in_buffer; /* holds characters read from stream end */
|
462
|
177 Lstream_data_count in_buffer_size; /* allocated size of buffer */
|
|
178 Lstream_data_count in_buffer_current; /* number of characters in buffer */
|
|
179 Lstream_data_count in_buffer_ind; /* pointer to next character to
|
|
180 take from buffer */
|
428
|
181
|
|
182 unsigned char *out_buffer; /* holds characters to write to stream end */
|
462
|
183 Lstream_data_count out_buffer_size; /* allocated size of buffer */
|
|
184 Lstream_data_count out_buffer_ind; /* pointer to next buffer spot to
|
|
185 write a character */
|
428
|
186
|
|
187 /* The unget buffer is more or less a stack -- things get pushed
|
|
188 onto the end and read back from the end. Lstream_read()
|
|
189 basically reads backwards from the end to get stuff; Lstream_unread()
|
|
190 similarly has to push the data on backwards. */
|
|
191 unsigned char *unget_buffer; /* holds characters pushed back onto input */
|
462
|
192 Lstream_data_count unget_buffer_size; /* allocated size of buffer */
|
|
193 Lstream_data_count unget_buffer_ind; /* pointer to next buffer spot
|
|
194 to write a character */
|
428
|
195
|
462
|
196 Lstream_data_count byte_count;
|
456
|
197 int flags;
|
|
198 max_align_t data[1];
|
428
|
199 };
|
|
200
|
|
201 #define LSTREAM_TYPE_P(lstr, type) \
|
|
202 ((lstr)->imp == lstream_##type)
|
|
203
|
|
204 #ifdef ERROR_CHECK_TYPECHECK
|
442
|
205 INLINE_HEADER struct lstream *
|
428
|
206 error_check_lstream_type (struct lstream *stream,
|
442
|
207 const Lstream_implementation *imp);
|
|
208 INLINE_HEADER struct lstream *
|
428
|
209 error_check_lstream_type (struct lstream *stream,
|
442
|
210 const Lstream_implementation *imp)
|
428
|
211 {
|
|
212 assert (stream->imp == imp);
|
|
213 return stream;
|
|
214 }
|
|
215 # define LSTREAM_TYPE_DATA(lstr, type) \
|
|
216 ((struct type##_stream *) \
|
|
217 Lstream_data (error_check_lstream_type(lstr, lstream_##type)))
|
|
218 #else
|
|
219 # define LSTREAM_TYPE_DATA(lstr, type) \
|
|
220 ((struct type##_stream *) Lstream_data (lstr))
|
|
221 #endif
|
|
222
|
|
223 /* Declare that lstream-type TYPE has method M; used in
|
|
224 initialization routines */
|
|
225 #define LSTREAM_HAS_METHOD(type, m) \
|
|
226 (lstream_##type->m = type##_##m)
|
|
227
|
|
228
|
442
|
229 Lstream *Lstream_new (const Lstream_implementation *imp,
|
|
230 const char *mode);
|
428
|
231 void Lstream_reopen (Lstream *lstr);
|
|
232 void Lstream_set_buffering (Lstream *lstr, Lstream_buffering buffering,
|
|
233 int buffering_size);
|
|
234 int Lstream_flush (Lstream *lstr);
|
|
235 int Lstream_flush_out (Lstream *lstr);
|
|
236 int Lstream_fputc (Lstream *lstr, int c);
|
|
237 int Lstream_fgetc (Lstream *lstr);
|
|
238 void Lstream_fungetc (Lstream *lstr, int c);
|
462
|
239 Lstream_data_count Lstream_read (Lstream *lstr, void *data,
|
|
240 Lstream_data_count size);
|
|
241 Lstream_data_count Lstream_write (Lstream *lstr, const void *data,
|
|
242 Lstream_data_count size);
|
428
|
243 int Lstream_was_blocked_p (Lstream *lstr);
|
462
|
244 void Lstream_unread (Lstream *lstr, const void *data, Lstream_data_count size);
|
428
|
245 int Lstream_rewind (Lstream *lstr);
|
|
246 int Lstream_seekable_p (Lstream *lstr);
|
|
247 int Lstream_close (Lstream *lstr);
|
|
248 void Lstream_delete (Lstream *lstr);
|
|
249 void Lstream_set_character_mode (Lstream *str);
|
|
250
|
|
251 /* Call the function equivalent if the out buffer is full. Otherwise,
|
|
252 add to the end of the out buffer and, if line buffering is called for
|
|
253 and the character marks the end of a line, write out the buffer. */
|
|
254
|
|
255 #define Lstream_putc(stream, c) \
|
|
256 ((stream)->out_buffer_ind >= (stream)->out_buffer_size ? \
|
|
257 Lstream_fputc (stream, c) : \
|
|
258 ((stream)->out_buffer[(stream)->out_buffer_ind++] = \
|
|
259 (unsigned char) (c), \
|
|
260 (stream)->byte_count++, \
|
|
261 (stream)->buffering == LSTREAM_LINE_BUFFERED && \
|
|
262 (stream)->out_buffer[(stream)->out_buffer_ind - 1] == '\n' ? \
|
|
263 Lstream_flush_out (stream) : 0))
|
|
264
|
|
265 /* Retrieve from unget buffer if there are any characters there;
|
|
266 else retrieve from in buffer if there's anything there;
|
|
267 else call the function equivalent */
|
|
268 #define Lstream_getc(stream) \
|
|
269 ((stream)->unget_buffer_ind > 0 ? \
|
|
270 ((stream)->byte_count++, \
|
|
271 (stream)->unget_buffer[--(stream)->unget_buffer_ind]) : \
|
|
272 (stream)->in_buffer_ind < (stream)->in_buffer_current ? \
|
|
273 ((stream)->byte_count++, \
|
|
274 (stream)->in_buffer[(stream)->in_buffer_ind++]) : \
|
|
275 Lstream_fgetc (stream))
|
|
276
|
|
277 /* Add to the end if it won't overflow buffer; otherwise call the
|
|
278 function equivalent */
|
|
279 #define Lstream_ungetc(stream, c) \
|
|
280 ((stream)->unget_buffer_ind >= (stream)->unget_buffer_size ? \
|
|
281 Lstream_fungetc (stream, c) : \
|
|
282 (void) ((stream)->byte_count--, \
|
|
283 ((stream)->unget_buffer[(stream)->unget_buffer_ind++] = \
|
|
284 (unsigned char) (c))))
|
|
285
|
|
286 #define Lstream_data(stream) ((void *) ((stream)->data))
|
|
287 #define Lstream_byte_count(stream) ((stream)->byte_count)
|
|
288
|
|
289
|
|
290 /************************************************************************/
|
|
291 /* working with an Lstream as a stream of Emchars */
|
|
292 /************************************************************************/
|
|
293
|
|
294 #ifdef MULE
|
|
295
|
|
296 #ifndef BYTE_ASCII_P
|
|
297 #include "mule-charset.h"
|
|
298 #endif
|
|
299
|
442
|
300 INLINE_HEADER Emchar Lstream_get_emchar (Lstream *stream);
|
|
301 INLINE_HEADER Emchar
|
428
|
302 Lstream_get_emchar (Lstream *stream)
|
|
303 {
|
|
304 int c = Lstream_getc (stream);
|
444
|
305 return (c < 0x80 /* c == EOF || BYTE_ASCII_P (c) */
|
|
306 ? (Emchar) c
|
|
307 : Lstream_get_emchar_1 (stream, c));
|
428
|
308 }
|
|
309
|
442
|
310 INLINE_HEADER int Lstream_put_emchar (Lstream *stream, Emchar ch);
|
|
311 INLINE_HEADER int
|
428
|
312 Lstream_put_emchar (Lstream *stream, Emchar ch)
|
|
313 {
|
|
314 return CHAR_ASCII_P (ch) ?
|
|
315 Lstream_putc (stream, ch) :
|
|
316 Lstream_fput_emchar (stream, ch);
|
|
317 }
|
|
318
|
442
|
319 INLINE_HEADER void Lstream_unget_emchar (Lstream *stream, Emchar ch);
|
|
320 INLINE_HEADER void
|
428
|
321 Lstream_unget_emchar (Lstream *stream, Emchar ch)
|
|
322 {
|
|
323 if (CHAR_ASCII_P (ch))
|
|
324 Lstream_ungetc (stream, ch);
|
|
325 else
|
|
326 Lstream_funget_emchar (stream, ch);
|
|
327 }
|
|
328 #else /* not MULE */
|
|
329
|
|
330 # define Lstream_get_emchar(stream) Lstream_getc (stream)
|
|
331 # define Lstream_put_emchar(stream, ch) Lstream_putc (stream, ch)
|
|
332 # define Lstream_unget_emchar(stream, ch) Lstream_ungetc (stream, ch)
|
|
333
|
|
334 #endif /* not MULE */
|
|
335
|
|
336
|
|
337 /************************************************************************/
|
|
338 /* Lstream implementations */
|
|
339 /************************************************************************/
|
|
340
|
|
341 /* Flags we can pass to the filedesc and stdio streams. */
|
|
342
|
|
343 /* If set, close the descriptor or FILE * when the stream is closed. */
|
|
344 #define LSTR_CLOSING 1
|
|
345
|
|
346 /* If set, allow quitting out of the actual I/O. */
|
|
347 #define LSTR_ALLOW_QUIT 2
|
|
348
|
|
349 /* If set and filedesc_stream_set_pty_flushing() has been called
|
|
350 on the stream, do not send more than pty_max_bytes on a single
|
|
351 line without flushing the data out using the eof_char. */
|
|
352 #define LSTR_PTY_FLUSHING 4
|
|
353
|
|
354 /* If set, an EWOULDBLOCK error is not treated as an error but
|
|
355 simply causes the write function to return 0 as the number
|
|
356 of bytes written out. */
|
|
357 #define LSTR_BLOCKED_OK 8
|
|
358
|
|
359 Lisp_Object make_stdio_input_stream (FILE *stream, int flags);
|
|
360 Lisp_Object make_stdio_output_stream (FILE *stream, int flags);
|
|
361 Lisp_Object make_filedesc_input_stream (int filedesc, int offset, int count,
|
|
362 int flags);
|
|
363 Lisp_Object make_filedesc_output_stream (int filedesc, int offset, int count,
|
|
364 int flags);
|
|
365 void filedesc_stream_set_pty_flushing (Lstream *stream,
|
|
366 int pty_max_bytes,
|
|
367 Bufbyte eof_char);
|
|
368 int filedesc_stream_fd (Lstream *stream);
|
|
369 Lisp_Object make_lisp_string_input_stream (Lisp_Object string,
|
|
370 Bytecount offset,
|
|
371 Bytecount len);
|
462
|
372 Lisp_Object make_fixed_buffer_input_stream (const void *buf,
|
|
373 Lstream_data_count size);
|
|
374 Lisp_Object make_fixed_buffer_output_stream (void *buf,
|
|
375 Lstream_data_count size);
|
442
|
376 const unsigned char *fixed_buffer_input_stream_ptr (Lstream *stream);
|
428
|
377 unsigned char *fixed_buffer_output_stream_ptr (Lstream *stream);
|
|
378 Lisp_Object make_resizing_buffer_output_stream (void);
|
|
379 unsigned char *resizing_buffer_stream_ptr (Lstream *stream);
|
|
380 Lisp_Object make_dynarr_output_stream (unsigned_char_dynarr *dyn);
|
|
381 #define LSTR_SELECTIVE 1
|
|
382 #define LSTR_IGNORE_ACCESSIBLE 2
|
|
383 Lisp_Object make_lisp_buffer_input_stream (struct buffer *buf, Bufpos start,
|
|
384 Bufpos end, int flags);
|
|
385 Lisp_Object make_lisp_buffer_output_stream (struct buffer *buf, Bufpos pos,
|
|
386 int flags);
|
|
387 Bufpos lisp_buffer_stream_startpos (Lstream *stream);
|
|
388
|
440
|
389 #endif /* INCLUDED_lstream_h_ */
|