annotate src/lstream.c @ 263:727739f917cb r20-5b30

Import from CVS: tag r20-5b30
author cvs
date Mon, 13 Aug 2007 10:24:41 +0200
parents f955c73f5258
children c5d627a313b1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 /* Generic stream implementation.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 Copyright (C) 1995 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 Copyright (C) 1995 Sun Microsystems, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 Copyright (C) 1996 Ben Wing.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 under the terms of the GNU General Public License as published by the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 Free Software Foundation; either version 2, or (at your option) any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 along with XEmacs; see the file COPYING. If not, write to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 Boston, MA 02111-1307, USA. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 /* Synched up with: Not in FSF. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 /* Written by Ben Wing. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 #include <config.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 #include "lisp.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 #include "buffer.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 #include "insdel.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 #include "lstream.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 #include "sysfile.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 #include <errno.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 /* This function provides a generic buffering stream implementation.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 Conceptually, you send data to the stream or read data from the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 stream, not caring what's on the other end of the stream. The
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 other end could be another stream, a file descriptor, a stdio
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 stream, a fixed block of memory, a reallocating block of memory,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 etc. The main purpose of the stream is to provide a standard
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 interface and to do buffering. Macros are defined to read
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 or write characters, so the calling functions do not have to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 worry about blocking data together in order to achieve efficiency.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 /* Note that this object is called "stream" in Lisp but "lstream"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 in C. The reason for this is that "stream" is too generic a name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 for C; too much likelihood of conflict/confusion with C++, etc. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 /* Functions are as follows:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 Lstream *Lstream_new (Lstream_implementation *imp, CONST char *mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 Allocate and return a new Lstream. This function is not
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 really meant to be called directly; rather, each stream type
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 should provide its own stream creation function, which
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 creates the stream and does any other necessary creation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 stuff (e.g. opening a file).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 void Lstream_set_buffering (Lstream *lstr, Lstream_buffering buffering,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 int buffering_size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 Change the buffering of a stream. See lstream.h. By default
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 the buffering is STREAM_BLOCK_BUFFERED.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 int Lstream_flush (Lstream *lstr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 Flush out any pending unwritten data in the stream. Clear
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 any buffered input data. Returns 0 on success, -1 on error.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 int Lstream_putc (Lstream *stream, int c)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 Write out one byte to the stream. This is a macro and so
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 it is very efficient. The C argument is only evaluated once
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 but the STREAM argument is evaluated more than once. Returns
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 0 on success, -1 on error.
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 173
diff changeset
75
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 int Lstream_getc (Lstream *stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 Read one byte from the stream. This is a macro and so it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 is very efficient. The STREAM argument is evaluated more
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 than once. Return value is -1 for EOF or error.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 void Lstream_ungetc (Lstream *stream, int c)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 Push one byte back onto the input queue. This will be the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 next byte read from the stream. Any number of bytes can be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 pushed back and will be read in the reverse order they were
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 pushed back -- most recent first. (This is necessary for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 consistency -- if there are a number of bytes that have been
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 unread and I read and unread a byte, it needs to be the first
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 to be read again.) This is a macro and so it is very
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 efficient. The C argument is only evaluated once but the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 STREAM argument is evaluated more than once.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 int Lstream_fputc (Lstream *stream, int c)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 int Lstream_fgetc (Lstream *stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 void Lstream_fungetc (Lstream *stream, int c)
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 173
diff changeset
95 Function equivalents of the above macros.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 int Lstream_read (Lstream *stream, void *data, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 Read SIZE bytes of DATA from the stream. Return the number of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 bytes read. 0 means EOF. -1 means an error occurred and no
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 bytes were read.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 int Lstream_write (Lstream *stream, void *data, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 Write SIZE bytes of DATA to the stream. Return the number of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 bytes written. -1 means an error occurred and no bytes were
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 written.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 void Lstream_unread (Lstream *stream, void *data, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 Push back SIZE bytes of DATA onto the input queue. The
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 next call to Lstream_read() with the same size will read the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 same bytes back. Note that this will be the case even if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 there is other pending unread data.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 173
diff changeset
113 int Lstream_delete (Lstream *stream)
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 173
diff changeset
114 Frees all memory associated with the stream is freed. Calling
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 173
diff changeset
115 this is not strictly necessary, but it is much more efficient
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 173
diff changeset
116 than having the Lstream be garbage-collected.
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 173
diff changeset
117
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 int Lstream_close (Lstream *stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 Close the stream. All data will be flushed out.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 void Lstream_reopen (Lstream *stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 Reopen a closed stream. This enables I/O on it again.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 This is not meant to be called except from a wrapper routine
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 that reinitializes variables and such -- the close routine
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 may well have freed some necessary storage structures, for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 example.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 void Lstream_rewind (Lstream *stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 Rewind the stream to the beginning.
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 8
diff changeset
130 */
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 8
diff changeset
131
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 8
diff changeset
132 #ifdef MULE
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 8
diff changeset
133 MAC_DEFINE (Emchar, MTlstream_emchar)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 8
diff changeset
134 MAC_DEFINE (int, MTlstream_emcint)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 8
diff changeset
135 #endif
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 MAC_DEFINE (struct lstream *, MTlstream_data)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 static Lisp_Object mark_lstream (Lisp_Object, void (*) (Lisp_Object));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 static void print_lstream (Lisp_Object obj, Lisp_Object printcharfun,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 int escapeflag);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 static void finalize_lstream (void *header, int for_disksave);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 static unsigned int sizeof_lstream (CONST void *header);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION ("stream", lstream,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 mark_lstream, print_lstream,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 finalize_lstream, 0, 0,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 sizeof_lstream, Lstream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 #define DEFAULT_BLOCK_BUFFERING_SIZE 512
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 #define MAX_READ_SIZE 512
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 static Lisp_Object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 mark_lstream (Lisp_Object obj, void (*markobj) (Lisp_Object))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 Lstream *lstr = XLSTREAM (obj);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 if (lstr->imp->marker)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 return (lstr->imp->marker) (obj, markobj);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 return Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 print_lstream (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 Lstream *lstr = XLSTREAM (obj);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 char buf[200];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167
173
8eaf7971accc Import from CVS: tag r20-3b13
cvs
parents: 163
diff changeset
168 sprintf (buf, "#<INTERNAL EMACS BUG (%s lstream) 0x%p>",
8eaf7971accc Import from CVS: tag r20-3b13
cvs
parents: 163
diff changeset
169 lstr->imp->name, lstr);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 write_c_string (buf, printcharfun);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 finalize_lstream (void *header, int for_disksave)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 /* WARNING WARNING WARNING. This function (and all finalize functions)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 may get called more than once on the same object, and may get called
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 (at dump time) on objects that are not being released. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 Lstream *lstr = (Lstream *) header;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 #if 0 /* this may cause weird Broken Pipes? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 if (for_disksave)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 Lstream_pseudo_close (lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 if (lstr->flags & LSTREAM_FL_IS_OPEN)
219
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents: 207
diff changeset
189 {
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents: 207
diff changeset
190 if (for_disksave)
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents: 207
diff changeset
191 {
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents: 207
diff changeset
192 if (lstr->flags & LSTREAM_FL_CLOSE_AT_DISKSAVE)
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents: 207
diff changeset
193 Lstream_close (lstr);
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents: 207
diff changeset
194 }
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents: 207
diff changeset
195 else
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents: 207
diff changeset
196 /* Just close. */
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents: 207
diff changeset
197 Lstream_close (lstr);
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents: 207
diff changeset
198 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 static unsigned int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 sizeof_lstream (CONST void *header)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 CONST Lstream *lstr = (CONST Lstream *) header;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 return sizeof (*lstr) + lstr->imp->size - 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 Lstream_set_buffering (Lstream *lstr, Lstream_buffering buffering,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 int buffering_size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 lstr->buffering = buffering;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 switch (buffering)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 case LSTREAM_UNBUFFERED:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 lstr->buffering_size = 0; break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 case LSTREAM_BLOCK_BUFFERED:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 lstr->buffering_size = DEFAULT_BLOCK_BUFFERING_SIZE; break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 case LSTREAM_BLOCKN_BUFFERED:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 lstr->buffering_size = buffering_size; break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 case LSTREAM_LINE_BUFFERED:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 case LSTREAM_UNLIMITED:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 lstr->buffering_size = INT_MAX; break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 static CONST Lstream_implementation *lstream_types[32];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 static Lisp_Object Vlstream_free_list[32];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 static int lstream_type_count;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 Lstream *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 Lstream_new (CONST Lstream_implementation *imp, CONST char *mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 Lstream *p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 int i;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 for (i = 0; i < lstream_type_count; i++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 if (lstream_types[i] == imp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 if (i == lstream_type_count)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 assert (lstream_type_count < countof (lstream_types));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 lstream_types[lstream_type_count] = imp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 Vlstream_free_list[lstream_type_count] =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 make_lcrecord_list (sizeof (*p) + imp->size - 1,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 lrecord_lstream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 lstream_type_count++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 p = XLSTREAM (allocate_managed_lcrecord (Vlstream_free_list[i]));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 /* Zero it out, except the header. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 memset ((char *) p + sizeof (p->header), 0,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 sizeof (*p) - sizeof (p->header) + imp->size - 1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 p->imp = imp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 Lstream_set_buffering (p, LSTREAM_BLOCK_BUFFERED, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 p->flags = LSTREAM_FL_IS_OPEN;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 /* convert mode (one of "r", "w", "rc", "wc") to p->flags */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 assert (mode[0] == 'r' || mode[0] == 'w');
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 assert (mode[1] == 'c' || mode[1] == '\0');
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 p->flags |= (mode[0] == 'r' ? LSTREAM_FL_READ : LSTREAM_FL_WRITE);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 if (mode[1] == 'c')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 p->flags |= LSTREAM_FL_NO_PARTIAL_CHARS;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 return p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 Lstream_set_character_mode (Lstream *lstr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 lstr->flags |= LSTREAM_FL_NO_PARTIAL_CHARS;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 Lstream_delete (Lstream *lstr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 int i;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 Lisp_Object val = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 XSETLSTREAM (val, lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 for (i = 0; i < lstream_type_count; i++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 if (lstream_types[i] == lstr->imp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 free_managed_lcrecord (Vlstream_free_list[i], val);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 abort ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 signal_simple_internal_error (CONST char *reason, Lisp_Object obj)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 signal_simple_error (reason, obj);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 Lstream_reopen (Lstream *lstr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 if (lstr->flags & LSTREAM_FL_IS_OPEN)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 Lisp_Object obj = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 XSETLSTREAM (obj, lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 signal_simple_internal_error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 ("Internal error: lstream already open", obj);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 lstr->flags |= LSTREAM_FL_IS_OPEN;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 /* Attempt to flush out all of the buffered data for writing. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 Lstream_flush_out (Lstream *lstr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 int num_written;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 while (lstr->out_buffer_ind > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 int size = lstr->out_buffer_ind;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 if (! (lstr->flags & LSTREAM_FL_IS_OPEN))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 Lisp_Object obj = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 XSETLSTREAM (obj, lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 signal_simple_internal_error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 ("Internal error: lstream not open", obj);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 if (! (lstr->flags & LSTREAM_FL_WRITE))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 Lisp_Object obj = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 XSETLSTREAM (obj, lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 signal_simple_internal_error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 ("Internal error: lstream not open for writing", obj);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 if (!lstr->imp->writer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 Lisp_Object obj = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 XSETLSTREAM (obj, lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 signal_simple_internal_error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 ("Internal error: lstream has no writer", obj);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 if (lstr->flags & LSTREAM_FL_NO_PARTIAL_CHARS)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 /* It's quite possible for us to get passed an incomplete
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 character at the end. We need to spit back that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 incomplete character. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 CONST unsigned char *data = lstr->out_buffer;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 CONST unsigned char *dataend = data + size - 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 assert (size > 0); /* safety check ... */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 /* Optimize the most common case. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 if (!BYTE_ASCII_P (*dataend))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 /* Go back to the beginning of the last (and possibly partial)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 character, and bump forward to see if the character is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 complete. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 VALIDATE_CHARPTR_BACKWARD (dataend);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 if (dataend + REP_BYTES_BY_FIRST_BYTE (*dataend) != data + size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 /* If not, chop the size down to ignore the last char
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 and stash it away for next time. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 size = dataend - data;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 /* If we don't even have one character to write, then just
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 skip out. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 if (size == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 num_written =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 (lstr->imp->writer) (lstr, lstr->out_buffer, size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 if (num_written == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 /* If nothing got written, then just hold the data. This may
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 occur, for example, if this stream does non-blocking I/O;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 the attempt to write the data might have resulted in an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 EWOULDBLOCK error. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 else if (num_written >= lstr->out_buffer_ind)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 lstr->out_buffer_ind = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 else if (num_written > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 memmove (lstr->out_buffer, lstr->out_buffer + num_written,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 lstr->out_buffer_ind - num_written);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 lstr->out_buffer_ind -= num_written;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 /* If error, just hold the data, for similar reasons as above. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 return -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 if (lstr->imp->flusher)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 return (lstr->imp->flusher) (lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 Lstream_flush (Lstream *lstr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 if (Lstream_flush_out (lstr) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 return -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 /* clear out buffered data */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 lstr->in_buffer_current = lstr->in_buffer_ind = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 lstr->unget_buffer_ind = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 /* We want to add NUM characters. This function ensures that the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 buffer is large enough for this (per the buffering size specified
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 in the stream) and returns the number of characters we can
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 actually write. If FORCE is set, ignore the buffering size
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 and go ahead and make space for all the chars even if it exceeds
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 the buffering size. (This is used to deal with the possibility
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 that the stream writer might refuse to write any bytes now, e.g.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 if it's getting EWOULDBLOCK errors. We have to keep stocking them
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 up until they can be written, so as to avoid losing data. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 Lstream_adding (Lstream *lstr, int num, int force)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 /* Compute the size that the outbuffer needs to be after the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 chars are added. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 int size_needed = max (lstr->out_buffer_size,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 num + lstr->out_buffer_ind);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 /* Maybe chop it down so that we don't buffer more characters
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431 than our advertised buffering size. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 if (!force)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 size_needed = min (lstr->buffering_size, size_needed);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 DO_REALLOC (lstr->out_buffer, lstr->out_buffer_size,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 size_needed, unsigned char);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 /* There might be more data buffered than the buffering size,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437 so make sure we don't return a negative number here. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438 return max (0, min (num, size_needed - lstr->out_buffer_ind));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 /* Like Lstream_write(), but does not handle line-buffering correctly. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 Lstream_write_1 (Lstream *lstr, CONST void *data, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 {
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 173
diff changeset
446 CONST unsigned char *p = (CONST unsigned char *) data;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447 int off = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448 if (! (lstr->flags & LSTREAM_FL_IS_OPEN))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 Lisp_Object obj = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451 XSETLSTREAM (obj, lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 signal_simple_internal_error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 ("Internal error: lstream not open", obj);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455 if (! (lstr->flags & LSTREAM_FL_WRITE))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457 Lisp_Object obj = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458 XSETLSTREAM (obj, lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459 signal_simple_internal_error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 ("Internal error: lstream not open for writing", obj);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 int couldnt_write_last_time = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465 while (1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467 /* Figure out how much we can add to the buffer */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468 int chunk = Lstream_adding (lstr, size, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 if (chunk == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 if (couldnt_write_last_time)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 /* Ung, we ran out of space and tried to flush
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 the buffer, but it didn't work because the stream
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 writer is refusing to accept any data. So we
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475 just have to squirrel away all the rest of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476 stuff. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 chunk = Lstream_adding (lstr, size, 1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479 couldnt_write_last_time = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 /* Do it. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 if (chunk > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484 memcpy (lstr->out_buffer + lstr->out_buffer_ind, p + off, chunk);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 lstr->out_buffer_ind += chunk;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 lstr->byte_count += chunk;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 size -= chunk;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488 off += chunk;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 /* If the buffer is full and we have more to add, flush it out. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491 if (size > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493 if (Lstream_flush_out (lstr) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495 if (off == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 return -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 return off;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 return off;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 /* If the stream is not line-buffered, then we can just call
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 Lstream_write_1(), which writes in chunks. Otherwise, we
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 repeatedly call Lstream_putc(), which knows how to handle
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511 line buffering. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514 Lstream_write (Lstream *lstr, CONST void *data, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516 int i;
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 173
diff changeset
517 CONST unsigned char *p = (CONST unsigned char *) data;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
519 assert (size >= 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
520 if (size == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521 return size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522 if (lstr->buffering != LSTREAM_LINE_BUFFERED)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
523 return Lstream_write_1 (lstr, data, size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
524 for (i = 0; i < size; i++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
525 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526 if (Lstream_putc (lstr, p[i]) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
527 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
528 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
529 return i == 0 ? -1 : 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
530 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
531
263
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 241
diff changeset
532 int
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 241
diff changeset
533 Lstream_was_blocked_p (Lstream *lstr)
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 241
diff changeset
534 {
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 241
diff changeset
535 if (lstr->imp->was_blocked_p)
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 241
diff changeset
536 return (lstr->imp->was_blocked_p) (lstr);
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 241
diff changeset
537 else
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 241
diff changeset
538 return 0;
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 241
diff changeset
539 }
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 241
diff changeset
540
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
542 Lstream_raw_read (Lstream *lstr, unsigned char *buffer, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544 if (! (lstr->flags & LSTREAM_FL_IS_OPEN))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
545 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546 Lisp_Object obj = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
547 XSETLSTREAM (obj, lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
548 signal_simple_internal_error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
549 ("Internal error: lstream not open", obj);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
550 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
551 if (! (lstr->flags & LSTREAM_FL_READ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
552 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
553 Lisp_Object obj = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
554 XSETLSTREAM (obj, lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
555 signal_simple_internal_error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
556 ("Internal error: lstream not open for reading", obj);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
557 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
558 if (!lstr->imp->reader)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
559 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
560 Lisp_Object obj = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
561 XSETLSTREAM (obj, lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
562 signal_simple_internal_error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
563 ("Internal error: lstream has no reader", obj);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
564 }
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 173
diff changeset
565
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
566 return (lstr->imp->reader) (lstr, buffer, size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
567 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
568
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
569 /* Assuming the buffer is empty, fill it up again. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
570
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
571 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
572 Lstream_read_more (Lstream *lstr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
573 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
574 #if 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
575 int size_needed = max (1, min (MAX_READ_SIZE, lstr->buffering_size));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
576 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
577 /* If someone requested a larger buffer size, so be it! */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
578 int size_needed = max (1, lstr->buffering_size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
579 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
580 int size_gotten;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
581
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
582 DO_REALLOC (lstr->in_buffer, lstr->in_buffer_size,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
583 size_needed, unsigned char);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
584 size_gotten = Lstream_raw_read (lstr, lstr->in_buffer, size_needed);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
585 lstr->in_buffer_current = max (0, size_gotten);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
586 lstr->in_buffer_ind = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
587 return size_gotten < 0 ? -1 : size_gotten;
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 173
diff changeset
588 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
589
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
590 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
591 Lstream_read (Lstream *lstr, void *data, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
592 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
593 unsigned char *p = (unsigned char *) data;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
594 int off = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
595 int chunk;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
596 int error_occurred = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
597
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
598 assert (size >= 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
599 if (size == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
600 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
601
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
602 /* First try to get some data from the unget buffer */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
603 chunk = min (size, lstr->unget_buffer_ind);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
604 if (chunk > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
605 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
606 /* The bytes come back in reverse order. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
607 for (; off < chunk; off++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
608 p[off] = lstr->unget_buffer[--lstr->unget_buffer_ind];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
609 lstr->byte_count += chunk;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
610 size -= chunk;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
611 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
612
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
613 while (size > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
614 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
615 /* Take whatever we can from the in buffer */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
616 chunk = min (size, lstr->in_buffer_current - lstr->in_buffer_ind);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
617 if (chunk > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
618 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
619 memcpy (p + off, lstr->in_buffer + lstr->in_buffer_ind, chunk);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
620 lstr->in_buffer_ind += chunk;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
621 lstr->byte_count += chunk;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
622 size -= chunk;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
623 off += chunk;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
624 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
625
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
626 /* If we need some more, try to get some more from the stream's end */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
627 if (size > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
628 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
629 int retval = Lstream_read_more (lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
630 if (retval < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
631 error_occurred = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
632 if (retval <= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
633 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
634 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
635 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
636
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
637 /* #### Beware of OFF ending up 0. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
638 if ((lstr->flags & LSTREAM_FL_NO_PARTIAL_CHARS) && off > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
639 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
640 /* It's quite possible for us to get passed an incomplete
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
641 character at the end. We need to spit back that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
642 incomplete character. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
643 CONST unsigned char *dataend = p + off - 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
644 /* Optimize the most common case. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
645 if (!BYTE_ASCII_P (*dataend))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
646 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
647 /* Go back to the beginning of the last (and possibly partial)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
648 character, and bump forward to see if the character is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
649 complete. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
650 VALIDATE_CHARPTR_BACKWARD (dataend);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
651 if (dataend + REP_BYTES_BY_FIRST_BYTE (*dataend) != p + off)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
652 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
653 int newoff = dataend - p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
654 /* If not, chop the size down to ignore the last char
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
655 and stash it away for next time. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
656 Lstream_unread (lstr, dataend, off - newoff);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
657 off = newoff;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
658 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
659 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
660 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
661
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
662 return ((off == 0 && error_occurred) ? -1 : off);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
663 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
664
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
665 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
666 Lstream_unread (Lstream *lstr, CONST void *data, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
667 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
668 int i;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
669 unsigned char *p = (unsigned char *) data;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
670
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
671 /* Make sure buffer is big enough */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
672
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
673 DO_REALLOC (lstr->unget_buffer, lstr->unget_buffer_size,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
674 lstr->unget_buffer_ind + size, unsigned char);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
675
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
676 /* Bytes have to go on in reverse order -- they are reversed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
677 again when read back. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
678 for (i = size - 1; i >= 0; i--)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
679 lstr->unget_buffer[lstr->unget_buffer_ind++] = p[i];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
680 lstr->byte_count -= size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
681 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
682
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
683 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
684 Lstream_rewind (Lstream *lstr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
685 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
686 if (!lstr->imp->rewinder)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
687 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
688 Lisp_Object obj = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
689 XSETLSTREAM (obj, lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
690 signal_simple_internal_error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
691 ("Internal error: lstream has no rewinder", obj);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
692 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
693 if (Lstream_flush (lstr) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
694 return -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
695 lstr->byte_count = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
696 return (lstr->imp->rewinder) (lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
697 }
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 173
diff changeset
698
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
699 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
700 Lstream_seekable_p (Lstream *lstr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
701 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
702 if (!lstr->imp->rewinder)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
703 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
704 if (!lstr->imp->seekable_p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
705 return 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
706 return (lstr->imp->seekable_p) (lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
707 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
708
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
709 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
710 Lstream_pseudo_close (Lstream *lstr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
711 {
74
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
712 int rc;
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
713
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
714 if (!lstr->flags & LSTREAM_FL_IS_OPEN)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
715 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
716 Lisp_Object obj = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
717 XSETLSTREAM (obj, lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
718 signal_simple_internal_error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
719 ("Internal error: lstream is not open", obj);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
720 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
721
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
722 /* don't check errors here -- best not to risk file descriptor loss */
74
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
723 rc = Lstream_flush (lstr);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
724
74
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
725 return rc;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
726 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
727
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
728 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
729 Lstream_close (Lstream *lstr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
730 {
82
6a378aca36af Import from CVS: tag r20-0b91
cvs
parents: 74
diff changeset
731 int rc = 0;
74
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
732
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
733 if (lstr->flags & LSTREAM_FL_IS_OPEN)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
734 {
74
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
735 rc = Lstream_pseudo_close (lstr);
207
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
736 /*
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
737 * We used to return immediately if the closer method reported
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
738 * failure, leaving the stream open. But this is no good, for
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
739 * the following reasons.
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
740 *
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
741 * 1. The finalizer method used in GC makes no provision for
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
742 * failure, so we must not return without freeing buffer
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
743 * memory.
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
744 *
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
745 * 2. The closer method may have already freed some memory
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
746 * used for I/O in this stream. E.g. encoding_closer frees
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
747 * ENCODING_STREAM_DATA(stream)->runoff. If a writer method
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
748 * tries to use this buffer later, it will write into memory
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
749 * that may have been allocated elsewhere. Sometime later
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
750 * you will see a sign that says "Welcome to Crash City."
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
751 *
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
752 * 3. The closer can report failure if a flush fails in the
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
753 * other stream in a MULE encoding/decoding stream pair.
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
754 * The other stream in the pair is closed, but returning
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
755 * early leaves the current stream open. If we try to
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
756 * flush the current stream later, we will crash when the
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
757 * flusher notices that the other end stream is closed.
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
758 *
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
759 * So, we no longer abort the close if the closer method
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
760 * reports some kind of failure. We still report the failure
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
761 * to the caller.
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
762 */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
763 if (lstr->imp->closer)
207
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
764 if ((lstr->imp->closer) (lstr) < 0)
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 185
diff changeset
765 rc = -1;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
766 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
767
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
768 lstr->flags &= ~LSTREAM_FL_IS_OPEN;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
769 lstr->byte_count = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
770 /* Note that Lstream_flush() reset all the buffer indices. That way,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
771 the next call to Lstream_putc(), Lstream_getc(), or Lstream_ungetc()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
772 on a closed stream will call into the function equivalents, which will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
773 cause an error. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
774
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
775 /* We set the pointers to 0 so that we don't lose when this function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
776 is called more than once on the same object */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
777 if (lstr->out_buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
778 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
779 xfree (lstr->out_buffer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
780 lstr->out_buffer = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
781 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
782 if (lstr->in_buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
783 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
784 xfree (lstr->in_buffer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
785 lstr->in_buffer = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
786 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
787 if (lstr->unget_buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
788 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
789 xfree (lstr->unget_buffer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
790 lstr->unget_buffer = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
791 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
792
74
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
793 return rc;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
794 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
795
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
796 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
797 Lstream_fputc (Lstream *lstr, int c)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
798 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
799 unsigned char ch = (unsigned char) c;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
800 int retval = Lstream_write_1 (lstr, &ch, 1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
801 if (retval >= 0 && lstr->buffering == LSTREAM_LINE_BUFFERED && ch == '\n')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
802 return Lstream_flush_out (lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
803 return retval < 0 ? -1 : 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
804 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
805
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
806 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
807 Lstream_fgetc (Lstream *lstr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
808 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
809 unsigned char ch;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
810 if (Lstream_read (lstr, &ch, 1) <= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
811 return -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
812 return ch;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
813 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
814
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
815 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
816 Lstream_fungetc (Lstream *lstr, int c)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
817 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
818 unsigned char ch = (unsigned char) c;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
819 Lstream_unread (lstr, &ch, 1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
820 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
821
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
822
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
823 /************************ some stream implementations *********************/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
824
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
825 /*********** a stdio stream ***********/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
826
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
827 struct stdio_stream
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
828 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
829 FILE *file;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
830 int closing;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
831 };
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
832
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
833 #define STDIO_STREAM_DATA(stream) LSTREAM_TYPE_DATA (stream, stdio)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
834
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
835 DEFINE_LSTREAM_IMPLEMENTATION ("stdio", lstream_stdio,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
836 sizeof (struct stdio_stream));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
837
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
838 static Lisp_Object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
839 make_stdio_stream_1 (FILE *stream, int flags, CONST char *mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
840 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
841 Lisp_Object obj = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
842 Lstream *lstr = Lstream_new (lstream_stdio, mode);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
843 struct stdio_stream *str = STDIO_STREAM_DATA (lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
844 str->file = stream;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
845 str->closing = flags & LSTR_CLOSING;
219
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents: 207
diff changeset
846 lstr->flags |= LSTREAM_FL_CLOSE_AT_DISKSAVE;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
847 XSETLSTREAM (obj, lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
848 return obj;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
849 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
850
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
851 Lisp_Object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
852 make_stdio_input_stream (FILE *stream, int flags)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
853 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
854 return make_stdio_stream_1 (stream, flags, "r");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
855 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
856
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
857 Lisp_Object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
858 make_stdio_output_stream (FILE *stream, int flags)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
859 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
860 return make_stdio_stream_1 (stream, flags, "w");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
861 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
862
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
863 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
864 stdio_reader (Lstream *stream, unsigned char *data, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
865 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
866 struct stdio_stream *str = STDIO_STREAM_DATA (stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
867 size_t val = fread (data, 1, (size_t) size, str->file);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
868 if (!val && ferror (str->file))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
869 return -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
870 return (int) val;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
871 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
872
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
873 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
874 stdio_writer (Lstream *stream, CONST unsigned char *data, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
875 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
876 struct stdio_stream *str = STDIO_STREAM_DATA (stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
877 size_t val = fwrite (data, 1, (size_t) size, str->file);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
878 if (!val && ferror (str->file))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
879 return -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
880 return (int) val;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
881 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
882
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
883 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
884 stdio_rewinder (Lstream *stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
885 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
886 rewind (STDIO_STREAM_DATA (stream)->file);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
887 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
888 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
889
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
890 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
891 stdio_seekable_p (Lstream *stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
892 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
893 struct stdio_stream *str = STDIO_STREAM_DATA (stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
894 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
895 struct stat lestat;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
896
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
897 if (fstat (fileno (str->file), &lestat) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
898 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
899 return S_ISREG (lestat.st_mode);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
900 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
901 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
902
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
903 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
904 stdio_flusher (Lstream *stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
905 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
906 struct stdio_stream *str = STDIO_STREAM_DATA (stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
907 if (stream->flags & LSTREAM_FL_WRITE)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
908 return fflush (str->file);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
909 else
241
f955c73f5258 Import from CVS: tag r20-5b19
cvs
parents: 219
diff changeset
910 /* call fpurge? Only exists on some systems. #### Why not add a
f955c73f5258 Import from CVS: tag r20-5b19
cvs
parents: 219
diff changeset
911 configure check for HAVE_FPURGE and utilize it on systems that
f955c73f5258 Import from CVS: tag r20-5b19
cvs
parents: 219
diff changeset
912 support it? --hniksic */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
913 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
914 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
915
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
916 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
917 stdio_closer (Lstream *stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
918 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
919 struct stdio_stream *str = STDIO_STREAM_DATA (stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
920 if (str->closing)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
921 return fclose (str->file);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
922 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
923 if (stream->flags & LSTREAM_FL_WRITE)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
924 return fflush (str->file);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
925 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
926 /* call fpurge? Only exists on some systems. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
927 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
928 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
929
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
930 /*********** a file descriptor ***********/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
931
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
932 struct filedesc_stream
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
933 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
934 int fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
935 int pty_max_bytes;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
936 Bufbyte eof_char;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
937 int starting_pos;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
938 int current_pos;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
939 int end_pos;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
940 int chars_sans_newline;
74
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
941 unsigned int closing :1;
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
942 unsigned int allow_quit :1;
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
943 unsigned int blocked_ok :1;
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
944 unsigned int pty_flushing :1;
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
945 unsigned int blocking_error_p :1;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
946 };
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
947
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
948 #define FILEDESC_STREAM_DATA(stream) LSTREAM_TYPE_DATA (stream, filedesc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
949
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
950 DEFINE_LSTREAM_IMPLEMENTATION ("filedesc", lstream_filedesc,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
951 sizeof (struct filedesc_stream));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
952
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
953 /* Make a stream that reads from or writes to a file descriptor FILEDESC.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
954 OFFSET is the offset from the *current* file pointer that the reading
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
955 should start at. COUNT is the number of bytes to be read (it is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
956 ignored when writing); -1 for unlimited. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
957 static Lisp_Object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
958 make_filedesc_stream_1 (int filedesc, int offset, int count, int flags,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
959 CONST char *mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
960 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
961 Lisp_Object obj = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
962 Lstream *lstr = Lstream_new (lstream_filedesc, mode);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
963 struct filedesc_stream *fstr = FILEDESC_STREAM_DATA (lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
964 fstr->fd = filedesc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
965 fstr->closing = !!(flags & LSTR_CLOSING);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
966 fstr->allow_quit = !!(flags & LSTR_ALLOW_QUIT);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
967 fstr->blocked_ok = !!(flags & LSTR_BLOCKED_OK);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
968 fstr->pty_flushing = !!(flags & LSTR_PTY_FLUSHING);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
969 fstr->blocking_error_p = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
970 fstr->chars_sans_newline = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
971 fstr->starting_pos = lseek (filedesc, offset, SEEK_CUR);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
972 if (fstr->starting_pos < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
973 fstr->current_pos = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
974 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
975 fstr->current_pos = fstr->starting_pos;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
976 if (count < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
977 fstr->end_pos = -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
978 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
979 fstr->end_pos = fstr->starting_pos + count;
219
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents: 207
diff changeset
980 lstr->flags |= LSTREAM_FL_CLOSE_AT_DISKSAVE;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
981 XSETLSTREAM (obj, lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
982 return obj;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
983 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
984
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
985 Lisp_Object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
986 make_filedesc_input_stream (int filedesc, int offset, int count, int flags)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
987 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
988 return make_filedesc_stream_1 (filedesc, offset, count, flags, "r");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
989 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
990
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
991 Lisp_Object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
992 make_filedesc_output_stream (int filedesc, int offset, int count, int flags)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
993 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
994 return make_filedesc_stream_1 (filedesc, offset, count, flags, "w");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
995 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
996
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
997 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
998 filedesc_reader (Lstream *stream, unsigned char *data, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
999 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1000 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1001 if (str->end_pos >= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1002 size = min (size, str->end_pos - str->current_pos);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1003 size = (str->allow_quit ? read_allowing_quit : read) (str->fd, data, size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1004 if (size > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1005 str->current_pos += size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1006 return size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1007 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1008
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1009 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1010 errno_would_block_p (int val)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1011 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1012 #ifdef EWOULDBLOCK
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1013 if (val == EWOULDBLOCK)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1014 return 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1015 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1016 #ifdef EAGAIN
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1017 if (val == EAGAIN)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1018 return 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1019 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1020 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1021 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1022
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1023 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1024 filedesc_writer (Lstream *stream, CONST unsigned char *data, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1025 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1026 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1027 int retval;
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 173
diff changeset
1028 int need_newline = 0;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1029
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1030 /* This function would be simple if it were not for the blasted
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1031 PTY max-bytes stuff. Why the hell can't they just have written
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1032 the PTY drivers right so this problem doesn't exist?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1033
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1034 Maybe all the PTY crap here should be moved into another stream
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1035 that does nothing but periodically insert EOF's as necessary. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1036 if (str->pty_flushing)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1037 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1038 /* To make life easy, only send out one line at the most. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1039 CONST unsigned char *ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1040
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents: 104
diff changeset
1041 ptr = (CONST unsigned char *) memchr (data, '\n', size);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1042 if (ptr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1043 need_newline = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1044 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1045 ptr = data + size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1046 if (ptr - data >= str->pty_max_bytes - str->chars_sans_newline)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1047 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1048 ptr = data + str->pty_max_bytes - str->chars_sans_newline;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1049 need_newline = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1050 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1051 size = ptr - data;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1052 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1053
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1054 /**** start of non-PTY-crap ****/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1055 if (size > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1056 retval = ((str->allow_quit ? write_allowing_quit : write)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1057 (str->fd, data, size));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1058 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1059 retval = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1060 if (retval < 0 && errno_would_block_p (errno) && str->blocked_ok)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1061 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1062 str->blocking_error_p = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1063 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1064 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1065 str->blocking_error_p = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1066 if (retval < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1067 return retval;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1068 /**** end non-PTY-crap ****/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1069
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1070 if (str->pty_flushing)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1071 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1072 str->chars_sans_newline += retval;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1073 /* Note that a newline was not among the bytes written out.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1074 Add to the number of non-newline bytes written out,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1075 and flush with an EOF if necessary. Be careful to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1076 keep track of write errors as we go along and look
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1077 out for EWOULDBLOCK. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1078 if (str->chars_sans_newline >= str->pty_max_bytes)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1079 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1080 int retval2 = ((str->allow_quit ? write_allowing_quit : write)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1081 (str->fd, &str->eof_char, 1));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1082 if (retval2 > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1083 str->chars_sans_newline = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1084 else if (retval2 < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1085 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1086 /* Error writing the EOF char. If nothing got written,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1087 then treat this as an error -- either return an error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1088 condition or set the blocking-error flag. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1089 if (retval == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1090 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1091 if (errno_would_block_p (errno) && str->blocked_ok)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1092 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1093 str->blocking_error_p = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1094 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1095 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1096 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1097 return retval2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1098 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1099 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1100 return retval;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1101 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1102 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1103 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1104
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1105 /* The need_newline flag is necessary because otherwise when the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1106 first byte is a newline, we'd get stuck never writing anything
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1107 in pty-flushing mode. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1108 if (need_newline)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1109 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1110 Bufbyte nl = '\n';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1111 int retval2 = ((str->allow_quit ? write_allowing_quit : write)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1112 (str->fd, &nl, 1));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1113 if (retval2 > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1114 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1115 str->chars_sans_newline = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1116 retval++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1117 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1118 else if (retval2 < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1119 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1120 /* Error writing the newline char. If nothing got written,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1121 then treat this as an error -- either return an error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1122 condition or set the blocking-error flag. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1123 if (retval == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1124 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1125 if (errno_would_block_p (errno) && str->blocked_ok)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1126 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1127 str->blocking_error_p = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1128 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1129 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1130 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1131 return retval2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1132 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1133 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1134 return retval;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1135 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1136 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1137
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1138 return retval;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1139 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1140
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1141 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1142 filedesc_rewinder (Lstream *stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1143 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1144 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1145 if (str->starting_pos < 0 ||
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1146 lseek (FILEDESC_STREAM_DATA (stream)->fd, str->starting_pos,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1147 SEEK_SET) == -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1148 return -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1149 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1150 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1151 str->current_pos = str->starting_pos;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1152 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1153 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1154 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1155
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1156 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1157 filedesc_seekable_p (Lstream *stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1158 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1159 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1160 if (str->starting_pos < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1161 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1162 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1163 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1164 struct stat lestat;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1165
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1166 if (fstat (str->fd, &lestat) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1167 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1168 return S_ISREG (lestat.st_mode);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1169 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1170 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1171
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1172 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1173 filedesc_closer (Lstream *stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1174 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1175 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1176 if (str->closing)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1177 return close (str->fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1178 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1179 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1180 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1181
263
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 241
diff changeset
1182 static int
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 241
diff changeset
1183 filedesc_was_blocked_p (Lstream *stream)
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 241
diff changeset
1184 {
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 241
diff changeset
1185 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream);
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 241
diff changeset
1186 return str->blocking_error_p;
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 241
diff changeset
1187 }
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 241
diff changeset
1188
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1189 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1190 filedesc_stream_set_pty_flushing (Lstream *stream, int pty_max_bytes,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1191 Bufbyte eof_char)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1192 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1193 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1194 str->pty_max_bytes = pty_max_bytes;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1195 str->eof_char = eof_char;
263
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 241
diff changeset
1196 str->pty_flushing = 1;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1197 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1198
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1199 int
263
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 241
diff changeset
1200 filedesc_stream_fd (Lstream *stream)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1201 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1202 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream);
263
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 241
diff changeset
1203 return str->fd;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1204 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1205
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1206 /*********** read from a Lisp string ***********/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1207
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1208 #define LISP_STRING_STREAM_DATA(stream) LSTREAM_TYPE_DATA (stream, lisp_string)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1209
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1210 struct lisp_string_stream
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1211 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1212 Lisp_Object obj;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1213 Bytecount init_offset;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1214 Bytecount offset, end;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1215 };
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1216
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1217 DEFINE_LSTREAM_IMPLEMENTATION ("lisp-string", lstream_lisp_string,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1218 sizeof (struct lisp_string_stream));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1219
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1220 Lisp_Object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1221 make_lisp_string_input_stream (Lisp_Object string, Bytecount offset,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1222 Bytecount len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1223 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1224 Lisp_Object obj = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1225 Lstream *lstr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1226 struct lisp_string_stream *str;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1227
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1228 CHECK_STRING (string);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1229 if (len < 0)
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 8
diff changeset
1230 len = XSTRING_LENGTH (string) - offset;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1231 assert (offset >= 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1232 assert (len >= 0);
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 8
diff changeset
1233 assert (offset + len <= XSTRING_LENGTH (string));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1234
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1235 lstr = Lstream_new (lstream_lisp_string, "r");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1236 str = LISP_STRING_STREAM_DATA (lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1237 str->offset = offset;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1238 str->end = offset + len;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1239 str->init_offset = offset;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1240 str->obj = string;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1241 XSETLSTREAM (obj, lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1242 return obj;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1243 }
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 173
diff changeset
1244
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1245 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1246 lisp_string_reader (Lstream *stream, unsigned char *data, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1247 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1248 struct lisp_string_stream *str = LISP_STRING_STREAM_DATA (stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1249 /* Don't lose if the string shrank past us ... */
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 8
diff changeset
1250 Bytecount offset = min (str->offset, XSTRING_LENGTH (str->obj));
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 8
diff changeset
1251 Bufbyte *strstart = XSTRING_DATA (str->obj);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1252 Bufbyte *start = strstart + offset;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1253
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1254 /* ... or if someone changed the string and we ended up in the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1255 middle of a character. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1256 /* Being in the middle of a character is `normal' unless
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1257 LSTREAM_NO_PARTIAL_CHARS - mrb */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1258 if (stream->flags & LSTREAM_FL_NO_PARTIAL_CHARS)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1259 VALIDATE_CHARPTR_BACKWARD (start);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1260 offset = start - strstart;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1261 size = min (size, str->end - offset);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1262 assert (size >= 0); /* paranoia */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1263 memcpy (data, start, size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1264 str->offset = offset + size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1265 return size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1266 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1267
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1268 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1269 lisp_string_rewinder (Lstream *stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1270 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1271 struct lisp_string_stream *str = LISP_STRING_STREAM_DATA (stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1272 int pos = str->init_offset;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1273 if (pos > str->end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1274 pos = str->end;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1275 /* Don't lose if the string shrank past us ... */
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 8
diff changeset
1276 pos = min (pos, XSTRING_LENGTH (str->obj));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1277 /* ... or if someone changed the string and we ended up in the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1278 middle of a character. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1279 {
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 8
diff changeset
1280 Bufbyte *strstart = XSTRING_DATA (str->obj);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1281 Bufbyte *start = strstart + pos;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1282 VALIDATE_CHARPTR_BACKWARD (start);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1283 pos = start - strstart;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1284 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1285 str->offset = pos;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1286 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1287 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1288
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1289 static Lisp_Object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1290 lisp_string_marker (Lisp_Object stream, void (*markobj) (Lisp_Object))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1291 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1292 struct lisp_string_stream *str = LISP_STRING_STREAM_DATA (XLSTREAM (stream));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1293 return str->obj;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1294 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1295
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1296 /*********** a fixed buffer ***********/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1297
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1298 #define FIXED_BUFFER_STREAM_DATA(stream) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1299 LSTREAM_TYPE_DATA (stream, fixed_buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1300
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1301 struct fixed_buffer_stream
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1302 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1303 CONST unsigned char *inbuf;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1304 unsigned char *outbuf;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1305 int size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1306 int offset;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1307 };
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1308
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1309 DEFINE_LSTREAM_IMPLEMENTATION ("fixed-buffer", lstream_fixed_buffer,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1310 sizeof (struct fixed_buffer_stream));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1311
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1312 Lisp_Object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1313 make_fixed_buffer_input_stream (CONST unsigned char *buf, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1314 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1315 Lisp_Object obj = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1316 Lstream *lstr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1317 struct fixed_buffer_stream *str;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1318
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1319 lstr = Lstream_new (lstream_fixed_buffer, "r");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1320 str = FIXED_BUFFER_STREAM_DATA (lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1321 str->inbuf = buf;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1322 str->size = size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1323 XSETLSTREAM (obj, lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1324 return obj;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1325 }
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 173
diff changeset
1326
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1327 Lisp_Object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1328 make_fixed_buffer_output_stream (unsigned char *buf, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1329 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1330 Lisp_Object obj = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1331 Lstream *lstr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1332 struct fixed_buffer_stream *str;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1333
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1334 lstr = Lstream_new (lstream_fixed_buffer, "w");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1335 str = FIXED_BUFFER_STREAM_DATA (lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1336 str->outbuf = buf;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1337 str->size = size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1338 XSETLSTREAM (obj, lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1339 return obj;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1340 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1341
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1342 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1343 fixed_buffer_reader (Lstream *stream, unsigned char *data, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1344 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1345 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1346 size = min (size, str->size - str->offset);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1347 memcpy (data, str->inbuf + str->offset, size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1348 str->offset += size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1349 return size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1350 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1351
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1352 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1353 fixed_buffer_writer (Lstream *stream, CONST unsigned char *data, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1354 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1355 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1356 if (str->offset == str->size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1357 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1358 /* If we're at the end, just throw away the data and pretend
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1359 we wrote all of it. If we return 0, then the lstream routines
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1360 will try again and again to write it out. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1361 return size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1362 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1363 size = min (size, str->size - str->offset);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1364 memcpy (str->outbuf + str->offset, data, size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1365 str->offset += size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1366 return size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1367 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1368
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1369 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1370 fixed_buffer_rewinder (Lstream *stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1371 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1372 FIXED_BUFFER_STREAM_DATA (stream)->offset = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1373 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1374 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1375
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1376 CONST unsigned char *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1377 fixed_buffer_input_stream_ptr (Lstream *stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1378 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1379 assert (stream->imp == lstream_fixed_buffer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1380 return FIXED_BUFFER_STREAM_DATA (stream)->inbuf;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1381 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1382
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1383 unsigned char *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1384 fixed_buffer_output_stream_ptr (Lstream *stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1385 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1386 assert (stream->imp == lstream_fixed_buffer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1387 return FIXED_BUFFER_STREAM_DATA (stream)->outbuf;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1388 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1389
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1390 /*********** write to a resizing buffer ***********/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1391
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1392 #define RESIZING_BUFFER_STREAM_DATA(stream) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1393 LSTREAM_TYPE_DATA (stream, resizing_buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1394
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1395 struct resizing_buffer_stream
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1396 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1397 unsigned char *buf;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1398 int allocked;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1399 int max_stored;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1400 int stored;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1401 };
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1402
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1403 DEFINE_LSTREAM_IMPLEMENTATION ("resizing-buffer", lstream_resizing_buffer,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1404 sizeof (struct resizing_buffer_stream));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1405
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1406 Lisp_Object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1407 make_resizing_buffer_output_stream (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1408 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1409 Lisp_Object obj = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1410 XSETLSTREAM (obj, Lstream_new (lstream_resizing_buffer, "w"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1411 return obj;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1412 }
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 173
diff changeset
1413
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1414 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1415 resizing_buffer_writer (Lstream *stream, CONST unsigned char *data, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1416 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1417 struct resizing_buffer_stream *str = RESIZING_BUFFER_STREAM_DATA (stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1418 DO_REALLOC (str->buf, str->allocked, str->stored + size, unsigned char);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1419 memcpy (str->buf + str->stored, data, size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1420 str->stored += size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1421 str->max_stored = max (str->max_stored, str->stored);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1422 return size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1423 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1424
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1425 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1426 resizing_buffer_rewinder (Lstream *stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1427 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1428 RESIZING_BUFFER_STREAM_DATA (stream)->stored = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1429 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1430 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1431
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1432 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1433 resizing_buffer_closer (Lstream *stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1434 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1435 struct resizing_buffer_stream *str = RESIZING_BUFFER_STREAM_DATA (stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1436 if (str->buf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1437 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1438 xfree (str->buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1439 str->buf = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1440 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1441 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1442 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1443
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1444 unsigned char *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1445 resizing_buffer_stream_ptr (Lstream *stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1446 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1447 return RESIZING_BUFFER_STREAM_DATA (stream)->buf;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1448 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1449
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1450 /*********** write to an unsigned-char dynarr ***********/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1451
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1452 /* Note: If you have a dynarr whose type is not unsigned_char_dynarr
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1453 but which is really just an unsigned_char_dynarr (e.g. its type
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1454 is Bufbyte or Extbyte), just cast to unsigned_char_dynarr. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1455
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1456 #define DYNARR_STREAM_DATA(stream) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1457 LSTREAM_TYPE_DATA (stream, dynarr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1458
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1459 struct dynarr_stream
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1460 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1461 unsigned_char_dynarr *dyn;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1462 };
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1463
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1464 DEFINE_LSTREAM_IMPLEMENTATION ("dynarr", lstream_dynarr,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1465 sizeof (struct dynarr_stream));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1466
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1467 Lisp_Object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1468 make_dynarr_output_stream (unsigned_char_dynarr *dyn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1469 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1470 Lisp_Object obj = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1471 XSETLSTREAM (obj, Lstream_new (lstream_dynarr, "w"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1472 DYNARR_STREAM_DATA (XLSTREAM (obj))->dyn = dyn;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1473 return obj;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1474 }
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 173
diff changeset
1475
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1476 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1477 dynarr_writer (Lstream *stream, CONST unsigned char *data, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1478 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1479 struct dynarr_stream *str = DYNARR_STREAM_DATA (stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1480 Dynarr_add_many (str->dyn, data, size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1481 return size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1482 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1483
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1484 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1485 dynarr_rewinder (Lstream *stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1486 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1487 Dynarr_reset (DYNARR_STREAM_DATA (stream)->dyn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1488 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1489 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1490
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1491 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1492 dynarr_closer (Lstream *stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1493 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1494 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1495 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1496
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1497 /************ read from or write to a Lisp buffer ************/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1498
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1499 /* Note: Lisp-buffer read streams never return partial characters,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1500 and Lisp-buffer write streams expect to never get partial
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1501 characters. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1502
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1503 #define LISP_BUFFER_STREAM_DATA(stream) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1504 LSTREAM_TYPE_DATA (stream, lisp_buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1505
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1506 struct lisp_buffer_stream
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1507 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1508 Lisp_Object buffer;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1509 Lisp_Object orig_start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1510 /* we use markers to properly deal with insertion/deletion */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1511 Lisp_Object start, end;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1512 int flags;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1513 };
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1514
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1515 DEFINE_LSTREAM_IMPLEMENTATION ("lisp-buffer", lstream_lisp_buffer,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1516 sizeof (struct lisp_buffer_stream));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1517
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1518 static Lisp_Object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1519 make_lisp_buffer_stream_1 (struct buffer *buf, Bufpos start, Bufpos end,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1520 int flags, CONST char *mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1521 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1522 Lisp_Object obj = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1523 Lstream *lstr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1524 struct lisp_buffer_stream *str;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1525 Bufpos bmin, bmax;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1526 int reading = !strcmp (mode, "r");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1527
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1528 /* Make sure the luser didn't pass "w" in. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1529 if (!strcmp (mode, "w"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1530 abort ();
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 173
diff changeset
1531
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1532 if (flags & LSTR_IGNORE_ACCESSIBLE)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1533 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1534 bmin = BUF_BEG (buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1535 bmax = BUF_Z (buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1536 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1537 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1538 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1539 bmin = BUF_BEGV (buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1540 bmax = BUF_ZV (buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1541 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1542
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1543 if (start == -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1544 start = bmin;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1545 if (end == -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1546 end = bmax;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1547 assert (bmin <= start);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1548 assert (start <= bmax);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1549 if (reading)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1550 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1551 assert (bmin <= end);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1552 assert (end <= bmax);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1553 assert (start <= end);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1554 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1555
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1556 lstr = Lstream_new (lstream_lisp_buffer, mode);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1557 str = LISP_BUFFER_STREAM_DATA (lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1558 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1559 Lisp_Object marker;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1560 Lisp_Object buffer = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1561
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1562 XSETBUFFER (buffer, buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1563 marker = Fmake_marker ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1564 Fset_marker (marker, make_int (start), buffer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1565 str->start = marker;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1566 marker = Fmake_marker ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1567 Fset_marker (marker, make_int (start), buffer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1568 str->orig_start = marker;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1569 if (reading)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1570 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1571 marker = Fmake_marker ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1572 Fset_marker (marker, make_int (end), buffer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1573 str->end = marker;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1574 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1575 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1576 str->end = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1577 str->buffer = buffer;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1578 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1579 str->flags = flags;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1580 XSETLSTREAM (obj, lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1581 return obj;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1582 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1583
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1584 Lisp_Object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1585 make_lisp_buffer_input_stream (struct buffer *buf, Bufpos start, Bufpos end,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1586 int flags)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1587 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1588 return make_lisp_buffer_stream_1 (buf, start, end, flags, "r");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1589 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1590
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1591 Lisp_Object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1592 make_lisp_buffer_output_stream (struct buffer *buf, Bufpos pos, int flags)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1593 {
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
1594 Lisp_Object lstr = make_lisp_buffer_stream_1 (buf, pos, 0, flags, "wc");
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1595
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1596 Lstream_set_character_mode (XLSTREAM (lstr));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1597 return lstr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1598 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1599
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1600 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1601 lisp_buffer_reader (Lstream *stream, unsigned char *data, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1602 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1603 struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1604 unsigned char *orig_data = data;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1605 Bytind start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1606 Bytind end;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1607 struct buffer *buf = XBUFFER (str->buffer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1608
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1609 if (!BUFFER_LIVE_P (buf))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1610 return 0; /* Fut. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1611
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1612 /* NOTE: We do all our operations in Bytind's.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1613 Keep in mind that SIZE is a value in bytes, not chars. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1614
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1615 start = bi_marker_position (str->start);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1616 end = bi_marker_position (str->end);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1617 if (!(str->flags & LSTR_IGNORE_ACCESSIBLE))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1618 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1619 start = bytind_clip_to_bounds (BI_BUF_BEGV (buf), start,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1620 BI_BUF_ZV (buf));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1621 end = bytind_clip_to_bounds (BI_BUF_BEGV (buf), end,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1622 BI_BUF_ZV (buf));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1623 }
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 173
diff changeset
1624
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1625 size = min (size, end - start);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1626 end = start + size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1627 /* We cannot return a partial character. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1628 VALIDATE_BYTIND_BACKWARD (buf, end);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1629
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1630 while (start < end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1631 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1632 Bytind ceil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1633 Bytecount chunk;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1634
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1635 if (str->flags & LSTR_IGNORE_ACCESSIBLE)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1636 ceil = BI_BUF_CEILING_OF_IGNORE_ACCESSIBLE (buf, start);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1637 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1638 ceil = BI_BUF_CEILING_OF (buf, start);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1639 chunk = min (ceil, end) - start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1640 memcpy (data, BI_BUF_BYTE_ADDRESS (buf, start), chunk);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1641 data += chunk;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1642 start += chunk;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1643 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1644
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1645 if (EQ (buf->selective_display, Qt) && str->flags & LSTR_SELECTIVE)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1646 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1647 /* What a kludge. What a kludge. What a kludge. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1648 unsigned char *p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1649 for (p = orig_data; p < data; p++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1650 if (*p == '\r')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1651 *p = '\n';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1652 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1653
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1654 set_bi_marker_position (str->start, end);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1655 return data - orig_data;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1656 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1657
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1658 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1659 lisp_buffer_writer (Lstream *stream, CONST unsigned char *data, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1660 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1661 struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1662 Bufpos pos;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1663 struct buffer *buf = XBUFFER (str->buffer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1664
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1665 if (!BUFFER_LIVE_P (buf))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1666 return 0; /* Fut. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1667
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1668 pos = marker_position (str->start);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1669 pos += buffer_insert_raw_string_1 (buf, pos, data, size, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1670 set_marker_position (str->start, pos);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1671 return size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1672 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1673
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1674 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1675 lisp_buffer_rewinder (Lstream *stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1676 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1677 struct lisp_buffer_stream *str =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1678 LISP_BUFFER_STREAM_DATA (stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1679 struct buffer *buf = XBUFFER (str->buffer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1680 long pos = marker_position (str->orig_start);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1681 if (!BUFFER_LIVE_P (buf))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1682 return -1; /* Fut. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1683 if (pos > BUF_ZV (buf))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1684 pos = BUF_ZV (buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1685 if (pos < marker_position (str->orig_start))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1686 pos = marker_position (str->orig_start);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1687 if (MARKERP (str->end) && pos > marker_position (str->end))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1688 pos = marker_position (str->end);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1689 set_marker_position (str->start, pos);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1690 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1691 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1692
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1693 static Lisp_Object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1694 lisp_buffer_marker (Lisp_Object stream, void (*markobj) (Lisp_Object))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1695 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1696 struct lisp_buffer_stream *str =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1697 LISP_BUFFER_STREAM_DATA (XLSTREAM (stream));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1698
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1699 (markobj) (str->start);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1700 (markobj) (str->end);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1701 return str->buffer;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1702 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1703
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1704 Bufpos
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1705 lisp_buffer_stream_startpos (Lstream *stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1706 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1707 return marker_position (LISP_BUFFER_STREAM_DATA (stream)->start);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1708 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1709
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1710
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1711 /************************************************************************/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1712 /* initialization */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1713 /************************************************************************/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1714
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1715 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1716 lstream_type_create (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1717 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1718 LSTREAM_HAS_METHOD (stdio, reader);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1719 LSTREAM_HAS_METHOD (stdio, writer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1720 LSTREAM_HAS_METHOD (stdio, rewinder);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1721 LSTREAM_HAS_METHOD (stdio, seekable_p);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1722 LSTREAM_HAS_METHOD (stdio, flusher);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1723 LSTREAM_HAS_METHOD (stdio, closer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1724
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1725 LSTREAM_HAS_METHOD (filedesc, reader);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1726 LSTREAM_HAS_METHOD (filedesc, writer);
263
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 241
diff changeset
1727 LSTREAM_HAS_METHOD (filedesc, was_blocked_p);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1728 LSTREAM_HAS_METHOD (filedesc, rewinder);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1729 LSTREAM_HAS_METHOD (filedesc, seekable_p);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1730 LSTREAM_HAS_METHOD (filedesc, closer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1731
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1732 LSTREAM_HAS_METHOD (lisp_string, reader);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1733 LSTREAM_HAS_METHOD (lisp_string, rewinder);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1734 LSTREAM_HAS_METHOD (lisp_string, marker);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1735
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1736 LSTREAM_HAS_METHOD (fixed_buffer, reader);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1737 LSTREAM_HAS_METHOD (fixed_buffer, writer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1738 LSTREAM_HAS_METHOD (fixed_buffer, rewinder);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1739
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1740 LSTREAM_HAS_METHOD (resizing_buffer, writer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1741 LSTREAM_HAS_METHOD (resizing_buffer, rewinder);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1742 LSTREAM_HAS_METHOD (resizing_buffer, closer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1743
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1744 LSTREAM_HAS_METHOD (dynarr, writer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1745 LSTREAM_HAS_METHOD (dynarr, rewinder);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1746 LSTREAM_HAS_METHOD (dynarr, closer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1747
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1748 LSTREAM_HAS_METHOD (lisp_buffer, reader);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1749 LSTREAM_HAS_METHOD (lisp_buffer, writer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1750 LSTREAM_HAS_METHOD (lisp_buffer, rewinder);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1751 LSTREAM_HAS_METHOD (lisp_buffer, marker);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1752 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1753
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1754 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1755 vars_of_lstream (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1756 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1757 int i;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1758
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1759 for (i = 0; i < countof (Vlstream_free_list); i++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1760 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1761 Vlstream_free_list[i] = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1762 staticpro (&Vlstream_free_list[i]);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1763 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1764 }