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