Commit | Line | Data |
---|---|---|
4251ae2e | 1 | /* Copyright (C) 2001, 2006, 2009, 2011 Free Software Foundation, Inc. |
b0e5fd8c | 2 | * |
73be1d9e | 3 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
4 | * modify it under the terms of the GNU Lesser General Public License |
5 | * as published by the Free Software Foundation; either version 3 of | |
6 | * the License, or (at your option) any later version. | |
b0e5fd8c | 7 | * |
53befeb7 NJ |
8 | * This library is distributed in the hope that it will be useful, but |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
73be1d9e MV |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | * Lesser General Public License for more details. | |
b0e5fd8c | 12 | * |
73be1d9e MV |
13 | * You should have received a copy of the GNU Lesser General Public |
14 | * License along with this library; if not, write to the Free Software | |
53befeb7 NJ |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
16 | * 02110-1301 USA | |
73be1d9e | 17 | */ |
b0e5fd8c GH |
18 | |
19 | \f | |
20 | ||
21 | /* This is the C part of the (ice-9 rw) module. */ | |
22 | ||
dbb605f5 | 23 | #ifdef HAVE_CONFIG_H |
7b5a83d3 RB |
24 | # include <config.h> |
25 | #endif | |
26 | ||
b0e5fd8c | 27 | #include <errno.h> |
fbbdb121 | 28 | #include <string.h> |
b0e5fd8c GH |
29 | |
30 | #include "libguile/_scm.h" | |
31 | #include "libguile/fports.h" | |
32 | #include "libguile/ports.h" | |
33 | #include "libguile/root.h" | |
34 | #include "libguile/rw.h" | |
35 | #include "libguile/strings.h" | |
36 | #include "libguile/validate.h" | |
fee12d18 MV |
37 | #include "libguile/modules.h" |
38 | #include "libguile/strports.h" | |
b0e5fd8c GH |
39 | |
40 | #ifdef HAVE_UNISTD_H | |
41 | #include <unistd.h> | |
42 | #endif | |
7beabedb MG |
43 | #ifdef HAVE_IO_H |
44 | #include <io.h> | |
45 | #endif | |
b0e5fd8c GH |
46 | |
47 | \f | |
48 | ||
49 | #if defined (EAGAIN) | |
50 | #define SCM_MAYBE_EAGAIN || errno == EAGAIN | |
51 | #else | |
52 | #define SCM_MAYBE_EAGAIN | |
53 | #endif | |
54 | ||
55 | #if defined (EWOULDBLOCK) | |
56 | #define SCM_MAYBE_EWOULDBLOCK || errno == EWOULDBLOCK | |
57 | #else | |
58 | #define SCM_MAYBE_EWOULDBLOCK | |
59 | #endif | |
60 | ||
61 | /* MAYBE there is EAGAIN way of defining this macro but now I EWOULDBLOCK. */ | |
62 | #define SCM_EBLOCK(errno) \ | |
63 | (0 SCM_MAYBE_EAGAIN SCM_MAYBE_EWOULDBLOCK) | |
64 | ||
65 | SCM_DEFINE (scm_read_string_x_partial, "read-string!/partial", 1, 3, 0, | |
66 | (SCM str, SCM port_or_fdes, SCM start, SCM end), | |
99004a28 GH |
67 | "Read characters from a port or file descriptor into a\n" |
68 | "string @var{str}. A port must have an underlying file\n" | |
69 | "descriptor --- a so-called fport. This procedure is\n" | |
70 | "scsh-compatible and can efficiently read large strings.\n" | |
71 | "It will:\n\n" | |
b0e5fd8c GH |
72 | "@itemize\n" |
73 | "@item\n" | |
74 | "attempt to fill the entire string, unless the @var{start}\n" | |
75 | "and/or @var{end} arguments are supplied. i.e., @var{start}\n" | |
76 | "defaults to 0 and @var{end} defaults to\n" | |
77 | "@code{(string-length str)}\n" | |
78 | "@item\n" | |
79 | "use the current input port if @var{port_or_fdes} is not\n" | |
80 | "supplied.\n" | |
81 | "@item\n" | |
99004a28 GH |
82 | "return fewer than the requested number of characters in some\n" |
83 | "cases, e.g., on end of file, if interrupted by a signal, or if\n" | |
84 | "not all the characters are immediately available.\n" | |
b0e5fd8c | 85 | "@item\n" |
99004a28 GH |
86 | "wait indefinitely for some input if no characters are\n" |
87 | "currently available,\n" | |
88 | "unless the port is in non-blocking mode.\n" | |
89 | "@item\n" | |
90 | "read characters from the port's input buffers if available,\n" | |
91 | "instead from the underlying file descriptor.\n" | |
b0e5fd8c GH |
92 | "@item\n" |
93 | "return @code{#f} if end-of-file is encountered before reading\n" | |
94 | "any characters, otherwise return the number of characters\n" | |
95 | "read.\n" | |
96 | "@item\n" | |
97 | "return 0 if the port is in non-blocking mode and no characters\n" | |
98 | "are immediately available.\n" | |
99 | "@item\n" | |
100 | "return 0 if the request is for 0 bytes, with no\n" | |
99004a28 | 101 | "end-of-file check.\n" |
b0e5fd8c GH |
102 | "@end itemize") |
103 | #define FUNC_NAME s_scm_read_string_x_partial | |
104 | { | |
105 | char *dest; | |
a9d13d44 | 106 | size_t offset; |
c014a02e ML |
107 | long read_len; |
108 | long chars_read = 0; | |
b0e5fd8c GH |
109 | int fdes; |
110 | ||
111 | { | |
8824ac88 | 112 | size_t last; |
b0e5fd8c | 113 | |
8824ac88 | 114 | SCM_VALIDATE_STRING (1, str); |
cc95e00a | 115 | scm_i_get_substring_spec (scm_i_string_length (str), |
8824ac88 | 116 | start, &offset, end, &last); |
b0e5fd8c GH |
117 | read_len = last - offset; |
118 | } | |
119 | ||
e11e83f3 MV |
120 | if (scm_is_integer (port_or_fdes)) |
121 | fdes = scm_to_int (port_or_fdes); | |
b0e5fd8c GH |
122 | else |
123 | { | |
9de87eea MV |
124 | SCM port = (SCM_UNBNDP (port_or_fdes)? |
125 | scm_current_input_port () : port_or_fdes); | |
b0e5fd8c GH |
126 | |
127 | SCM_VALIDATE_OPFPORT (2, port); | |
128 | SCM_VALIDATE_INPUT_PORT (2, port); | |
129 | ||
130 | /* if there's anything in the port buffers, use it, but then | |
131 | don't touch the file descriptor. otherwise the | |
132 | "return immediately if something is available" rule may | |
133 | be violated. */ | |
9c44cd45 | 134 | str = scm_i_string_start_writing (str); |
a9d13d44 | 135 | dest = scm_i_string_writable_chars (str) + offset; |
b0e5fd8c | 136 | chars_read = scm_take_from_input_buffers (port, dest, read_len); |
cc95e00a | 137 | scm_i_string_stop_writing (); |
b0e5fd8c GH |
138 | fdes = SCM_FPORT_FDES (port); |
139 | } | |
140 | ||
141 | if (chars_read == 0 && read_len > 0) /* don't confuse read_len == 0 with | |
142 | EOF. */ | |
143 | { | |
9c44cd45 | 144 | str = scm_i_string_start_writing (str); |
a9d13d44 | 145 | dest = scm_i_string_writable_chars (str) + offset; |
b0e5fd8c | 146 | SCM_SYSCALL (chars_read = read (fdes, dest, read_len)); |
cc95e00a | 147 | scm_i_string_stop_writing (); |
b0e5fd8c GH |
148 | if (chars_read == -1) |
149 | { | |
150 | if (SCM_EBLOCK (errno)) | |
151 | chars_read = 0; | |
152 | else | |
153 | SCM_SYSERROR; | |
154 | } | |
155 | else if (chars_read == 0) | |
8824ac88 MV |
156 | { |
157 | scm_remember_upto_here_1 (str); | |
158 | return SCM_BOOL_F; | |
159 | } | |
b0e5fd8c | 160 | } |
8824ac88 MV |
161 | |
162 | scm_remember_upto_here_1 (str); | |
b9bd8526 | 163 | return scm_from_long (chars_read); |
b0e5fd8c GH |
164 | } |
165 | #undef FUNC_NAME | |
166 | ||
99004a28 GH |
167 | SCM_DEFINE (scm_write_string_partial, "write-string/partial", 1, 3, 0, |
168 | (SCM str, SCM port_or_fdes, SCM start, SCM end), | |
169 | "Write characters from a string @var{str} to a port or file\n" | |
170 | "descriptor. A port must have an underlying file descriptor\n" | |
171 | "--- a so-called fport. This procedure is\n" | |
172 | "scsh-compatible and can efficiently write large strings.\n" | |
173 | "It will:\n\n" | |
174 | "@itemize\n" | |
175 | "@item\n" | |
176 | "attempt to write the entire string, unless the @var{start}\n" | |
177 | "and/or @var{end} arguments are supplied. i.e., @var{start}\n" | |
178 | "defaults to 0 and @var{end} defaults to\n" | |
179 | "@code{(string-length str)}\n" | |
180 | "@item\n" | |
181 | "use the current output port if @var{port_of_fdes} is not\n" | |
182 | "supplied.\n" | |
183 | "@item\n" | |
184 | "in the case of a buffered port, store the characters in the\n" | |
185 | "port's output buffer, if all will fit. If they will not fit\n" | |
186 | "then any existing buffered characters will be flushed\n" | |
187 | "before attempting\n" | |
188 | "to write the new characters directly to the underlying file\n" | |
189 | "descriptor. If the port is in non-blocking mode and\n" | |
190 | "buffered characters can not be flushed immediately, then an\n" | |
191 | "@code{EAGAIN} system-error exception will be raised (Note:\n" | |
192 | "scsh does not support the use of non-blocking buffered ports.)\n" | |
193 | "@item\n" | |
194 | "write fewer than the requested number of\n" | |
195 | "characters in some cases, e.g., if interrupted by a signal or\n" | |
196 | "if not all of the output can be accepted immediately.\n" | |
197 | "@item\n" | |
198 | "wait indefinitely for at least one character\n" | |
199 | "from @var{str} to be accepted by the port, unless the port is\n" | |
200 | "in non-blocking mode.\n" | |
201 | "@item\n" | |
202 | "return the number of characters accepted by the port.\n" | |
203 | "@item\n" | |
204 | "return 0 if the port is in non-blocking mode and can not accept\n" | |
205 | "at least one character from @var{str} immediately\n" | |
206 | "@item\n" | |
207 | "return 0 immediately if the request size is 0 bytes.\n" | |
208 | "@end itemize") | |
209 | #define FUNC_NAME s_scm_write_string_partial | |
210 | { | |
cc95e00a | 211 | const char *src; |
f1ce9199 | 212 | scm_t_off write_len; |
99004a28 GH |
213 | int fdes; |
214 | ||
215 | { | |
8824ac88 MV |
216 | size_t offset; |
217 | size_t last; | |
99004a28 | 218 | |
8824ac88 | 219 | SCM_VALIDATE_STRING (1, str); |
cc95e00a MV |
220 | src = scm_i_string_chars (str); |
221 | scm_i_get_substring_spec (scm_i_string_length (str), | |
8824ac88 | 222 | start, &offset, end, &last); |
99004a28 GH |
223 | src += offset; |
224 | write_len = last - offset; | |
225 | } | |
226 | ||
227 | if (write_len == 0) | |
228 | return SCM_INUM0; | |
229 | ||
e11e83f3 MV |
230 | if (scm_is_integer (port_or_fdes)) |
231 | fdes = scm_to_int (port_or_fdes); | |
99004a28 GH |
232 | else |
233 | { | |
9de87eea MV |
234 | SCM port = (SCM_UNBNDP (port_or_fdes)? |
235 | scm_current_output_port () : port_or_fdes); | |
92c2555f | 236 | scm_t_port *pt; |
f1ce9199 | 237 | scm_t_off space; |
99004a28 GH |
238 | |
239 | SCM_VALIDATE_OPFPORT (2, port); | |
240 | SCM_VALIDATE_OUTPUT_PORT (2, port); | |
241 | pt = SCM_PTAB_ENTRY (port); | |
242 | /* filling the last character in the buffer would require a flush. */ | |
243 | space = pt->write_end - pt->write_pos - 1; | |
244 | if (space >= write_len) | |
245 | { | |
246 | memcpy (pt->write_pos, src, write_len); | |
247 | pt->write_pos += write_len; | |
b9bd8526 | 248 | return scm_from_long (write_len); |
99004a28 GH |
249 | } |
250 | if (pt->write_pos > pt->write_buf) | |
4251ae2e | 251 | scm_flush_unlocked (port); |
99004a28 GH |
252 | fdes = SCM_FPORT_FDES (port); |
253 | } | |
254 | { | |
255 | long rv; | |
256 | ||
257 | SCM_SYSCALL (rv = write (fdes, src, write_len)); | |
258 | if (rv == -1) | |
259 | { | |
260 | if (SCM_EBLOCK (errno)) | |
261 | rv = 0; | |
262 | else | |
263 | SCM_SYSERROR; | |
264 | } | |
8824ac88 MV |
265 | |
266 | scm_remember_upto_here_1 (str); | |
b9bd8526 | 267 | return scm_from_long (rv); |
99004a28 GH |
268 | } |
269 | } | |
270 | #undef FUNC_NAME | |
271 | ||
e615ee8d MV |
272 | SCM |
273 | scm_init_rw_builtins () | |
b0e5fd8c | 274 | { |
b0e5fd8c | 275 | #include "libguile/rw.x" |
b0e5fd8c | 276 | |
e615ee8d MV |
277 | return SCM_UNSPECIFIED; |
278 | } | |
279 | ||
280 | void | |
281 | scm_init_rw () | |
282 | { | |
9a441ddb | 283 | scm_c_define_gsubr ("%init-rw-builtins", 0, 0, 0, scm_init_rw_builtins); |
b0e5fd8c GH |
284 | } |
285 | ||
286 | /* | |
287 | Local Variables: | |
288 | c-file-style: "gnu" | |
289 | End: | |
290 | */ |