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