1 /* Copyright (C) 2001 Free Software Foundation, Inc.
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.
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.
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
20 /* This is the C part of the (ice-9 rw) module. */
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"
36 #include "libguile/modules.h"
37 #include "libguile/strports.h"
49 #define SCM_MAYBE_EAGAIN || errno == EAGAIN
51 #define SCM_MAYBE_EAGAIN
54 #if defined (EWOULDBLOCK)
55 #define SCM_MAYBE_EWOULDBLOCK || errno == EWOULDBLOCK
57 #define SCM_MAYBE_EWOULDBLOCK
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)
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
),
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"
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"
78 "use the current input port if @var{port_or_fdes} is not\n"
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"
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"
89 "read characters from the port's input buffers if available,\n"
90 "instead from the underlying file descriptor.\n"
92 "return @code{#f} if end-of-file is encountered before reading\n"
93 "any characters, otherwise return the number of characters\n"
96 "return 0 if the port is in non-blocking mode and no characters\n"
97 "are immediately available.\n"
99 "return 0 if the request is for 0 bytes, with no\n"
100 "end-of-file check.\n"
102 #define FUNC_NAME s_scm_read_string_x_partial
113 SCM_VALIDATE_STRING (1, str
);
114 dest
= SCM_I_STRING_CHARS (str
);
115 scm_i_get_substring_spec (SCM_I_STRING_LENGTH (str
),
116 start
, &offset
, end
, &last
);
118 read_len
= last
- offset
;
121 if (scm_is_integer (port_or_fdes
))
122 fdes
= scm_to_int (port_or_fdes
);
125 SCM port
= SCM_UNBNDP (port_or_fdes
) ? scm_cur_inp
: port_or_fdes
;
127 SCM_VALIDATE_OPFPORT (2, port
);
128 SCM_VALIDATE_INPUT_PORT (2, port
);
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
134 chars_read
= scm_take_from_input_buffers (port
, dest
, read_len
);
135 fdes
= SCM_FPORT_FDES (port
);
138 if (chars_read
== 0 && read_len
> 0) /* don't confuse read_len == 0 with
141 SCM_SYSCALL (chars_read
= read (fdes
, dest
, read_len
));
142 if (chars_read
== -1)
144 if (SCM_EBLOCK (errno
))
149 else if (chars_read
== 0)
151 scm_remember_upto_here_1 (str
);
156 scm_remember_upto_here_1 (str
);
157 return scm_from_long (chars_read
);
161 SCM_DEFINE (scm_write_string_partial
, "write-string/partial", 1, 3, 0,
162 (SCM str
, SCM port_or_fdes
, SCM start
, SCM end
),
163 "Write characters from a string @var{str} to a port or file\n"
164 "descriptor. A port must have an underlying file descriptor\n"
165 "--- a so-called fport. This procedure is\n"
166 "scsh-compatible and can efficiently write large strings.\n"
170 "attempt to write the entire string, unless the @var{start}\n"
171 "and/or @var{end} arguments are supplied. i.e., @var{start}\n"
172 "defaults to 0 and @var{end} defaults to\n"
173 "@code{(string-length str)}\n"
175 "use the current output port if @var{port_of_fdes} is not\n"
178 "in the case of a buffered port, store the characters in the\n"
179 "port's output buffer, if all will fit. If they will not fit\n"
180 "then any existing buffered characters will be flushed\n"
181 "before attempting\n"
182 "to write the new characters directly to the underlying file\n"
183 "descriptor. If the port is in non-blocking mode and\n"
184 "buffered characters can not be flushed immediately, then an\n"
185 "@code{EAGAIN} system-error exception will be raised (Note:\n"
186 "scsh does not support the use of non-blocking buffered ports.)\n"
188 "write fewer than the requested number of\n"
189 "characters in some cases, e.g., if interrupted by a signal or\n"
190 "if not all of the output can be accepted immediately.\n"
192 "wait indefinitely for at least one character\n"
193 "from @var{str} to be accepted by the port, unless the port is\n"
194 "in non-blocking mode.\n"
196 "return the number of characters accepted by the port.\n"
198 "return 0 if the port is in non-blocking mode and can not accept\n"
199 "at least one character from @var{str} immediately\n"
201 "return 0 immediately if the request size is 0 bytes.\n"
203 #define FUNC_NAME s_scm_write_string_partial
213 SCM_VALIDATE_STRING (1, str
);
214 src
= SCM_I_STRING_CHARS (str
);
215 scm_i_get_substring_spec (SCM_I_STRING_LENGTH (str
),
216 start
, &offset
, end
, &last
);
218 write_len
= last
- offset
;
224 if (scm_is_integer (port_or_fdes
))
225 fdes
= scm_to_int (port_or_fdes
);
228 SCM port
= SCM_UNBNDP (port_or_fdes
) ? scm_cur_outp
: port_or_fdes
;
232 SCM_VALIDATE_OPFPORT (2, port
);
233 SCM_VALIDATE_OUTPUT_PORT (2, port
);
234 pt
= SCM_PTAB_ENTRY (port
);
235 /* filling the last character in the buffer would require a flush. */
236 space
= pt
->write_end
- pt
->write_pos
- 1;
237 if (space
>= write_len
)
239 memcpy (pt
->write_pos
, src
, write_len
);
240 pt
->write_pos
+= write_len
;
241 return scm_from_long (write_len
);
243 if (pt
->write_pos
> pt
->write_buf
)
245 fdes
= SCM_FPORT_FDES (port
);
250 SCM_SYSCALL (rv
= write (fdes
, src
, write_len
));
253 if (SCM_EBLOCK (errno
))
259 scm_remember_upto_here_1 (str
);
260 return scm_from_long (rv
);
266 scm_init_rw_builtins ()
268 #include "libguile/rw.x"
270 return SCM_UNSPECIFIED
;
276 scm_c_define_gsubr ("%init-rw-builtins", 0, 0, 0, scm_init_rw_builtins
);