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