Mercurial > hg > xemacs-beta
annotate src/lstream.c @ 5776:65d65b52d608
Pass character count from coding systems to buffer insertion code.
src/ChangeLog addition:
2014-01-16 Aidan Kehoe <kehoea@parhasard.net>
Pass character count information from the no-conversion and
unicode coding systems to the buffer insertion code, making
#'find-file on large buffers a little snappier (if
ERROR_CHECK_TEXT is not defined).
* file-coding.c:
* file-coding.c (coding_character_tell): New.
* file-coding.c (conversion_coding_stream_description): New.
* file-coding.c (no_conversion_convert):
Update characters_seen when decoding.
* file-coding.c (no_conversion_character_tell): New.
* file-coding.c (lstream_type_create_file_coding): Create the
no_conversion type with data.
* file-coding.c (coding_system_type_create):
Make the character_tell method available here.
* file-coding.h:
* file-coding.h (struct coding_system_methods):
Add a new character_tell() method, passing charcount information
from the coding systems to the buffer code, avoiding duplicate
bytecount-to-charcount work especially with large buffers.
* fileio.c (Finsert_file_contents_internal):
Update this to pass charcount information to
buffer_insert_string_1(), if that is available from the lstream code.
* insdel.c:
* insdel.c (buffer_insert_string_1):
Add a new CCLEN argument, giving the character count of the string
to insert. It can be -1 to indicate that te function should work
it out itself using bytecount_to_charcount(), as it used to.
* insdel.c (buffer_insert_raw_string_1):
* insdel.c (buffer_insert_lisp_string_1):
* insdel.c (buffer_insert_ascstring_1):
* insdel.c (buffer_insert_emacs_char_1):
* insdel.c (buffer_insert_from_buffer_1):
* insdel.c (buffer_replace_char):
Update these functions to use the new calling convention.
* insdel.h:
* insdel.h (buffer_insert_string):
Update this header to reflect the new buffer_insert_string_1()
argument.
* lstream.c (Lstream_character_tell): New.
Return the number of characters *read* and seen by the consumer so
far, taking into account the unget buffer, and buffered reading.
* lstream.c (Lstream_unread):
Update unget_character_count here as appropriate.
* lstream.c (Lstream_rewind):
Reset unget_character_count here too.
* lstream.h:
* lstream.h (struct lstream):
Provide the character_tell method, add a new field,
unget_character_count, giving the number of characters ever passed
to Lstream_unread().
Declare Lstream_character_tell().
Make Lstream_ungetc(), which happens to be unused, an inline
function rather than a macro, in the course of updating it to
modify unget_character_count.
* print.c (output_string):
Use the new argument to buffer_insert_string_1().
* tests.c:
* tests.c (Ftest_character_tell):
New test function.
* tests.c (syms_of_tests):
Make it available.
* unicode.c:
* unicode.c (struct unicode_coding_stream):
* unicode.c (unicode_character_tell):
New method.
* unicode.c (unicode_convert):
Update the character counter as appropriate.
* unicode.c (coding_system_type_create_unicode):
Make the character_tell method available.
author | Aidan Kehoe <kehoea@parhasard.net> |
---|---|
date | Thu, 16 Jan 2014 16:27:52 +0000 |
parents | 2dbefd79b3d3 |
children | 0cb4f494a548 |
rev | line source |
---|---|
428 | 1 /* Generic stream implementation. |
2 Copyright (C) 1995 Free Software Foundation, Inc. | |
3 Copyright (C) 1995 Sun Microsystems, Inc. | |
5124
623d57b7fbe8
separate regular and disksave finalization, print method fixes.
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
4 Copyright (C) 1996, 2001, 2002, 2010 Ben Wing. |
428 | 5 |
6 This file is part of XEmacs. | |
7 | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5157
diff
changeset
|
8 XEmacs is free software: you can redistribute it and/or modify it |
428 | 9 under the terms of the GNU General Public License as published by the |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5157
diff
changeset
|
10 Free Software Foundation, either version 3 of the License, or (at your |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5157
diff
changeset
|
11 option) any later version. |
428 | 12 |
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5157
diff
changeset
|
19 along with XEmacs. If not, see <http://www.gnu.org/licenses/>. */ |
428 | 20 |
21 /* Synched up with: Not in FSF. */ | |
22 | |
23 /* Written by Ben Wing. */ | |
24 | |
25 #include <config.h> | |
26 #include "lisp.h" | |
27 | |
28 #include "buffer.h" | |
29 #include "insdel.h" | |
30 #include "lstream.h" | |
31 | |
32 #include "sysfile.h" | |
33 | |
771 | 34 /* This module provides a generic buffering stream implementation. |
428 | 35 Conceptually, you send data to the stream or read data from the |
36 stream, not caring what's on the other end of the stream. The | |
37 other end could be another stream, a file descriptor, a stdio | |
38 stream, a fixed block of memory, a reallocating block of memory, | |
39 etc. The main purpose of the stream is to provide a standard | |
40 interface and to do buffering. Macros are defined to read | |
41 or write characters, so the calling functions do not have to | |
42 worry about blocking data together in order to achieve efficiency. | |
43 | |
771 | 44 Note that this object is called "stream" in Lisp but "lstream" |
428 | 45 in C. The reason for this is that "stream" is too generic a name |
46 for C; too much likelihood of conflict/confusion with C++, etc. */ | |
47 | |
48 #define DEFAULT_BLOCK_BUFFERING_SIZE 512 | |
49 #define MAX_READ_SIZE 512 | |
50 | |
51 static Lisp_Object | |
52 mark_lstream (Lisp_Object obj) | |
53 { | |
54 Lstream *lstr = XLSTREAM (obj); | |
55 return lstr->imp->marker ? (lstr->imp->marker) (obj) : Qnil; | |
56 } | |
57 | |
58 static void | |
2286 | 59 print_lstream (Lisp_Object obj, Lisp_Object printcharfun, |
60 int UNUSED (escapeflag)) | |
428 | 61 { |
62 Lstream *lstr = XLSTREAM (obj); | |
63 | |
800 | 64 write_fmt_string (printcharfun, |
5146
88bd4f3ef8e4
make lrecord UID's have a separate UID space for each object, resurrect debug SOE code in extents.c
Ben Wing <ben@xemacs.org>
parents:
5127
diff
changeset
|
65 "#<INTERNAL OBJECT (XEmacs bug?) (%s lstream) 0x%x>", |
88bd4f3ef8e4
make lrecord UID's have a separate UID space for each object, resurrect debug SOE code in extents.c
Ben Wing <ben@xemacs.org>
parents:
5127
diff
changeset
|
66 lstr->imp->name, LISP_OBJECT_UID (obj)); |
428 | 67 } |
68 | |
69 static void | |
5127
a9c41067dd88
more cleanups, terminology clarification, lots of doc work
Ben Wing <ben@xemacs.org>
parents:
5126
diff
changeset
|
70 finalize_lstream (Lisp_Object obj) |
428 | 71 { |
72 /* WARNING WARNING WARNING. This function (and all finalize functions) | |
5124
623d57b7fbe8
separate regular and disksave finalization, print method fixes.
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
73 may get called more than once on the same object. */ |
5127
a9c41067dd88
more cleanups, terminology clarification, lots of doc work
Ben Wing <ben@xemacs.org>
parents:
5126
diff
changeset
|
74 Lstream *lstr = XLSTREAM (obj); |
428 | 75 |
5124
623d57b7fbe8
separate regular and disksave finalization, print method fixes.
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
76 if (lstr->flags & LSTREAM_FL_IS_OPEN) |
623d57b7fbe8
separate regular and disksave finalization, print method fixes.
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
77 Lstream_close (lstr); |
623d57b7fbe8
separate regular and disksave finalization, print method fixes.
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
78 |
623d57b7fbe8
separate regular and disksave finalization, print method fixes.
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
79 if (lstr->imp->finalizer) |
623d57b7fbe8
separate regular and disksave finalization, print method fixes.
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
80 (lstr->imp->finalizer) (lstr); |
623d57b7fbe8
separate regular and disksave finalization, print method fixes.
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
81 } |
623d57b7fbe8
separate regular and disksave finalization, print method fixes.
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
82 |
623d57b7fbe8
separate regular and disksave finalization, print method fixes.
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
83 static void |
623d57b7fbe8
separate regular and disksave finalization, print method fixes.
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
84 disksave_lstream (Lisp_Object lstream) |
623d57b7fbe8
separate regular and disksave finalization, print method fixes.
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
85 { |
623d57b7fbe8
separate regular and disksave finalization, print method fixes.
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
86 Lstream *lstr = XLSTREAM (lstream); |
623d57b7fbe8
separate regular and disksave finalization, print method fixes.
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
87 |
428 | 88 #if 0 /* this may cause weird Broken Pipes? */ |
5124
623d57b7fbe8
separate regular and disksave finalization, print method fixes.
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
89 Lstream_pseudo_close (lstr); |
623d57b7fbe8
separate regular and disksave finalization, print method fixes.
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
90 return; |
428 | 91 #endif |
5124
623d57b7fbe8
separate regular and disksave finalization, print method fixes.
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
92 if ((lstr->flags & LSTREAM_FL_IS_OPEN) && |
623d57b7fbe8
separate regular and disksave finalization, print method fixes.
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
93 (lstr->flags & LSTREAM_FL_CLOSE_AT_DISKSAVE)) |
623d57b7fbe8
separate regular and disksave finalization, print method fixes.
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
94 Lstream_close (lstr); |
428 | 95 } |
96 | |
665 | 97 inline static Bytecount |
98 aligned_sizeof_lstream (Bytecount lstream_type_specific_size) | |
456 | 99 { |
826 | 100 return MAX_ALIGN_SIZE (offsetof (Lstream, data) + |
101 lstream_type_specific_size); | |
456 | 102 } |
103 | |
665 | 104 static Bytecount |
5127
a9c41067dd88
more cleanups, terminology clarification, lots of doc work
Ben Wing <ben@xemacs.org>
parents:
5126
diff
changeset
|
105 sizeof_lstream (Lisp_Object obj) |
428 | 106 { |
5127
a9c41067dd88
more cleanups, terminology clarification, lots of doc work
Ben Wing <ben@xemacs.org>
parents:
5126
diff
changeset
|
107 return aligned_sizeof_lstream (XLSTREAM (obj)->imp->size); |
428 | 108 } |
109 | |
1204 | 110 static const struct memory_description lstream_implementation_description_1[] |
111 = { | |
112 { XD_END } | |
113 }; | |
114 | |
115 const struct sized_memory_description lstream_implementation_description = { | |
116 sizeof (struct lstream_implementation), | |
117 lstream_implementation_description_1 | |
118 }; | |
119 | |
120 static const struct sized_memory_description lstream_extra_description_map[] = | |
121 { | |
122 { offsetof (Lstream, imp) }, | |
123 { offsetof (struct lstream_implementation, extra_description) }, | |
124 { -1 }, | |
125 }; | |
126 | |
127 static const struct memory_description lstream_description[] = | |
128 { | |
2367 | 129 { XD_BLOCK_PTR, offsetof (Lstream, imp), 1, |
2551 | 130 { &lstream_implementation_description } }, |
2367 | 131 { XD_BLOCK_ARRAY, offsetof (Lstream, data), 1, |
2551 | 132 { lstream_extra_description_map } }, |
1204 | 133 { XD_END } |
134 }; | |
135 | |
136 static const struct memory_description lstream_empty_extra_description_1[] = | |
137 { | |
138 { XD_END } | |
139 }; | |
140 | |
141 const struct sized_memory_description lstream_empty_extra_description = { | |
142 0, lstream_empty_extra_description_1 | |
143 }; | |
144 | |
5157
1fae11d56ad2
redo memory-usage mechanism, add way of dynamically initializing Lisp objects
Ben Wing <ben@xemacs.org>
parents:
5146
diff
changeset
|
145 DEFINE_NODUMP_SIZABLE_LISP_OBJECT ("stream", lstream, |
1fae11d56ad2
redo memory-usage mechanism, add way of dynamically initializing Lisp objects
Ben Wing <ben@xemacs.org>
parents:
5146
diff
changeset
|
146 mark_lstream, print_lstream, |
1fae11d56ad2
redo memory-usage mechanism, add way of dynamically initializing Lisp objects
Ben Wing <ben@xemacs.org>
parents:
5146
diff
changeset
|
147 finalize_lstream, |
1fae11d56ad2
redo memory-usage mechanism, add way of dynamically initializing Lisp objects
Ben Wing <ben@xemacs.org>
parents:
5146
diff
changeset
|
148 0, 0, /* no equal or hash */ |
1fae11d56ad2
redo memory-usage mechanism, add way of dynamically initializing Lisp objects
Ben Wing <ben@xemacs.org>
parents:
5146
diff
changeset
|
149 lstream_description, |
1fae11d56ad2
redo memory-usage mechanism, add way of dynamically initializing Lisp objects
Ben Wing <ben@xemacs.org>
parents:
5146
diff
changeset
|
150 sizeof_lstream, Lstream); |
428 | 151 |
771 | 152 |
153 /* Change the buffering of a stream. See lstream.h. By default the | |
154 buffering is STREAM_BLOCK_BUFFERED. */ | |
155 | |
428 | 156 void |
157 Lstream_set_buffering (Lstream *lstr, Lstream_buffering buffering, | |
158 int buffering_size) | |
159 { | |
160 lstr->buffering = buffering; | |
161 switch (buffering) | |
162 { | |
163 case LSTREAM_UNBUFFERED: | |
164 lstr->buffering_size = 0; break; | |
165 case LSTREAM_BLOCK_BUFFERED: | |
166 lstr->buffering_size = DEFAULT_BLOCK_BUFFERING_SIZE; break; | |
167 case LSTREAM_BLOCKN_BUFFERED: | |
168 lstr->buffering_size = buffering_size; break; | |
169 case LSTREAM_LINE_BUFFERED: | |
170 case LSTREAM_UNLIMITED: | |
171 lstr->buffering_size = INT_MAX; break; | |
172 } | |
173 } | |
174 | |
3263 | 175 #ifndef NEW_GC |
442 | 176 static const Lstream_implementation *lstream_types[32]; |
428 | 177 static Lisp_Object Vlstream_free_list[32]; |
178 static int lstream_type_count; | |
3263 | 179 #endif /* not NEW_GC */ |
428 | 180 |
771 | 181 /* Allocate and return a new Lstream. This function is not really |
182 meant to be called directly; rather, each stream type should | |
183 provide its own stream creation function, which creates the stream | |
184 and does any other necessary creation stuff (e.g. opening a | |
185 file). */ | |
186 | |
428 | 187 Lstream * |
442 | 188 Lstream_new (const Lstream_implementation *imp, const char *mode) |
428 | 189 { |
190 Lstream *p; | |
3263 | 191 #ifdef NEW_GC |
5127
a9c41067dd88
more cleanups, terminology clarification, lots of doc work
Ben Wing <ben@xemacs.org>
parents:
5126
diff
changeset
|
192 p = XLSTREAM (ALLOC_SIZED_LISP_OBJECT (aligned_sizeof_lstream (imp->size), |
a9c41067dd88
more cleanups, terminology clarification, lots of doc work
Ben Wing <ben@xemacs.org>
parents:
5126
diff
changeset
|
193 lstream)); |
3263 | 194 #else /* not NEW_GC */ |
428 | 195 int i; |
196 | |
197 for (i = 0; i < lstream_type_count; i++) | |
198 { | |
199 if (lstream_types[i] == imp) | |
200 break; | |
201 } | |
202 | |
203 if (i == lstream_type_count) | |
204 { | |
205 assert (lstream_type_count < countof (lstream_types)); | |
206 lstream_types[lstream_type_count] = imp; | |
207 Vlstream_free_list[lstream_type_count] = | |
456 | 208 make_lcrecord_list (aligned_sizeof_lstream (imp->size), |
428 | 209 &lrecord_lstream); |
210 lstream_type_count++; | |
211 } | |
212 | |
1204 | 213 p = XLSTREAM (alloc_managed_lcrecord (Vlstream_free_list[i])); |
3263 | 214 #endif /* not NEW_GC */ |
5127
a9c41067dd88
more cleanups, terminology clarification, lots of doc work
Ben Wing <ben@xemacs.org>
parents:
5126
diff
changeset
|
215 /* Formerly, we zeroed out the object minus its header, but it's now |
a9c41067dd88
more cleanups, terminology clarification, lots of doc work
Ben Wing <ben@xemacs.org>
parents:
5126
diff
changeset
|
216 handled automatically. ALLOC_SIZED_LISP_OBJECT() always zeroes out |
a9c41067dd88
more cleanups, terminology clarification, lots of doc work
Ben Wing <ben@xemacs.org>
parents:
5126
diff
changeset
|
217 the whole object other than its header, and alloc_managed_lcrecord() |
a9c41067dd88
more cleanups, terminology clarification, lots of doc work
Ben Wing <ben@xemacs.org>
parents:
5126
diff
changeset
|
218 does the same. */ |
428 | 219 p->imp = imp; |
220 Lstream_set_buffering (p, LSTREAM_BLOCK_BUFFERED, 0); | |
221 p->flags = LSTREAM_FL_IS_OPEN; | |
222 | |
223 /* convert mode (one of "r", "w", "rc", "wc") to p->flags */ | |
224 assert (mode[0] == 'r' || mode[0] == 'w'); | |
225 assert (mode[1] == 'c' || mode[1] == '\0'); | |
226 p->flags |= (mode[0] == 'r' ? LSTREAM_FL_READ : LSTREAM_FL_WRITE); | |
227 if (mode[1] == 'c') | |
228 p->flags |= LSTREAM_FL_NO_PARTIAL_CHARS; | |
229 | |
230 return p; | |
231 } | |
232 | |
771 | 233 /* Set or unset "character mode" on the stream. The basic idea is that, |
234 assuming valid internal-format data is passing through the stream and | |
235 we're processing the data character by character, we don't want partial | |
236 characters at the end of the data. (No partial characters at the | |
237 beginning happens naturally if we eliminate partial characters at the | |
238 end and the stream is implemented correctly.) | |
239 | |
240 Character mode actually has two somewhat different meanings, depending | |
241 on whether this is a read stream or write stream. If a read stream, | |
242 character mode means that data returned from calling Lstream_read() on | |
243 the stream will contain only full characters. If a write stream, | |
244 character mode means that data passed to the write method in the stream | |
245 implementation will contain only full characters. It's important to | |
246 note the non-parallelism in who should set this mode on the stream: The | |
247 *CALLER* sets character mode on read streams it creates; the *STREAM | |
248 ITSELF* sets character mode on write streams, typically at creation | |
814 | 249 time. |
250 | |
251 (However, if a read stream always generates internal-format data, then | |
252 the callers will almost always want character mode, and it's allowed to | |
253 set this on behalf of the caller, as long as a flag can be provided at | |
254 creation time to disable this behavior.) */ | |
771 | 255 |
428 | 256 void |
257 Lstream_set_character_mode (Lstream *lstr) | |
258 { | |
259 lstr->flags |= LSTREAM_FL_NO_PARTIAL_CHARS; | |
260 } | |
261 | |
771 | 262 /* Unset character mode. See Lstream_set_character_mode(). */ |
263 | |
264 void | |
265 Lstream_unset_character_mode (Lstream *lstr) | |
266 { | |
267 lstr->flags &= ~LSTREAM_FL_NO_PARTIAL_CHARS; | |
268 } | |
269 | |
270 /* Close the stream (if it's open), and free all memory associated with the | |
271 stream. Put the stream on a free list; later calls to create a new | |
272 stream of this type may reuse this stream. Calling this is not strictly | |
273 necessary, but it is much more efficient than having the Lstream be | |
274 garbage-collected. Be VERY VERY SURE there are no pointers to this | |
275 object hanging around anywhere where they might be used! When streams | |
276 are chained together, be VERY CAREFUL of the order in which you delete | |
277 them! (e.g. if the streams are in a singly-linked list, delete the head | |
814 | 278 first; this will close (but check the documentation, e.g. of |
279 make_coding_input_stream()), and may send data down to the rest. Then | |
771 | 280 proceed to the rest, one by one. If the chains are in a doubly-linked |
281 list, close all the streams first (again, from the head to the tail), | |
282 disconnect the back links, then delete starting from the head. In | |
814 | 283 general, it's a good idea to close everything before deleting anything. |
771 | 284 |
285 NOTE: DO NOT CALL DURING GARBAGE COLLECTION (e.g. in a finalizer). You | |
286 will be aborted. See free_managed_lcrecord(). */ | |
287 | |
428 | 288 void |
289 Lstream_delete (Lstream *lstr) | |
290 { | |
3263 | 291 #ifndef NEW_GC |
428 | 292 int i; |
3263 | 293 #endif /* not NEW_GC */ |
793 | 294 Lisp_Object val = wrap_lstream (lstr); |
428 | 295 |
3263 | 296 #ifdef NEW_GC |
5127
a9c41067dd88
more cleanups, terminology clarification, lots of doc work
Ben Wing <ben@xemacs.org>
parents:
5126
diff
changeset
|
297 free_normal_lisp_object (val); |
3263 | 298 #else /* not NEW_GC */ |
428 | 299 for (i = 0; i < lstream_type_count; i++) |
300 { | |
301 if (lstream_types[i] == lstr->imp) | |
302 { | |
303 free_managed_lcrecord (Vlstream_free_list[i], val); | |
304 return; | |
305 } | |
306 } | |
307 | |
2500 | 308 ABORT (); |
3263 | 309 #endif /* not NEW_GC */ |
428 | 310 } |
311 | |
312 #define Lstream_internal_error(reason, lstr) \ | |
563 | 313 signal_error (Qinternal_error, reason, wrap_lstream (lstr)) |
428 | 314 |
771 | 315 /* Reopen a closed stream. This enables I/O on it again. This is not |
316 meant to be called except from a wrapper routine that reinitializes | |
317 variables and such -- the close routine may well have freed some | |
318 necessary storage structures, for example. */ | |
319 | |
428 | 320 void |
321 Lstream_reopen (Lstream *lstr) | |
322 { | |
323 if (lstr->flags & LSTREAM_FL_IS_OPEN) | |
324 Lstream_internal_error ("lstream already open", lstr); | |
325 lstr->flags |= LSTREAM_FL_IS_OPEN; | |
326 } | |
327 | |
771 | 328 /* Try to write as much of DATA as possible to the stream. Return the |
329 number of bytes written. */ | |
428 | 330 |
771 | 331 static int |
332 Lstream_really_write (Lstream *lstr, const unsigned char *data, int size) | |
428 | 333 { |
665 | 334 Bytecount num_written; |
771 | 335 const unsigned char *orig_data = data; |
336 int error_occurred = 0; | |
428 | 337 |
771 | 338 while (size > 0) |
428 | 339 { |
340 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) | |
341 Lstream_internal_error ("lstream not open", lstr); | |
342 if (! (lstr->flags & LSTREAM_FL_WRITE)) | |
343 Lstream_internal_error ("lstream not open for writing", lstr); | |
344 if (!lstr->imp->writer) | |
345 Lstream_internal_error ("lstream has no writer", lstr); | |
346 | |
347 if (lstr->flags & LSTREAM_FL_NO_PARTIAL_CHARS) | |
348 /* It's quite possible for us to get passed an incomplete | |
349 character at the end. We need to spit back that | |
350 incomplete character. */ | |
351 { | |
442 | 352 const unsigned char *dataend = data + size - 1; |
428 | 353 assert (size > 0); /* safety check ... */ |
354 /* Optimize the most common case. */ | |
826 | 355 if (!byte_ascii_p (*dataend)) |
428 | 356 { |
357 /* Go back to the beginning of the last (and possibly partial) | |
358 character, and bump forward to see if the character is | |
359 complete. */ | |
867 | 360 VALIDATE_IBYTEPTR_BACKWARD (dataend); |
826 | 361 if (dataend + rep_bytes_by_first_byte (*dataend) != data + size) |
428 | 362 /* If not, chop the size down to ignore the last char |
363 and stash it away for next time. */ | |
364 size = dataend - data; | |
365 /* If we don't even have one character to write, then just | |
366 skip out. */ | |
367 if (size == 0) | |
368 break; | |
369 } | |
370 } | |
371 | |
771 | 372 num_written = (lstr->imp->writer) (lstr, data, size); |
428 | 373 if (num_written == 0) |
374 /* If nothing got written, then just hold the data. This may | |
375 occur, for example, if this stream does non-blocking I/O; | |
376 the attempt to write the data might have resulted in an | |
377 EWOULDBLOCK error. */ | |
771 | 378 break; |
379 else if (num_written > size) | |
2500 | 380 ABORT (); |
428 | 381 else if (num_written > 0) |
382 { | |
771 | 383 data += num_written; |
384 size -= num_written; | |
428 | 385 } |
386 else | |
771 | 387 { |
388 /* If error, just hold the data, for similar reasons as above. */ | |
389 error_occurred = 1; | |
390 break; | |
391 } | |
428 | 392 } |
393 | |
2383 | 394 if (!error_occurred && lstr->imp->flusher) |
771 | 395 error_occurred = (lstr->imp->flusher) (lstr) < 0; |
396 | |
397 if (data == orig_data && error_occurred) | |
398 return -1; | |
399 | |
400 return data - orig_data; | |
401 } | |
402 | |
403 /* Attempt to flush out all of the buffered data for writing. Leaves | |
404 whatever wasn't flushed sitting in the stream's buffers. Return -1 if | |
405 nothing written and error occurred, 0 otherwise. */ | |
428 | 406 |
771 | 407 int |
408 Lstream_flush_out (Lstream *lstr) | |
409 { | |
410 Bytecount num_written = | |
411 Lstream_really_write (lstr, lstr->out_buffer, lstr->out_buffer_ind); | |
412 if (num_written == lstr->out_buffer_ind) | |
413 { | |
414 lstr->out_buffer_ind = 0; | |
415 return 0; | |
416 } | |
417 else if (num_written > 0) | |
418 { | |
419 memmove (lstr->out_buffer, lstr->out_buffer + num_written, | |
420 lstr->out_buffer_ind - num_written); | |
421 lstr->out_buffer_ind -= num_written; | |
422 return 0; | |
423 } | |
424 else return num_written; | |
428 | 425 } |
426 | |
771 | 427 /* Flush out any pending unwritten data in the stream. Clear any buffered |
428 input data. This differs from Lstream_flush_out() in that it also | |
429 clears any unflushable buffered data. Returns 0 on success, -1 on | |
430 error. */ | |
431 | |
428 | 432 int |
433 Lstream_flush (Lstream *lstr) | |
434 { | |
435 if (Lstream_flush_out (lstr) < 0) | |
436 return -1; | |
437 | |
438 /* clear out buffered data */ | |
439 lstr->in_buffer_current = lstr->in_buffer_ind = 0; | |
440 lstr->unget_buffer_ind = 0; | |
441 | |
442 return 0; | |
443 } | |
444 | |
445 /* We want to add NUM characters. This function ensures that the | |
446 buffer is large enough for this (per the buffering size specified | |
447 in the stream) and returns the number of characters we can | |
448 actually write. If FORCE is set, ignore the buffering size | |
449 and go ahead and make space for all the chars even if it exceeds | |
450 the buffering size. (This is used to deal with the possibility | |
451 that the stream writer might refuse to write any bytes now, e.g. | |
452 if it's getting EWOULDBLOCK errors. We have to keep stocking them | |
771 | 453 up until they can be written, so as to avoid losing data.) */ |
428 | 454 |
665 | 455 static Bytecount |
456 Lstream_adding (Lstream *lstr, Bytecount num, int force) | |
428 | 457 { |
665 | 458 Bytecount size = num + lstr->out_buffer_ind; |
430 | 459 |
460 if (size <= lstr->out_buffer_size) | |
461 return num; | |
462 | |
428 | 463 /* Maybe chop it down so that we don't buffer more characters |
464 than our advertised buffering size. */ | |
430 | 465 if ((size > lstr->buffering_size) && !force) |
466 { | |
467 size = lstr->buffering_size; | |
468 /* There might be more data buffered than the buffering size. */ | |
469 if (size <= lstr->out_buffer_ind) | |
470 return 0; | |
471 } | |
472 | |
473 DO_REALLOC (lstr->out_buffer, lstr->out_buffer_size, size, unsigned char); | |
474 | |
475 return size - lstr->out_buffer_ind; | |
428 | 476 } |
477 | |
478 /* Like Lstream_write(), but does not handle line-buffering correctly. */ | |
479 | |
771 | 480 static int |
665 | 481 Lstream_write_1 (Lstream *lstr, const void *data, Bytecount size) |
428 | 482 { |
442 | 483 const unsigned char *p = (const unsigned char *) data; |
665 | 484 Bytecount off = 0; |
428 | 485 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) |
486 Lstream_internal_error ("lstream not open", lstr); | |
487 if (! (lstr->flags & LSTREAM_FL_WRITE)) | |
488 Lstream_internal_error ("lstream not open for writing", lstr); | |
771 | 489 |
490 if (lstr->buffering == LSTREAM_UNBUFFERED) | |
491 { | |
492 /* If there is buffered data, it means we ran into blocking | |
493 errors the previous time and had to buffer our remaining | |
494 data. Try to write it now. */ | |
495 if (lstr->out_buffer_ind > 0) | |
496 { | |
497 if (Lstream_flush_out (lstr) < 0) | |
498 return -1; | |
499 } | |
500 | |
501 /* If not still blocked, try to write the new data */ | |
502 if (lstr->out_buffer_ind == 0) | |
503 { | |
504 /* we don't need to loop because Lstream_really_write does that | |
505 for us. */ | |
506 Bytecount num_written = Lstream_really_write (lstr, p, size); | |
507 if (num_written < 0) | |
508 return -1; | |
509 off += num_written; | |
510 } | |
511 | |
512 /* squirrel away the rest of the data */ | |
513 if (off < size) | |
514 { | |
515 Lstream_adding (lstr, size - off, 1); | |
516 memcpy (lstr->out_buffer + lstr->out_buffer_ind, p + off, | |
517 size - off); | |
518 lstr->out_buffer_ind += size - off; | |
519 } | |
520 | |
521 lstr->byte_count += size; | |
522 return 0; | |
523 } | |
524 else | |
525 { | |
526 int couldnt_write_last_time = 0; | |
428 | 527 |
771 | 528 while (1) |
529 { | |
530 /* Figure out how much we can add to the buffer */ | |
531 Bytecount chunk = Lstream_adding (lstr, size, 0); | |
532 if (chunk == 0) | |
533 { | |
534 if (couldnt_write_last_time) | |
535 /* Ung, we ran out of space and tried to flush | |
536 the buffer, but it didn't work because the stream | |
537 writer is refusing to accept any data. So we | |
538 just have to squirrel away all the rest of the | |
539 stuff. */ | |
540 chunk = Lstream_adding (lstr, size, 1); | |
541 else | |
542 couldnt_write_last_time = 1; | |
543 } | |
544 /* Do it. */ | |
545 if (chunk > 0) | |
546 { | |
547 memcpy (lstr->out_buffer + lstr->out_buffer_ind, p + off, chunk); | |
548 lstr->out_buffer_ind += chunk; | |
549 lstr->byte_count += chunk; | |
550 size -= chunk; | |
551 off += chunk; | |
552 } | |
553 /* If the buffer is full and we have more to add, flush it out. */ | |
554 if (size > 0) | |
555 { | |
556 if (Lstream_flush_out (lstr) < 0) | |
557 { | |
558 if (off == 0) | |
559 return -1; | |
560 else | |
561 return 0; | |
562 } | |
563 } | |
564 else | |
565 break; | |
566 } | |
567 } | |
568 return 0; | |
428 | 569 } |
570 | |
771 | 571 /* Write SIZE bytes of DATA to the stream. Return value is 0 on success, |
572 -1 on error. -1 is only returned when no bytes could be written; if any | |
573 bytes could be written, then 0 is returned and any unwritten bytes are | |
574 buffered and the next call to Lstream_write() will try to write them | |
575 again. (This buffering happens even when the stream's buffering type is | |
576 LSTREAM_UNBUFFERED, and regardless of how much data is passed in or what | |
577 the stream's buffering size was set to. #### There should perhaps be a | |
578 way to control whether this happens.) */ | |
428 | 579 |
771 | 580 int |
665 | 581 Lstream_write (Lstream *lstr, const void *data, Bytecount size) |
428 | 582 { |
665 | 583 Bytecount i; |
442 | 584 const unsigned char *p = (const unsigned char *) data; |
428 | 585 |
771 | 586 /* If the stream is not line-buffered, then we can just call |
587 Lstream_write_1(), which writes in chunks. Otherwise, we repeatedly | |
588 call Lstream_putc(), which knows how to handle line buffering. | |
589 Returns 0 on success, -1 on failure. */ | |
590 | |
428 | 591 if (size == 0) |
771 | 592 return 0; |
428 | 593 if (lstr->buffering != LSTREAM_LINE_BUFFERED) |
594 return Lstream_write_1 (lstr, data, size); | |
595 for (i = 0; i < size; i++) | |
596 { | |
597 if (Lstream_putc (lstr, p[i]) < 0) | |
598 break; | |
599 } | |
771 | 600 return i == 0 ? -1 : 0; |
428 | 601 } |
602 | |
603 int | |
604 Lstream_was_blocked_p (Lstream *lstr) | |
605 { | |
606 return lstr->imp->was_blocked_p ? lstr->imp->was_blocked_p (lstr) : 0; | |
607 } | |
608 | |
665 | 609 static Bytecount |
462 | 610 Lstream_raw_read (Lstream *lstr, unsigned char *buffer, |
665 | 611 Bytecount size) |
428 | 612 { |
613 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) | |
614 Lstream_internal_error ("lstream not open", lstr); | |
615 if (! (lstr->flags & LSTREAM_FL_READ)) | |
616 Lstream_internal_error ("lstream not open for reading", lstr); | |
617 if (!lstr->imp->reader) | |
618 Lstream_internal_error ("lstream has no reader", lstr); | |
619 | |
620 return (lstr->imp->reader) (lstr, buffer, size); | |
621 } | |
622 | |
623 /* Assuming the buffer is empty, fill it up again. */ | |
624 | |
665 | 625 static Bytecount |
428 | 626 Lstream_read_more (Lstream *lstr) |
627 { | |
628 #if 0 | |
665 | 629 Bytecount size_needed |
462 | 630 = max (1, min (MAX_READ_SIZE, lstr->buffering_size)); |
428 | 631 #else |
632 /* If someone requested a larger buffer size, so be it! */ | |
665 | 633 Bytecount size_needed = |
462 | 634 max (1, lstr->buffering_size); |
428 | 635 #endif |
665 | 636 Bytecount size_gotten; |
428 | 637 |
638 DO_REALLOC (lstr->in_buffer, lstr->in_buffer_size, | |
639 size_needed, unsigned char); | |
640 size_gotten = Lstream_raw_read (lstr, lstr->in_buffer, size_needed); | |
641 lstr->in_buffer_current = max (0, size_gotten); | |
642 lstr->in_buffer_ind = 0; | |
643 return size_gotten < 0 ? -1 : size_gotten; | |
644 } | |
645 | |
771 | 646 /* Read SIZE bytes of DATA from the stream. Return the number of bytes |
647 read. 0 means EOF (#### sometimes; it may simply indicate we can't read | |
648 any data at other times, particularly if SIZE is too small. this needs | |
649 to be fixed!). -1 means an error occurred and no bytes were read. */ | |
650 | |
814 | 651 static Bytecount |
652 Lstream_read_1 (Lstream *lstr, void *data, Bytecount size, | |
653 int override_no_partial_chars) | |
428 | 654 { |
655 unsigned char *p = (unsigned char *) data; | |
665 | 656 Bytecount off = 0; |
657 Bytecount chunk; | |
428 | 658 int error_occurred = 0; |
659 | |
660 if (size == 0) | |
661 return 0; | |
662 | |
663 /* First try to get some data from the unget buffer */ | |
664 chunk = min (size, lstr->unget_buffer_ind); | |
665 if (chunk > 0) | |
666 { | |
667 /* The bytes come back in reverse order. */ | |
668 for (; off < chunk; off++) | |
669 p[off] = lstr->unget_buffer[--lstr->unget_buffer_ind]; | |
670 lstr->byte_count += chunk; | |
671 size -= chunk; | |
672 } | |
673 | |
674 while (size > 0) | |
675 { | |
771 | 676 /* If unbuffered, then simply read directly into output buffer. |
677 No need to copy. */ | |
678 if (lstr->buffering == LSTREAM_UNBUFFERED) | |
679 { | |
680 chunk = Lstream_raw_read (lstr, p + off, size); | |
681 if (chunk < 0) | |
682 error_occurred = 1; | |
683 if (chunk <= 0) | |
684 break; | |
685 lstr->byte_count += chunk; | |
428 | 686 size -= chunk; |
771 | 687 off += chunk; |
688 } | |
689 else | |
428 | 690 { |
771 | 691 /* Take whatever we can from the in buffer */ |
692 chunk = min (size, lstr->in_buffer_current - lstr->in_buffer_ind); | |
693 if (chunk > 0) | |
694 { | |
695 memcpy (p + off, lstr->in_buffer + lstr->in_buffer_ind, chunk); | |
696 lstr->in_buffer_ind += chunk; | |
697 lstr->byte_count += chunk; | |
698 size -= chunk; | |
699 off += chunk; | |
700 } | |
701 | |
702 /* If we need some more, try to get some more from the | |
703 stream's end */ | |
704 if (size > 0) | |
705 { | |
706 Bytecount retval = Lstream_read_more (lstr); | |
707 if (retval < 0) | |
708 error_occurred = 1; | |
709 if (retval <= 0) | |
710 break; | |
711 } | |
428 | 712 } |
713 } | |
714 | |
814 | 715 if ((lstr->flags & LSTREAM_FL_NO_PARTIAL_CHARS) && |
716 !override_no_partial_chars) | |
428 | 717 { |
718 /* It's quite possible for us to get passed an incomplete | |
719 character at the end. We need to spit back that | |
720 incomplete character. */ | |
867 | 721 Bytecount newoff = validate_ibyte_string_backward (p, off); |
771 | 722 if (newoff < off) |
428 | 723 { |
771 | 724 Lstream_unread (lstr, p + newoff, off - newoff); |
725 off = newoff; | |
428 | 726 } |
727 } | |
728 | |
462 | 729 return off == 0 && error_occurred ? -1 : off; |
428 | 730 } |
731 | |
814 | 732 Bytecount |
733 Lstream_read (Lstream *lstr, void *data, Bytecount size) | |
734 { | |
735 return Lstream_read_1 (lstr, data, size, 0); | |
736 } | |
737 | |
5776
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
738 Charcount |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
739 Lstream_character_tell (Lstream *lstr) |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
740 { |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
741 Charcount ctell = lstr->imp->character_tell ? |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
742 lstr->imp->character_tell (lstr) : -1; |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
743 |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
744 if (ctell >= 0) |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
745 { |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
746 /* Our implementation's character tell code doesn't know about the |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
747 unget buffer, update its figure to reflect it. */ |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
748 ctell += lstr->unget_character_count; |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
749 |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
750 if (lstr->unget_buffer_ind > 0) |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
751 { |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
752 /* The character count should not include those characters |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
753 currently *in* the unget buffer, subtract that count. */ |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
754 Ibyte *ungot, *ungot_ptr; |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
755 Bytecount ii = lstr->unget_buffer_ind, impartial, sevenflen; |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
756 |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
757 ungot_ptr = ungot |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
758 = alloca_ibytes (lstr->unget_buffer_ind) + MAX_ICHAR_LEN; |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
759 |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
760 /* Make sure the string starts with a valid ibyteptr, otherwise |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
761 validate_ibyte_string_backward could run off the beginning. */ |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
762 sevenflen = set_itext_ichar (ungot, (Ichar) 0x7f); |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
763 ungot_ptr += sevenflen; |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
764 |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
765 /* Internal format data, but in reverse order. There's not |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
766 actually a need to alloca here, we could work out the character |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
767 count directly from the reversed bytes, but the alloca approach |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
768 is more robust to changes in our internal format, and the unget |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
769 buffer is not going to blow the stack. */ |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
770 while (ii > 0) |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
771 { |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
772 *ungot_ptr++ = lstr->unget_buffer[--ii]; |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
773 } |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
774 |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
775 impartial |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
776 = validate_ibyte_string_backward (ungot, ungot_ptr - ungot); |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
777 |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
778 /* Move past the character we added. */ |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
779 impartial -= sevenflen; |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
780 INC_IBYTEPTR (ungot); |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
781 |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
782 if (impartial > 0 && !valid_ibyteptr_p (ungot)) |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
783 { |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
784 Ibyte *newstart = ungot, *limit = ungot + impartial; |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
785 /* Our consumer has the start of a partial character, we |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
786 have the rest. */ |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
787 |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
788 while (!valid_ibyteptr_p (newstart) && newstart < limit) |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
789 { |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
790 newstart++, impartial--; |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
791 } |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
792 |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
793 /* Remove this character from the count, since the |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
794 end-consumer hasn't seen the full character. */ |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
795 ctell--; |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
796 ungot = newstart; |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
797 } |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
798 else if (valid_ibyteptr_p (ungot) |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
799 && rep_bytes_by_first_byte (*ungot) > impartial) |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
800 { |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
801 /* Rest of a partial character has yet to be read, its first |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
802 octet has probably been unread by Lstream_read_1(). We |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
803 included it in the accounting in Lstream_unread(), adjust |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
804 the figure here appropriately. */ |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
805 ctell--; |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
806 } |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
807 |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
808 /* bytecount_to_charcount will throw an assertion failure if we're |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
809 not at the start of a character. */ |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
810 text_checking_assert (impartial == 0 || valid_ibyteptr_p (ungot)); |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
811 |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
812 /* The character length of this text is included in |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
813 unget_character_count; if the bytes are still in the unget |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
814 buffer, then our consumers haven't seen them, and so the |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
815 character tell figure shouldn't reflect them. Subtract it from |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
816 the total. */ |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
817 ctell -= bytecount_to_charcount (ungot, impartial); |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
818 } |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
819 |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
820 if (lstr->in_buffer_ind < lstr->in_buffer_current) |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
821 { |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
822 Ibyte *inbuf = lstr->in_buffer + lstr->in_buffer_ind; |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
823 Bytecount partial = lstr->in_buffer_current - lstr->in_buffer_ind, |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
824 impartial; |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
825 |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
826 if (!valid_ibyteptr_p (inbuf)) |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
827 { |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
828 Ibyte *newstart = inbuf; |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
829 Ibyte *limit = lstr->in_buffer + lstr->in_buffer_current; |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
830 /* Our consumer has the start of a partial character, we |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
831 have the rest. */ |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
832 |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
833 while (newstart < limit && !valid_ibyteptr_p (newstart)) |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
834 { |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
835 newstart++; |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
836 } |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
837 |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
838 /* Remove this character from the count, since the |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
839 end-consumer hasn't seen the full character. */ |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
840 ctell--; |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
841 inbuf = newstart; |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
842 partial = limit - newstart; |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
843 } |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
844 |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
845 if (valid_ibyteptr_p (inbuf)) |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
846 { |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
847 /* There's at least one valid starting char in the string, |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
848 validate_ibyte_string_backward won't run off the |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
849 begining. */ |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
850 impartial = |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
851 validate_ibyte_string_backward (inbuf, partial); |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
852 } |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
853 else |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
854 { |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
855 impartial = 0; |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
856 } |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
857 |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
858 ctell -= bytecount_to_charcount (inbuf, impartial); |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
859 } |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
860 |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
861 text_checking_assert (ctell >= 0); |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
862 } |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
863 |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
864 return ctell; |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
865 } |
814 | 866 |
771 | 867 /* Push back SIZE bytes of DATA onto the input queue. The next call |
868 to Lstream_read() with the same size will read the same bytes back. | |
869 Note that this will be the case even if there is other pending | |
870 unread data. */ | |
871 | |
428 | 872 void |
665 | 873 Lstream_unread (Lstream *lstr, const void *data, Bytecount size) |
428 | 874 { |
442 | 875 const unsigned char *p = (const unsigned char *) data; |
428 | 876 |
877 /* Make sure buffer is big enough */ | |
878 DO_REALLOC (lstr->unget_buffer, lstr->unget_buffer_size, | |
879 lstr->unget_buffer_ind + size, unsigned char); | |
880 | |
881 lstr->byte_count -= size; | |
882 | |
883 /* Bytes have to go on in reverse order -- they are reversed | |
884 again when read back. */ | |
885 while (size--) | |
5776
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
886 { |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
887 lstr->unget_buffer[lstr->unget_buffer_ind++] = p[size]; |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
888 /* If we see a valid first byte, that is the last octet in a |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
889 character, so increase the count of ungot characters. */ |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
890 lstr->unget_character_count += valid_ibyteptr_p (p + size); |
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
891 } |
428 | 892 } |
893 | |
771 | 894 /* Rewind the stream to the beginning. */ |
895 | |
428 | 896 int |
897 Lstream_rewind (Lstream *lstr) | |
898 { | |
899 if (!lstr->imp->rewinder) | |
900 Lstream_internal_error ("lstream has no rewinder", lstr); | |
901 if (Lstream_flush (lstr) < 0) | |
902 return -1; | |
903 lstr->byte_count = 0; | |
5776
65d65b52d608
Pass character count from coding systems to buffer insertion code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5588
diff
changeset
|
904 lstr->unget_character_count = 0; |
428 | 905 return (lstr->imp->rewinder) (lstr); |
906 } | |
907 | |
908 int | |
909 Lstream_seekable_p (Lstream *lstr) | |
910 { | |
911 if (!lstr->imp->rewinder) | |
912 return 0; | |
913 if (!lstr->imp->seekable_p) | |
914 return 1; | |
915 return (lstr->imp->seekable_p) (lstr); | |
916 } | |
917 | |
918 static int | |
919 Lstream_pseudo_close (Lstream *lstr) | |
920 { | |
1943 | 921 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) |
428 | 922 Lstream_internal_error ("lstream is not open", lstr); |
923 | |
924 /* don't check errors here -- best not to risk file descriptor loss */ | |
925 return Lstream_flush (lstr); | |
926 } | |
927 | |
5588
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
928 /* Close the stream without flushing buffers. |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
929 In current practice, this is only useful when a subprocess terminates |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
930 unexpectedly, and the OS closes its pipes without warning. In that case, |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
931 we do not want to flush our output buffers, as there is no longer a pipe |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
932 to write to. |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
933 This nomenclature may deserve review if XEmacs starts getting called as |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
934 a subprocess. */ |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
935 |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
936 int |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
937 Lstream_close_noflush (Lstream *lstr) |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
938 { |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
939 lstr->flags &= ~LSTREAM_FL_IS_OPEN; |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
940 lstr->byte_count = 0; |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
941 /* Note that Lstream_flush() reset all the buffer indices. That way, |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
942 the next call to Lstream_putc(), Lstream_getc(), or Lstream_ungetc() |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
943 on a closed stream will call into the function equivalents, which will |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
944 cause an error. */ |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
945 |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
946 /* We set the pointers to 0 so that we don't lose when this function |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
947 is called more than once on the same object */ |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
948 if (lstr->out_buffer) |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
949 { |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
950 xfree (lstr->out_buffer); |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
951 lstr->out_buffer = 0; |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
952 } |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
953 if (lstr->in_buffer) |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
954 { |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
955 xfree (lstr->in_buffer); |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
956 lstr->in_buffer = 0; |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
957 } |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
958 if (lstr->unget_buffer) |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
959 { |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
960 xfree (lstr->unget_buffer); |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
961 lstr->unget_buffer = 0; |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
962 } |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
963 |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
964 return 0; |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
965 } |
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
966 |
771 | 967 /* Close the stream. All data will be flushed out. If the stream is |
968 already closed, nothing happens. Note that, even if all data has | |
969 already been flushed out, the act of closing a stream may generate more | |
970 data -- for example, if the stream implements some sort of conversion, | |
971 such as gzip, there may be special "end-data" that need to be written | |
972 out when the file is closed. */ | |
973 | |
428 | 974 int |
975 Lstream_close (Lstream *lstr) | |
976 { | |
977 int rc = 0; | |
978 | |
979 if (lstr->flags & LSTREAM_FL_IS_OPEN) | |
980 { | |
981 rc = Lstream_pseudo_close (lstr); | |
982 /* | |
983 * We used to return immediately if the closer method reported | |
984 * failure, leaving the stream open. But this is no good, for | |
985 * the following reasons. | |
986 * | |
987 * 1. The finalizer method used in GC makes no provision for | |
988 * failure, so we must not return without freeing buffer | |
989 * memory. | |
990 * | |
991 * 2. The closer method may have already freed some memory | |
992 * used for I/O in this stream. E.g. encoding_closer frees | |
993 * ENCODING_STREAM_DATA(stream)->runoff. If a writer method | |
994 * tries to use this buffer later, it will write into memory | |
995 * that may have been allocated elsewhere. Sometime later | |
996 * you will see a sign that says "Welcome to Crash City." | |
997 * | |
998 * 3. The closer can report failure if a flush fails in the | |
999 * other stream in a MULE encoding/decoding stream pair. | |
1000 * The other stream in the pair is closed, but returning | |
1001 * early leaves the current stream open. If we try to | |
1002 * flush the current stream later, we will crash when the | |
1003 * flusher notices that the other end stream is closed. | |
1004 * | |
1005 * So, we no longer abort the close if the closer method | |
1006 * reports some kind of failure. We still report the failure | |
1007 * to the caller. | |
1008 */ | |
1009 if (lstr->imp->closer) | |
1010 if ((lstr->imp->closer) (lstr) < 0) | |
1011 rc = -1; | |
1012 } | |
1013 | |
5588
2dbefd79b3d3
Prevent SIGPIPEs in deactivate_process().
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5581
diff
changeset
|
1014 Lstream_close_noflush (lstr); |
428 | 1015 |
1016 return rc; | |
1017 } | |
1018 | |
771 | 1019 |
1020 /* Function equivalent of Lstream_putc(). */ | |
1021 | |
428 | 1022 int |
1023 Lstream_fputc (Lstream *lstr, int c) | |
1024 { | |
1025 unsigned char ch = (unsigned char) c; | |
771 | 1026 int retval = Lstream_write_1 (lstr, &ch, 1); |
1027 if (retval == 0 && lstr->buffering == LSTREAM_LINE_BUFFERED && ch == '\n') | |
428 | 1028 return Lstream_flush_out (lstr); |
771 | 1029 return retval; |
428 | 1030 } |
1031 | |
771 | 1032 /* Function equivalent of Lstream_getc(). */ |
1033 | |
428 | 1034 int |
1035 Lstream_fgetc (Lstream *lstr) | |
1036 { | |
1037 unsigned char ch; | |
814 | 1038 if (Lstream_read_1 (lstr, &ch, 1, 1) <= 0) |
428 | 1039 return -1; |
1040 return ch; | |
1041 } | |
1042 | |
771 | 1043 /* Function equivalent of Lstream_ungetc(). */ |
1044 | |
428 | 1045 void |
1046 Lstream_fungetc (Lstream *lstr, int c) | |
1047 { | |
1048 unsigned char ch = (unsigned char) c; | |
1049 Lstream_unread (lstr, &ch, 1); | |
1050 } | |
1051 | |
1052 | |
1053 /************************ some stream implementations *********************/ | |
1054 | |
1055 /*********** a stdio stream ***********/ | |
1056 | |
1057 struct stdio_stream | |
1058 { | |
1059 FILE *file; | |
1060 int closing; | |
1061 }; | |
1062 | |
1063 #define STDIO_STREAM_DATA(stream) LSTREAM_TYPE_DATA (stream, stdio) | |
1064 | |
771 | 1065 DEFINE_LSTREAM_IMPLEMENTATION ("stdio", stdio); |
428 | 1066 |
1067 static Lisp_Object | |
442 | 1068 make_stdio_stream_1 (FILE *stream, int flags, const char *mode) |
428 | 1069 { |
1070 Lstream *lstr = Lstream_new (lstream_stdio, mode); | |
1071 struct stdio_stream *str = STDIO_STREAM_DATA (lstr); | |
1072 str->file = stream; | |
1073 str->closing = flags & LSTR_CLOSING; | |
1074 lstr->flags |= LSTREAM_FL_CLOSE_AT_DISKSAVE; | |
793 | 1075 return wrap_lstream (lstr); |
428 | 1076 } |
1077 | |
1078 Lisp_Object | |
1079 make_stdio_input_stream (FILE *stream, int flags) | |
1080 { | |
1081 return make_stdio_stream_1 (stream, flags, "r"); | |
1082 } | |
1083 | |
1084 Lisp_Object | |
1085 make_stdio_output_stream (FILE *stream, int flags) | |
1086 { | |
1087 return make_stdio_stream_1 (stream, flags, "w"); | |
1088 } | |
1089 | |
1090 /* #### From reading the Unix 98 specification, it appears that if we | |
1091 want stdio_reader() to be completely correct, we should check for | |
1092 0 < val < size and if so, check to see if an error has occurred. | |
1093 If an error has occurred, but val is non-zero, we should go ahead | |
1094 and act as if the read was successful, but remember in some fashion | |
1095 or other, that an error has occurred, and report that on the next | |
771 | 1096 call to stdio_reader instead of calling retry_fread() again. |
428 | 1097 |
771 | 1098 Currently, in such a case, we end up calling retry_fread() twice and we |
428 | 1099 assume that |
1100 | |
1101 1) this is not harmful, and | |
1102 2) the error will still be reported on the second read. | |
1103 | |
1104 This is probably reasonable, so I don't think we should change this | |
1105 code (it could even be argued that the error might have fixed | |
771 | 1106 itself, so we should do the retry_fread() again. */ |
428 | 1107 |
665 | 1108 static Bytecount |
1109 stdio_reader (Lstream *stream, unsigned char *data, Bytecount size) | |
428 | 1110 { |
1111 struct stdio_stream *str = STDIO_STREAM_DATA (stream); | |
771 | 1112 Bytecount val = retry_fread (data, 1, size, str->file); |
1113 if (!val) | |
1114 { | |
1115 if (ferror (str->file)) | |
1116 return LSTREAM_ERROR; | |
1117 if (feof (str->file)) | |
1118 return 0; /* LSTREAM_EOF; */ | |
1119 } | |
428 | 1120 return val; |
1121 } | |
1122 | |
665 | 1123 static Bytecount |
462 | 1124 stdio_writer (Lstream *stream, const unsigned char *data, |
665 | 1125 Bytecount size) |
428 | 1126 { |
1127 struct stdio_stream *str = STDIO_STREAM_DATA (stream); | |
771 | 1128 Bytecount val = retry_fwrite (data, 1, size, str->file); |
428 | 1129 if (!val && ferror (str->file)) |
771 | 1130 return LSTREAM_ERROR; |
428 | 1131 return val; |
1132 } | |
1133 | |
1134 static int | |
1135 stdio_rewinder (Lstream *stream) | |
1136 { | |
1137 rewind (STDIO_STREAM_DATA (stream)->file); | |
1138 return 0; | |
1139 } | |
1140 | |
1141 static int | |
1142 stdio_seekable_p (Lstream *stream) | |
1143 { | |
1144 struct stat lestat; | |
1145 struct stdio_stream *str = STDIO_STREAM_DATA (stream); | |
1146 | |
771 | 1147 if (qxe_fstat (fileno (str->file), &lestat) < 0) |
428 | 1148 return 0; |
1149 return S_ISREG (lestat.st_mode); | |
1150 } | |
1151 | |
1152 static int | |
1153 stdio_flusher (Lstream *stream) | |
1154 { | |
1155 struct stdio_stream *str = STDIO_STREAM_DATA (stream); | |
1156 if (stream->flags & LSTREAM_FL_WRITE) | |
1157 return fflush (str->file); | |
1158 else | |
1159 return 0; | |
1160 } | |
1161 | |
1162 static int | |
1163 stdio_closer (Lstream *stream) | |
1164 { | |
1165 struct stdio_stream *str = STDIO_STREAM_DATA (stream); | |
1166 if (str->closing) | |
771 | 1167 return retry_fclose (str->file); |
428 | 1168 else |
1169 if (stream->flags & LSTREAM_FL_WRITE) | |
1170 return fflush (str->file); | |
1171 else | |
1172 return 0; | |
1173 } | |
1174 | |
1175 /*********** a file descriptor ***********/ | |
1176 | |
1177 struct filedesc_stream | |
1178 { | |
1179 int fd; | |
1180 int pty_max_bytes; | |
867 | 1181 Ibyte eof_char; |
428 | 1182 int starting_pos; |
1183 int current_pos; | |
1184 int end_pos; | |
1185 int chars_sans_newline; | |
1186 unsigned int closing :1; | |
1187 unsigned int allow_quit :1; | |
1188 unsigned int blocked_ok :1; | |
1189 unsigned int pty_flushing :1; | |
1190 unsigned int blocking_error_p :1; | |
1191 }; | |
1192 | |
1193 #define FILEDESC_STREAM_DATA(stream) LSTREAM_TYPE_DATA (stream, filedesc) | |
1194 | |
771 | 1195 DEFINE_LSTREAM_IMPLEMENTATION ("filedesc", filedesc); |
428 | 1196 |
1197 /* Make a stream that reads from or writes to a file descriptor FILEDESC. | |
1198 OFFSET is the offset from the *current* file pointer that the reading | |
1199 should start at. COUNT is the number of bytes to be read (it is | |
1200 ignored when writing); -1 for unlimited. */ | |
1201 static Lisp_Object | |
1202 make_filedesc_stream_1 (int filedesc, int offset, int count, int flags, | |
442 | 1203 const char *mode) |
428 | 1204 { |
1205 Lstream *lstr = Lstream_new (lstream_filedesc, mode); | |
1206 struct filedesc_stream *fstr = FILEDESC_STREAM_DATA (lstr); | |
1207 fstr->fd = filedesc; | |
1208 fstr->closing = !!(flags & LSTR_CLOSING); | |
1209 fstr->allow_quit = !!(flags & LSTR_ALLOW_QUIT); | |
1210 fstr->blocked_ok = !!(flags & LSTR_BLOCKED_OK); | |
1211 fstr->pty_flushing = !!(flags & LSTR_PTY_FLUSHING); | |
1212 fstr->blocking_error_p = 0; | |
1213 fstr->chars_sans_newline = 0; | |
1214 fstr->starting_pos = lseek (filedesc, offset, SEEK_CUR); | |
1215 fstr->current_pos = max (fstr->starting_pos, 0); | |
1216 if (count < 0) | |
1217 fstr->end_pos = -1; | |
1218 else | |
1219 fstr->end_pos = fstr->starting_pos + count; | |
1220 lstr->flags |= LSTREAM_FL_CLOSE_AT_DISKSAVE; | |
793 | 1221 return wrap_lstream (lstr); |
428 | 1222 } |
1223 | |
814 | 1224 /* Flags: |
1225 | |
1226 LSTR_CLOSING | |
1227 If set, close the descriptor or FILE * when the stream is closed. | |
1228 | |
1229 LSTR_ALLOW_QUIT | |
1230 If set, allow quitting out of the actual I/O. | |
1231 | |
1232 LSTR_PTY_FLUSHING | |
1233 If set and filedesc_stream_set_pty_flushing() has been called | |
1234 on the stream, do not send more than pty_max_bytes on a single | |
1235 line without flushing the data out using the eof_char. | |
1236 | |
1237 LSTR_BLOCKED_OK | |
1238 If set, an EWOULDBLOCK error is not treated as an error but | |
1239 simply causes the write function to return 0 as the number | |
1240 of bytes written out. | |
1241 */ | |
1242 | |
428 | 1243 Lisp_Object |
1244 make_filedesc_input_stream (int filedesc, int offset, int count, int flags) | |
1245 { | |
1246 return make_filedesc_stream_1 (filedesc, offset, count, flags, "r"); | |
1247 } | |
1248 | |
1249 Lisp_Object | |
1250 make_filedesc_output_stream (int filedesc, int offset, int count, int flags) | |
1251 { | |
1252 return make_filedesc_stream_1 (filedesc, offset, count, flags, "w"); | |
1253 } | |
1254 | |
665 | 1255 static Bytecount |
1256 filedesc_reader (Lstream *stream, unsigned char *data, Bytecount size) | |
428 | 1257 { |
665 | 1258 Bytecount nread; |
428 | 1259 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); |
1260 if (str->end_pos >= 0) | |
665 | 1261 size = min (size, (Bytecount) (str->end_pos - str->current_pos)); |
430 | 1262 nread = str->allow_quit ? |
1263 read_allowing_quit (str->fd, data, size) : | |
771 | 1264 retry_read (str->fd, data, size); |
428 | 1265 if (nread > 0) |
1266 str->current_pos += nread; | |
771 | 1267 if (nread == 0) |
1268 return 0; /* LSTREAM_EOF; */ | |
1269 if (nread < 0) | |
1270 return LSTREAM_ERROR; | |
428 | 1271 return nread; |
1272 } | |
1273 | |
1274 static int | |
1275 errno_would_block_p (int val) | |
1276 { | |
1277 #ifdef EWOULDBLOCK | |
1278 if (val == EWOULDBLOCK) | |
1279 return 1; | |
1280 #endif | |
1281 #ifdef EAGAIN | |
1282 if (val == EAGAIN) | |
1283 return 1; | |
1284 #endif | |
1285 return 0; | |
1286 } | |
1287 | |
665 | 1288 static Bytecount |
462 | 1289 filedesc_writer (Lstream *stream, const unsigned char *data, |
665 | 1290 Bytecount size) |
428 | 1291 { |
1292 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); | |
665 | 1293 Bytecount retval; |
428 | 1294 int need_newline = 0; |
1295 | |
1296 /* This function would be simple if it were not for the blasted | |
1297 PTY max-bytes stuff. Why the hell can't they just have written | |
1298 the PTY drivers right so this problem doesn't exist? | |
1299 | |
1300 Maybe all the PTY crap here should be moved into another stream | |
1301 that does nothing but periodically insert EOF's as necessary. */ | |
1302 if (str->pty_flushing) | |
1303 { | |
1304 /* To make life easy, only send out one line at the most. */ | |
442 | 1305 const unsigned char *ptr; |
428 | 1306 |
442 | 1307 ptr = (const unsigned char *) memchr (data, '\n', size); |
428 | 1308 if (ptr) |
1309 need_newline = 1; | |
1310 else | |
1311 ptr = data + size; | |
1312 if (ptr - data >= str->pty_max_bytes - str->chars_sans_newline) | |
1313 { | |
1314 ptr = data + str->pty_max_bytes - str->chars_sans_newline; | |
1315 need_newline = 0; | |
1316 } | |
1317 size = ptr - data; | |
1318 } | |
1319 | |
1320 /**** start of non-PTY-crap ****/ | |
1321 if (size > 0) | |
430 | 1322 retval = str->allow_quit ? |
1323 write_allowing_quit (str->fd, data, size) : | |
771 | 1324 retry_write (str->fd, data, size); |
428 | 1325 else |
1326 retval = 0; | |
1327 if (retval < 0 && errno_would_block_p (errno) && str->blocked_ok) | |
1328 { | |
1329 str->blocking_error_p = 1; | |
1330 return 0; | |
1331 } | |
1332 str->blocking_error_p = 0; | |
1333 if (retval < 0) | |
771 | 1334 return LSTREAM_ERROR; |
428 | 1335 /**** end non-PTY-crap ****/ |
1336 | |
1337 if (str->pty_flushing) | |
1338 { | |
1339 str->chars_sans_newline += retval; | |
1340 /* Note that a newline was not among the bytes written out. | |
1341 Add to the number of non-newline bytes written out, | |
1342 and flush with an EOF if necessary. Be careful to | |
1343 keep track of write errors as we go along and look | |
1344 out for EWOULDBLOCK. */ | |
1345 if (str->chars_sans_newline >= str->pty_max_bytes) | |
1346 { | |
665 | 1347 Bytecount retval2 = str->allow_quit ? |
430 | 1348 write_allowing_quit (str->fd, &str->eof_char, 1) : |
771 | 1349 retry_write (str->fd, &str->eof_char, 1); |
430 | 1350 |
428 | 1351 if (retval2 > 0) |
1352 str->chars_sans_newline = 0; | |
1353 else if (retval2 < 0) | |
1354 { | |
1355 /* Error writing the EOF char. If nothing got written, | |
1356 then treat this as an error -- either return an error | |
1357 condition or set the blocking-error flag. */ | |
1358 if (retval == 0) | |
1359 { | |
1360 if (errno_would_block_p (errno) && str->blocked_ok) | |
1361 { | |
1362 str->blocking_error_p = 1; | |
1363 return 0; | |
1364 } | |
1365 else | |
771 | 1366 return LSTREAM_ERROR; |
428 | 1367 } |
1368 else | |
1369 return retval; | |
1370 } | |
1371 } | |
1372 } | |
1373 | |
1374 /* The need_newline flag is necessary because otherwise when the | |
1375 first byte is a newline, we'd get stuck never writing anything | |
1376 in pty-flushing mode. */ | |
1377 if (need_newline) | |
1378 { | |
867 | 1379 Ibyte nl = '\n'; |
665 | 1380 Bytecount retval2 = str->allow_quit ? |
430 | 1381 write_allowing_quit (str->fd, &nl, 1) : |
771 | 1382 retry_write (str->fd, &nl, 1); |
430 | 1383 |
428 | 1384 if (retval2 > 0) |
1385 { | |
1386 str->chars_sans_newline = 0; | |
1387 retval++; | |
1388 } | |
1389 else if (retval2 < 0) | |
1390 { | |
1391 /* Error writing the newline char. If nothing got written, | |
1392 then treat this as an error -- either return an error | |
1393 condition or set the blocking-error flag. */ | |
1394 if (retval == 0) | |
1395 { | |
1396 if (errno_would_block_p (errno) && str->blocked_ok) | |
1397 { | |
1398 str->blocking_error_p = 1; | |
1399 return 0; | |
1400 } | |
1401 else | |
771 | 1402 return LSTREAM_ERROR; |
428 | 1403 } |
1404 else | |
1405 return retval; | |
1406 } | |
1407 } | |
1408 | |
1409 return retval; | |
1410 } | |
1411 | |
1412 static int | |
1413 filedesc_rewinder (Lstream *stream) | |
1414 { | |
1415 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); | |
1416 if (str->starting_pos < 0 || | |
1417 lseek (FILEDESC_STREAM_DATA (stream)->fd, str->starting_pos, | |
1418 SEEK_SET) == -1) | |
1419 return -1; | |
1420 else | |
1421 { | |
1422 str->current_pos = str->starting_pos; | |
1423 return 0; | |
1424 } | |
1425 } | |
1426 | |
1427 static int | |
1428 filedesc_seekable_p (Lstream *stream) | |
1429 { | |
1430 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); | |
1431 if (str->starting_pos < 0) | |
1432 return 0; | |
1433 else | |
1434 { | |
1435 struct stat lestat; | |
1436 | |
771 | 1437 if (qxe_fstat (str->fd, &lestat) < 0) |
428 | 1438 return 0; |
1439 return S_ISREG (lestat.st_mode); | |
1440 } | |
1441 } | |
1442 | |
1443 static int | |
1444 filedesc_closer (Lstream *stream) | |
1445 { | |
1446 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); | |
1447 if (str->closing) | |
771 | 1448 return retry_close (str->fd); |
428 | 1449 else |
1450 return 0; | |
1451 } | |
1452 | |
1453 static int | |
1454 filedesc_was_blocked_p (Lstream *stream) | |
1455 { | |
1456 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); | |
1457 return str->blocking_error_p; | |
1458 } | |
1459 | |
1460 void | |
1461 filedesc_stream_set_pty_flushing (Lstream *stream, int pty_max_bytes, | |
867 | 1462 Ibyte eof_char) |
428 | 1463 { |
1464 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); | |
1465 str->pty_max_bytes = pty_max_bytes; | |
1466 str->eof_char = eof_char; | |
1467 str->pty_flushing = 1; | |
1468 } | |
1469 | |
1470 int | |
1471 filedesc_stream_fd (Lstream *stream) | |
1472 { | |
1473 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); | |
1474 return str->fd; | |
1475 } | |
1476 | |
1477 /*********** read from a Lisp string ***********/ | |
1478 | |
1479 #define LISP_STRING_STREAM_DATA(stream) LSTREAM_TYPE_DATA (stream, lisp_string) | |
1480 | |
1481 struct lisp_string_stream | |
1482 { | |
1483 Lisp_Object obj; | |
1484 Bytecount init_offset; | |
1485 Bytecount offset, end; | |
1486 }; | |
1487 | |
1204 | 1488 static const struct memory_description lisp_string_lstream_description[] = { |
1489 { XD_LISP_OBJECT, offsetof (struct lisp_string_stream, obj) }, | |
1490 { XD_END } | |
1491 }; | |
1492 | |
1493 DEFINE_LSTREAM_IMPLEMENTATION_WITH_DATA ("lisp-string", lisp_string); | |
428 | 1494 |
1495 Lisp_Object | |
1496 make_lisp_string_input_stream (Lisp_Object string, Bytecount offset, | |
1497 Bytecount len) | |
1498 { | |
1499 Lstream *lstr; | |
1500 struct lisp_string_stream *str; | |
1501 | |
1502 CHECK_STRING (string); | |
1503 if (len < 0) | |
1504 len = XSTRING_LENGTH (string) - offset; | |
1505 assert (offset >= 0); | |
1506 assert (len >= 0); | |
1507 assert (offset + len <= XSTRING_LENGTH (string)); | |
1508 | |
1509 lstr = Lstream_new (lstream_lisp_string, "r"); | |
1510 str = LISP_STRING_STREAM_DATA (lstr); | |
1511 str->offset = offset; | |
1512 str->end = offset + len; | |
1513 str->init_offset = offset; | |
1514 str->obj = string; | |
793 | 1515 return wrap_lstream (lstr); |
428 | 1516 } |
1517 | |
665 | 1518 static Bytecount |
462 | 1519 lisp_string_reader (Lstream *stream, unsigned char *data, |
665 | 1520 Bytecount size) |
428 | 1521 { |
1522 struct lisp_string_stream *str = LISP_STRING_STREAM_DATA (stream); | |
1523 /* Don't lose if the string shrank past us ... */ | |
1524 Bytecount offset = min (str->offset, XSTRING_LENGTH (str->obj)); | |
867 | 1525 Ibyte *strstart = XSTRING_DATA (str->obj); |
1526 Ibyte *start = strstart + offset; | |
428 | 1527 |
1528 /* ... or if someone changed the string and we ended up in the | |
1529 middle of a character. */ | |
1530 /* Being in the middle of a character is `normal' unless | |
1531 LSTREAM_NO_PARTIAL_CHARS - mrb */ | |
1532 if (stream->flags & LSTREAM_FL_NO_PARTIAL_CHARS) | |
867 | 1533 VALIDATE_IBYTEPTR_BACKWARD (start); |
428 | 1534 offset = start - strstart; |
665 | 1535 size = min (size, (Bytecount) (str->end - offset)); |
428 | 1536 memcpy (data, start, size); |
1537 str->offset = offset + size; | |
1538 return size; | |
1539 } | |
1540 | |
1541 static int | |
1542 lisp_string_rewinder (Lstream *stream) | |
1543 { | |
1544 struct lisp_string_stream *str = LISP_STRING_STREAM_DATA (stream); | |
1545 int pos = str->init_offset; | |
1546 if (pos > str->end) | |
1547 pos = str->end; | |
1548 /* Don't lose if the string shrank past us ... */ | |
1549 pos = min (pos, XSTRING_LENGTH (str->obj)); | |
1550 /* ... or if someone changed the string and we ended up in the | |
1551 middle of a character. */ | |
1552 { | |
867 | 1553 Ibyte *strstart = XSTRING_DATA (str->obj); |
1554 Ibyte *start = strstart + pos; | |
1555 VALIDATE_IBYTEPTR_BACKWARD (start); | |
428 | 1556 pos = start - strstart; |
1557 } | |
1558 str->offset = pos; | |
1559 return 0; | |
1560 } | |
1561 | |
1562 static Lisp_Object | |
1563 lisp_string_marker (Lisp_Object stream) | |
1564 { | |
1565 struct lisp_string_stream *str = LISP_STRING_STREAM_DATA (XLSTREAM (stream)); | |
1566 return str->obj; | |
1567 } | |
1568 | |
1569 /*********** a fixed buffer ***********/ | |
1570 | |
1571 #define FIXED_BUFFER_STREAM_DATA(stream) \ | |
1572 LSTREAM_TYPE_DATA (stream, fixed_buffer) | |
1573 | |
1574 struct fixed_buffer_stream | |
1575 { | |
442 | 1576 const unsigned char *inbuf; |
428 | 1577 unsigned char *outbuf; |
665 | 1578 Bytecount size; |
1579 Bytecount offset; | |
428 | 1580 }; |
1581 | |
771 | 1582 DEFINE_LSTREAM_IMPLEMENTATION ("fixed-buffer", fixed_buffer); |
428 | 1583 |
1584 Lisp_Object | |
665 | 1585 make_fixed_buffer_input_stream (const void *buf, Bytecount size) |
428 | 1586 { |
1587 Lstream *lstr = Lstream_new (lstream_fixed_buffer, "r"); | |
1588 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (lstr); | |
440 | 1589 str->inbuf = (const unsigned char *) buf; |
428 | 1590 str->size = size; |
793 | 1591 return wrap_lstream (lstr); |
428 | 1592 } |
1593 | |
1594 Lisp_Object | |
665 | 1595 make_fixed_buffer_output_stream (void *buf, Bytecount size) |
428 | 1596 { |
1597 Lstream *lstr = Lstream_new (lstream_fixed_buffer, "w"); | |
1598 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (lstr); | |
440 | 1599 str->outbuf = (unsigned char *) buf; |
428 | 1600 str->size = size; |
793 | 1601 return wrap_lstream (lstr); |
428 | 1602 } |
1603 | |
665 | 1604 static Bytecount |
462 | 1605 fixed_buffer_reader (Lstream *stream, unsigned char *data, |
665 | 1606 Bytecount size) |
428 | 1607 { |
1608 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream); | |
1609 size = min (size, str->size - str->offset); | |
1610 memcpy (data, str->inbuf + str->offset, size); | |
1611 str->offset += size; | |
1612 return size; | |
1613 } | |
1614 | |
665 | 1615 static Bytecount |
462 | 1616 fixed_buffer_writer (Lstream *stream, const unsigned char *data, |
665 | 1617 Bytecount size) |
428 | 1618 { |
1619 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream); | |
1620 if (str->offset == str->size) | |
1621 { | |
1622 /* If we're at the end, just throw away the data and pretend | |
1623 we wrote all of it. If we return 0, then the lstream routines | |
1624 will try again and again to write it out. */ | |
1625 return size; | |
1626 } | |
1627 size = min (size, str->size - str->offset); | |
1628 memcpy (str->outbuf + str->offset, data, size); | |
1629 str->offset += size; | |
1630 return size; | |
1631 } | |
1632 | |
1633 static int | |
1634 fixed_buffer_rewinder (Lstream *stream) | |
1635 { | |
1636 FIXED_BUFFER_STREAM_DATA (stream)->offset = 0; | |
1637 return 0; | |
1638 } | |
1639 | |
442 | 1640 const unsigned char * |
428 | 1641 fixed_buffer_input_stream_ptr (Lstream *stream) |
1642 { | |
1643 assert (stream->imp == lstream_fixed_buffer); | |
1644 return FIXED_BUFFER_STREAM_DATA (stream)->inbuf; | |
1645 } | |
1646 | |
1647 unsigned char * | |
1648 fixed_buffer_output_stream_ptr (Lstream *stream) | |
1649 { | |
1650 assert (stream->imp == lstream_fixed_buffer); | |
1651 return FIXED_BUFFER_STREAM_DATA (stream)->outbuf; | |
1652 } | |
1653 | |
1654 /*********** write to a resizing buffer ***********/ | |
1655 | |
1656 #define RESIZING_BUFFER_STREAM_DATA(stream) \ | |
1657 LSTREAM_TYPE_DATA (stream, resizing_buffer) | |
1658 | |
1659 struct resizing_buffer_stream | |
1660 { | |
1661 unsigned char *buf; | |
665 | 1662 Bytecount allocked; |
428 | 1663 int max_stored; |
1664 int stored; | |
1665 }; | |
1666 | |
771 | 1667 DEFINE_LSTREAM_IMPLEMENTATION ("resizing-buffer", resizing_buffer); |
428 | 1668 |
1669 Lisp_Object | |
1670 make_resizing_buffer_output_stream (void) | |
1671 { | |
793 | 1672 return wrap_lstream (Lstream_new (lstream_resizing_buffer, "w")); |
428 | 1673 } |
1674 | |
665 | 1675 static Bytecount |
462 | 1676 resizing_buffer_writer (Lstream *stream, const unsigned char *data, |
665 | 1677 Bytecount size) |
428 | 1678 { |
1679 struct resizing_buffer_stream *str = RESIZING_BUFFER_STREAM_DATA (stream); | |
1680 DO_REALLOC (str->buf, str->allocked, str->stored + size, unsigned char); | |
1681 memcpy (str->buf + str->stored, data, size); | |
1682 str->stored += size; | |
1683 str->max_stored = max (str->max_stored, str->stored); | |
1684 return size; | |
1685 } | |
1686 | |
1687 static int | |
1688 resizing_buffer_rewinder (Lstream *stream) | |
1689 { | |
1690 RESIZING_BUFFER_STREAM_DATA (stream)->stored = 0; | |
1691 return 0; | |
1692 } | |
1693 | |
1694 static int | |
1695 resizing_buffer_closer (Lstream *stream) | |
1696 { | |
1697 struct resizing_buffer_stream *str = RESIZING_BUFFER_STREAM_DATA (stream); | |
1698 if (str->buf) | |
1699 { | |
4976
16112448d484
Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents:
3263
diff
changeset
|
1700 xfree (str->buf); |
428 | 1701 str->buf = 0; |
1702 } | |
1703 return 0; | |
1704 } | |
1705 | |
1706 unsigned char * | |
1707 resizing_buffer_stream_ptr (Lstream *stream) | |
1708 { | |
1709 return RESIZING_BUFFER_STREAM_DATA (stream)->buf; | |
1710 } | |
1711 | |
788 | 1712 Lisp_Object |
1713 resizing_buffer_to_lisp_string (Lstream *stream) | |
1714 { | |
1715 return make_string (resizing_buffer_stream_ptr (stream), | |
1716 Lstream_byte_count (stream)); | |
1717 } | |
1718 | |
428 | 1719 /*********** write to an unsigned-char dynarr ***********/ |
1720 | |
1721 /* Note: If you have a dynarr whose type is not unsigned_char_dynarr | |
1722 but which is really just an unsigned_char_dynarr (e.g. its type | |
867 | 1723 is Ibyte or Extbyte), just cast to unsigned_char_dynarr. */ |
428 | 1724 |
1725 #define DYNARR_STREAM_DATA(stream) \ | |
1726 LSTREAM_TYPE_DATA (stream, dynarr) | |
1727 | |
1728 struct dynarr_stream | |
1729 { | |
1730 unsigned_char_dynarr *dyn; | |
1731 }; | |
1732 | |
771 | 1733 DEFINE_LSTREAM_IMPLEMENTATION ("dynarr", dynarr); |
428 | 1734 |
1735 Lisp_Object | |
1736 make_dynarr_output_stream (unsigned_char_dynarr *dyn) | |
1737 { | |
793 | 1738 Lisp_Object obj = wrap_lstream (Lstream_new (lstream_dynarr, "w")); |
1739 | |
428 | 1740 DYNARR_STREAM_DATA (XLSTREAM (obj))->dyn = dyn; |
1741 return obj; | |
1742 } | |
1743 | |
665 | 1744 static Bytecount |
462 | 1745 dynarr_writer (Lstream *stream, const unsigned char *data, |
665 | 1746 Bytecount size) |
428 | 1747 { |
1748 struct dynarr_stream *str = DYNARR_STREAM_DATA (stream); | |
1749 Dynarr_add_many (str->dyn, data, size); | |
1750 return size; | |
1751 } | |
1752 | |
1753 static int | |
1754 dynarr_rewinder (Lstream *stream) | |
1755 { | |
1756 Dynarr_reset (DYNARR_STREAM_DATA (stream)->dyn); | |
1757 return 0; | |
1758 } | |
1759 | |
1760 static int | |
2286 | 1761 dynarr_closer (Lstream *UNUSED (stream)) |
428 | 1762 { |
1763 return 0; | |
1764 } | |
1765 | |
1766 /************ read from or write to a Lisp buffer ************/ | |
1767 | |
1768 /* Note: Lisp-buffer read streams never return partial characters, | |
1769 and Lisp-buffer write streams expect to never get partial | |
1770 characters. */ | |
1771 | |
1772 #define LISP_BUFFER_STREAM_DATA(stream) \ | |
1773 LSTREAM_TYPE_DATA (stream, lisp_buffer) | |
1774 | |
1775 struct lisp_buffer_stream | |
1776 { | |
1777 Lisp_Object buffer; | |
1778 Lisp_Object orig_start; | |
1779 /* we use markers to properly deal with insertion/deletion */ | |
1780 Lisp_Object start, end; | |
1781 int flags; | |
1782 }; | |
1783 | |
1204 | 1784 static const struct memory_description lisp_buffer_lstream_description[] = { |
1785 { XD_LISP_OBJECT, offsetof (struct lisp_buffer_stream, buffer) }, | |
1786 { XD_LISP_OBJECT, offsetof (struct lisp_buffer_stream, orig_start) }, | |
1787 { XD_LISP_OBJECT, offsetof (struct lisp_buffer_stream, start) }, | |
1788 { XD_LISP_OBJECT, offsetof (struct lisp_buffer_stream, end) }, | |
1789 { XD_END } | |
1790 }; | |
1791 | |
1792 DEFINE_LSTREAM_IMPLEMENTATION_WITH_DATA ("lisp-buffer", lisp_buffer); | |
428 | 1793 |
1794 static Lisp_Object | |
665 | 1795 make_lisp_buffer_stream_1 (struct buffer *buf, Charbpos start, Charbpos end, |
2367 | 1796 int flags, const Ascbyte *mode) |
428 | 1797 { |
1798 Lstream *lstr; | |
1799 struct lisp_buffer_stream *str; | |
665 | 1800 Charbpos bmin, bmax; |
428 | 1801 int reading = !strcmp (mode, "r"); |
1802 | |
1803 /* Make sure the luser didn't pass "w" in. */ | |
5050
6f2158fa75ed
Fix quick-build, use asserts() in place of ABORT()
Ben Wing <ben@xemacs.org>
parents:
4976
diff
changeset
|
1804 assert (strcmp (mode, "w")); |
428 | 1805 |
1806 if (flags & LSTR_IGNORE_ACCESSIBLE) | |
1807 { | |
1808 bmin = BUF_BEG (buf); | |
1809 bmax = BUF_Z (buf); | |
1810 } | |
1811 else | |
1812 { | |
1813 bmin = BUF_BEGV (buf); | |
1814 bmax = BUF_ZV (buf); | |
1815 } | |
1816 | |
1817 if (start == -1) | |
1818 start = bmin; | |
1819 if (end == -1) | |
1820 end = bmax; | |
1821 assert (bmin <= start); | |
1822 assert (start <= bmax); | |
1823 if (reading) | |
1824 { | |
1825 assert (bmin <= end); | |
1826 assert (end <= bmax); | |
1827 assert (start <= end); | |
1828 } | |
1829 | |
1830 lstr = Lstream_new (lstream_lisp_buffer, mode); | |
1831 str = LISP_BUFFER_STREAM_DATA (lstr); | |
1832 { | |
1833 Lisp_Object marker; | |
793 | 1834 Lisp_Object buffer = wrap_buffer (buf); |
428 | 1835 |
1836 marker = Fmake_marker (); | |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
1837 Fset_marker (marker, make_fixnum (start), buffer); |
428 | 1838 str->start = marker; |
1839 marker = Fmake_marker (); | |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
1840 Fset_marker (marker, make_fixnum (start), buffer); |
428 | 1841 str->orig_start = marker; |
1842 if (reading) | |
1843 { | |
1844 marker = Fmake_marker (); | |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
1845 Fset_marker (marker, make_fixnum (end), buffer); |
428 | 1846 str->end = marker; |
1847 } | |
1848 else | |
1849 str->end = Qnil; | |
1850 str->buffer = buffer; | |
1851 } | |
1852 str->flags = flags; | |
793 | 1853 return wrap_lstream (lstr); |
428 | 1854 } |
1855 | |
1856 Lisp_Object | |
826 | 1857 make_lisp_buffer_input_stream (struct buffer *buf, Charbpos start, |
1858 Charbpos end, int flags) | |
428 | 1859 { |
1860 return make_lisp_buffer_stream_1 (buf, start, end, flags, "r"); | |
1861 } | |
1862 | |
1863 Lisp_Object | |
665 | 1864 make_lisp_buffer_output_stream (struct buffer *buf, Charbpos pos, int flags) |
428 | 1865 { |
1866 Lisp_Object lstr = make_lisp_buffer_stream_1 (buf, pos, 0, flags, "wc"); | |
1867 | |
1868 Lstream_set_character_mode (XLSTREAM (lstr)); | |
1869 return lstr; | |
1870 } | |
1871 | |
665 | 1872 static Bytecount |
867 | 1873 lisp_buffer_reader (Lstream *stream, Ibyte *data, Bytecount size) |
428 | 1874 { |
1875 struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream); | |
665 | 1876 Bytebpos start; |
1877 Bytebpos end; | |
428 | 1878 struct buffer *buf = XBUFFER (str->buffer); |
826 | 1879 Bytecount src_used; |
428 | 1880 |
1881 if (!BUFFER_LIVE_P (buf)) | |
1882 return 0; /* Fut. */ | |
1883 | |
826 | 1884 start = byte_marker_position (str->start); |
1885 end = byte_marker_position (str->end); | |
428 | 1886 if (!(str->flags & LSTR_IGNORE_ACCESSIBLE)) |
1887 { | |
826 | 1888 start = bytebpos_clip_to_bounds (BYTE_BUF_BEGV (buf), start, |
1889 BYTE_BUF_ZV (buf)); | |
1890 end = bytebpos_clip_to_bounds (BYTE_BUF_BEGV (buf), end, | |
1891 BYTE_BUF_ZV (buf)); | |
428 | 1892 } |
1893 | |
826 | 1894 size = copy_buffer_text_out (buf, start, end - start, data, size, |
1895 FORMAT_DEFAULT, Qnil, &src_used); | |
1896 end = start + src_used; | |
428 | 1897 |
1898 if (EQ (buf->selective_display, Qt) && str->flags & LSTR_SELECTIVE) | |
1899 { | |
1900 /* What a kludge. What a kludge. What a kludge. */ | |
867 | 1901 Ibyte *p; |
840 | 1902 for (p = data; p < data + src_used; p++) |
428 | 1903 if (*p == '\r') |
1904 *p = '\n'; | |
1905 } | |
1906 | |
826 | 1907 set_byte_marker_position (str->start, end); |
1908 return size; | |
428 | 1909 } |
1910 | |
665 | 1911 static Bytecount |
867 | 1912 lisp_buffer_writer (Lstream *stream, const Ibyte *data, |
665 | 1913 Bytecount size) |
428 | 1914 { |
1915 struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream); | |
665 | 1916 Charbpos pos; |
428 | 1917 struct buffer *buf = XBUFFER (str->buffer); |
1918 | |
1919 if (!BUFFER_LIVE_P (buf)) | |
1920 return 0; /* Fut. */ | |
1921 | |
1922 pos = marker_position (str->start); | |
1923 pos += buffer_insert_raw_string_1 (buf, pos, data, size, 0); | |
1924 set_marker_position (str->start, pos); | |
1925 return size; | |
1926 } | |
1927 | |
1928 static int | |
1929 lisp_buffer_rewinder (Lstream *stream) | |
1930 { | |
1931 struct lisp_buffer_stream *str = | |
1932 LISP_BUFFER_STREAM_DATA (stream); | |
1933 struct buffer *buf = XBUFFER (str->buffer); | |
1934 long pos = marker_position (str->orig_start); | |
1935 if (!BUFFER_LIVE_P (buf)) | |
1936 return -1; /* Fut. */ | |
1937 if (pos > BUF_ZV (buf)) | |
1938 pos = BUF_ZV (buf); | |
1939 if (pos < marker_position (str->orig_start)) | |
1940 pos = marker_position (str->orig_start); | |
1941 if (MARKERP (str->end) && pos > marker_position (str->end)) | |
1942 pos = marker_position (str->end); | |
1943 set_marker_position (str->start, pos); | |
1944 return 0; | |
1945 } | |
1946 | |
1947 static Lisp_Object | |
1948 lisp_buffer_marker (Lisp_Object stream) | |
1949 { | |
1950 struct lisp_buffer_stream *str = | |
1951 LISP_BUFFER_STREAM_DATA (XLSTREAM (stream)); | |
1952 | |
1204 | 1953 mark_object (str->orig_start); |
428 | 1954 mark_object (str->start); |
1955 mark_object (str->end); | |
1956 return str->buffer; | |
1957 } | |
1958 | |
665 | 1959 Charbpos |
428 | 1960 lisp_buffer_stream_startpos (Lstream *stream) |
1961 { | |
1962 return marker_position (LISP_BUFFER_STREAM_DATA (stream)->start); | |
1963 } | |
1964 | |
1965 | |
1966 /************************************************************************/ | |
1967 /* initialization */ | |
1968 /************************************************************************/ | |
1969 | |
1970 void | |
5157
1fae11d56ad2
redo memory-usage mechanism, add way of dynamically initializing Lisp objects
Ben Wing <ben@xemacs.org>
parents:
5146
diff
changeset
|
1971 syms_of_lstream (void) |
1fae11d56ad2
redo memory-usage mechanism, add way of dynamically initializing Lisp objects
Ben Wing <ben@xemacs.org>
parents:
5146
diff
changeset
|
1972 { |
1fae11d56ad2
redo memory-usage mechanism, add way of dynamically initializing Lisp objects
Ben Wing <ben@xemacs.org>
parents:
5146
diff
changeset
|
1973 INIT_LISP_OBJECT (lstream); |
1fae11d56ad2
redo memory-usage mechanism, add way of dynamically initializing Lisp objects
Ben Wing <ben@xemacs.org>
parents:
5146
diff
changeset
|
1974 } |
1fae11d56ad2
redo memory-usage mechanism, add way of dynamically initializing Lisp objects
Ben Wing <ben@xemacs.org>
parents:
5146
diff
changeset
|
1975 |
1fae11d56ad2
redo memory-usage mechanism, add way of dynamically initializing Lisp objects
Ben Wing <ben@xemacs.org>
parents:
5146
diff
changeset
|
1976 void |
1fae11d56ad2
redo memory-usage mechanism, add way of dynamically initializing Lisp objects
Ben Wing <ben@xemacs.org>
parents:
5146
diff
changeset
|
1977 lstream_objects_create (void) |
1fae11d56ad2
redo memory-usage mechanism, add way of dynamically initializing Lisp objects
Ben Wing <ben@xemacs.org>
parents:
5146
diff
changeset
|
1978 { |
1fae11d56ad2
redo memory-usage mechanism, add way of dynamically initializing Lisp objects
Ben Wing <ben@xemacs.org>
parents:
5146
diff
changeset
|
1979 OBJECT_HAS_PREMETHOD (lstream, disksave); |
1fae11d56ad2
redo memory-usage mechanism, add way of dynamically initializing Lisp objects
Ben Wing <ben@xemacs.org>
parents:
5146
diff
changeset
|
1980 } |
1fae11d56ad2
redo memory-usage mechanism, add way of dynamically initializing Lisp objects
Ben Wing <ben@xemacs.org>
parents:
5146
diff
changeset
|
1981 |
1fae11d56ad2
redo memory-usage mechanism, add way of dynamically initializing Lisp objects
Ben Wing <ben@xemacs.org>
parents:
5146
diff
changeset
|
1982 void |
428 | 1983 lstream_type_create (void) |
1984 { | |
1985 LSTREAM_HAS_METHOD (stdio, reader); | |
1986 LSTREAM_HAS_METHOD (stdio, writer); | |
1987 LSTREAM_HAS_METHOD (stdio, rewinder); | |
1988 LSTREAM_HAS_METHOD (stdio, seekable_p); | |
1989 LSTREAM_HAS_METHOD (stdio, flusher); | |
1990 LSTREAM_HAS_METHOD (stdio, closer); | |
1991 | |
1992 LSTREAM_HAS_METHOD (filedesc, reader); | |
1993 LSTREAM_HAS_METHOD (filedesc, writer); | |
1994 LSTREAM_HAS_METHOD (filedesc, was_blocked_p); | |
1995 LSTREAM_HAS_METHOD (filedesc, rewinder); | |
1996 LSTREAM_HAS_METHOD (filedesc, seekable_p); | |
1997 LSTREAM_HAS_METHOD (filedesc, closer); | |
1998 | |
1999 LSTREAM_HAS_METHOD (lisp_string, reader); | |
2000 LSTREAM_HAS_METHOD (lisp_string, rewinder); | |
2001 LSTREAM_HAS_METHOD (lisp_string, marker); | |
2002 | |
2003 LSTREAM_HAS_METHOD (fixed_buffer, reader); | |
2004 LSTREAM_HAS_METHOD (fixed_buffer, writer); | |
2005 LSTREAM_HAS_METHOD (fixed_buffer, rewinder); | |
2006 | |
2007 LSTREAM_HAS_METHOD (resizing_buffer, writer); | |
2008 LSTREAM_HAS_METHOD (resizing_buffer, rewinder); | |
2009 LSTREAM_HAS_METHOD (resizing_buffer, closer); | |
2010 | |
2011 LSTREAM_HAS_METHOD (dynarr, writer); | |
2012 LSTREAM_HAS_METHOD (dynarr, rewinder); | |
2013 LSTREAM_HAS_METHOD (dynarr, closer); | |
2014 | |
2015 LSTREAM_HAS_METHOD (lisp_buffer, reader); | |
2016 LSTREAM_HAS_METHOD (lisp_buffer, writer); | |
2017 LSTREAM_HAS_METHOD (lisp_buffer, rewinder); | |
2018 LSTREAM_HAS_METHOD (lisp_buffer, marker); | |
2019 } | |
2020 | |
3263 | 2021 #ifndef NEW_GC |
428 | 2022 void |
2023 reinit_vars_of_lstream (void) | |
2024 { | |
2025 int i; | |
2026 | |
2027 for (i = 0; i < countof (Vlstream_free_list); i++) | |
2028 { | |
2029 Vlstream_free_list[i] = Qnil; | |
2030 staticpro_nodump (&Vlstream_free_list[i]); | |
2031 } | |
2032 } | |
3263 | 2033 #endif /* not NEW_GC */ |
428 | 2034 |
2035 void | |
2036 vars_of_lstream (void) | |
2037 { | |
2038 } |