Commit | Line | Data |
---|---|---|
b0e5fd8c GH |
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 | ||
48 | #include "libguile/_scm.h" | |
49 | #include "libguile/fports.h" | |
50 | #include "libguile/ports.h" | |
51 | #include "libguile/root.h" | |
52 | #include "libguile/rw.h" | |
53 | #include "libguile/strings.h" | |
54 | #include "libguile/validate.h" | |
fee12d18 MV |
55 | #include "libguile/modules.h" |
56 | #include "libguile/strports.h" | |
b0e5fd8c GH |
57 | |
58 | #ifdef HAVE_UNISTD_H | |
59 | #include <unistd.h> | |
60 | #endif | |
61 | ||
62 | \f | |
63 | ||
64 | #if defined (EAGAIN) | |
65 | #define SCM_MAYBE_EAGAIN || errno == EAGAIN | |
66 | #else | |
67 | #define SCM_MAYBE_EAGAIN | |
68 | #endif | |
69 | ||
70 | #if defined (EWOULDBLOCK) | |
71 | #define SCM_MAYBE_EWOULDBLOCK || errno == EWOULDBLOCK | |
72 | #else | |
73 | #define SCM_MAYBE_EWOULDBLOCK | |
74 | #endif | |
75 | ||
76 | /* MAYBE there is EAGAIN way of defining this macro but now I EWOULDBLOCK. */ | |
77 | #define SCM_EBLOCK(errno) \ | |
78 | (0 SCM_MAYBE_EAGAIN SCM_MAYBE_EWOULDBLOCK) | |
79 | ||
80 | SCM_DEFINE (scm_read_string_x_partial, "read-string!/partial", 1, 3, 0, | |
81 | (SCM str, SCM port_or_fdes, SCM start, SCM end), | |
82 | "Read characters from an fport or file descriptor into a\n" | |
83 | "string @var{str}. This procedure is scsh-compatible\n" | |
84 | "and can efficiently read large strings. It will:\n\n" | |
85 | "@itemize\n" | |
86 | "@item\n" | |
87 | "attempt to fill the entire string, unless the @var{start}\n" | |
88 | "and/or @var{end} arguments are supplied. i.e., @var{start}\n" | |
89 | "defaults to 0 and @var{end} defaults to\n" | |
90 | "@code{(string-length str)}\n" | |
91 | "@item\n" | |
92 | "use the current input port if @var{port_or_fdes} is not\n" | |
93 | "supplied.\n" | |
94 | "@item\n" | |
95 | "read any characters that are currently available,\n" | |
96 | "without waiting for the rest (short reads are possible).\n\n" | |
97 | "@item\n" | |
98 | "wait for as long as it needs to for the first character to\n" | |
99 | "become available, unless the port is in non-blocking mode\n" | |
100 | "@item\n" | |
101 | "return @code{#f} if end-of-file is encountered before reading\n" | |
102 | "any characters, otherwise return the number of characters\n" | |
103 | "read.\n" | |
104 | "@item\n" | |
105 | "return 0 if the port is in non-blocking mode and no characters\n" | |
106 | "are immediately available.\n" | |
107 | "@item\n" | |
108 | "return 0 if the request is for 0 bytes, with no\n" | |
109 | "end-of-file check\n" | |
110 | "@end itemize") | |
111 | #define FUNC_NAME s_scm_read_string_x_partial | |
112 | { | |
113 | char *dest; | |
c014a02e ML |
114 | long read_len; |
115 | long chars_read = 0; | |
b0e5fd8c GH |
116 | int fdes; |
117 | ||
118 | { | |
c014a02e ML |
119 | long offset; |
120 | long last; | |
b0e5fd8c GH |
121 | |
122 | SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, str, dest, 3, start, offset, | |
123 | 4, end, last); | |
124 | dest += offset; | |
125 | read_len = last - offset; | |
126 | } | |
127 | ||
128 | if (SCM_INUMP (port_or_fdes)) | |
129 | fdes = SCM_INUM (port_or_fdes); | |
130 | else | |
131 | { | |
132 | SCM port = SCM_UNBNDP (port_or_fdes) ? scm_cur_inp : port_or_fdes; | |
133 | ||
134 | SCM_VALIDATE_OPFPORT (2, port); | |
135 | SCM_VALIDATE_INPUT_PORT (2, port); | |
136 | ||
137 | /* if there's anything in the port buffers, use it, but then | |
138 | don't touch the file descriptor. otherwise the | |
139 | "return immediately if something is available" rule may | |
140 | be violated. */ | |
141 | chars_read = scm_take_from_input_buffers (port, dest, read_len); | |
142 | fdes = SCM_FPORT_FDES (port); | |
143 | } | |
144 | ||
145 | if (chars_read == 0 && read_len > 0) /* don't confuse read_len == 0 with | |
146 | EOF. */ | |
147 | { | |
148 | SCM_SYSCALL (chars_read = read (fdes, dest, read_len)); | |
149 | if (chars_read == -1) | |
150 | { | |
151 | if (SCM_EBLOCK (errno)) | |
152 | chars_read = 0; | |
153 | else | |
154 | SCM_SYSERROR; | |
155 | } | |
156 | else if (chars_read == 0) | |
157 | return SCM_BOOL_F; | |
158 | } | |
159 | return scm_long2num (chars_read); | |
160 | } | |
161 | #undef FUNC_NAME | |
162 | ||
e615ee8d MV |
163 | SCM |
164 | scm_init_rw_builtins () | |
b0e5fd8c | 165 | { |
b0e5fd8c GH |
166 | #ifndef SCM_MAGIC_SNARFER |
167 | #include "libguile/rw.x" | |
168 | #endif | |
169 | ||
e615ee8d MV |
170 | return SCM_UNSPECIFIED; |
171 | } | |
172 | ||
173 | void | |
174 | scm_init_rw () | |
175 | { | |
9a441ddb | 176 | scm_c_define_gsubr ("%init-rw-builtins", 0, 0, 0, scm_init_rw_builtins); |
b0e5fd8c GH |
177 | } |
178 | ||
179 | /* | |
180 | Local Variables: | |
181 | c-file-style: "gnu" | |
182 | End: | |
183 | */ |