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