Commit | Line | Data |
---|---|---|
6d36532c | 1 | /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc. |
0f2d19dd JB |
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 | |
82892bed JB |
15 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
16 | * Boston, MA 02111-1307 USA | |
0f2d19dd JB |
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. | |
82892bed | 40 | * If you do not wish that, delete this exception notice. */ |
1bbd0b84 GB |
41 | |
42 | /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999, | |
43 | gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */ | |
44 | ||
0f2d19dd JB |
45 | \f |
46 | ||
e6e2e95a MD |
47 | #include <errno.h> |
48 | ||
a0599745 | 49 | #include "libguile/_scm.h" |
6d36532c | 50 | #include "libguile/ioext.h" |
a0599745 | 51 | #include "libguile/fports.h" |
a0599745 | 52 | #include "libguile/feature.h" |
6d36532c | 53 | #include "libguile/ports.h" |
a0599745 MD |
54 | #include "libguile/root.h" |
55 | #include "libguile/strings.h" | |
a0599745 | 56 | #include "libguile/validate.h" |
0f2d19dd | 57 | |
ee149d03 JB |
58 | #include <fcntl.h> |
59 | ||
95b88819 GH |
60 | #ifdef HAVE_STRING_H |
61 | #include <string.h> | |
62 | #endif | |
63 | #ifdef HAVE_UNISTD_H | |
64 | #include <unistd.h> | |
65 | #endif | |
0f2d19dd JB |
66 | \f |
67 | ||
10288a09 GH |
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 | ||
c2da2648 GH |
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 an fport or file descriptor into a\n" | |
87 | "string @var{str}. This procedure is scsh-compatible\n" | |
88 | "and can efficiently read large strings. It will:\n\n" | |
89 | "@itemize\n" | |
90 | "@item\n" | |
91 | "attempt to fill the entire string, unless the @var{start}\n" | |
92 | "and/or @var{end} arguments are supplied. i.e., @var{start}\n" | |
93 | "defaults to 0 and @var{end} defaults to\n" | |
94 | "@code{(string-length str)}\n" | |
95 | "@item\n" | |
96 | "use the current input port if @var{port_or_fdes} is not\n" | |
97 | "supplied.\n" | |
98 | "@item\n" | |
99 | "read any characters that are currently available,\n" | |
100 | "without waiting for the rest (short reads are possible).\n\n" | |
101 | "@item\n" | |
102 | "wait for as long as it needs to for the first character to\n" | |
103 | "become available, unless the port is in non-blocking mode\n" | |
104 | "@item\n" | |
105 | "return @code{#f} if end-of-file is encountered before reading\n" | |
106 | "any characters, otherwise return the number of characters\n" | |
107 | "read.\n" | |
108 | "@item\n" | |
109 | "return 0 if the port is in non-blocking mode and no characters\n" | |
110 | "are immediately available.\n" | |
111 | "@item\n" | |
112 | "return 0 if the request is for 0 bytes, with no\n" | |
113 | "end-of-file check\n" | |
114 | "@end itemize") | |
115 | #define FUNC_NAME s_scm_read_string_x_partial | |
116 | { | |
117 | char *dest; | |
118 | long read_len; | |
119 | long chars_read = 0; | |
60d02d09 | 120 | int fdes; |
c2da2648 GH |
121 | |
122 | { | |
60d02d09 GH |
123 | long offset; |
124 | long last; | |
c2da2648 | 125 | |
60d02d09 GH |
126 | SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, str, dest, 3, start, offset, |
127 | 4, end, last); | |
c2da2648 GH |
128 | dest += offset; |
129 | read_len = last - offset; | |
130 | } | |
131 | ||
60d02d09 GH |
132 | if (SCM_INUMP (port_or_fdes)) |
133 | fdes = SCM_INUM (port_or_fdes); | |
134 | else | |
c2da2648 | 135 | { |
60d02d09 | 136 | SCM port = SCM_UNBNDP (port_or_fdes) ? scm_cur_inp : port_or_fdes; |
c2da2648 | 137 | |
60d02d09 GH |
138 | SCM_VALIDATE_OPFPORT (2, port); |
139 | SCM_VALIDATE_INPUT_PORT (2, port); | |
140 | ||
141 | /* if there's anything in the port buffers, use it, but then | |
142 | don't touch the file descriptor. otherwise the | |
143 | "return immediately if something is available" rule may | |
144 | be violated. */ | |
145 | chars_read = scm_take_from_input_buffers (port, dest, read_len); | |
146 | fdes = SCM_FPORT_FDES (port); | |
c2da2648 GH |
147 | } |
148 | ||
149 | if (chars_read == 0 && read_len > 0) /* don't confuse read_len == 0 with | |
150 | EOF. */ | |
151 | { | |
c2da2648 GH |
152 | SCM_SYSCALL (chars_read = read (fdes, dest, read_len)); |
153 | if (chars_read == -1) | |
154 | { | |
10288a09 | 155 | if (SCM_EBLOCK (errno)) |
c2da2648 GH |
156 | chars_read = 0; |
157 | else | |
c2da2648 GH |
158 | SCM_SYSERROR; |
159 | } | |
160 | else if (chars_read == 0) | |
161 | return SCM_BOOL_F; | |
162 | } | |
163 | return scm_long2num (chars_read); | |
164 | } | |
165 | #undef FUNC_NAME | |
166 | ||
a1ec6916 | 167 | SCM_DEFINE (scm_ftell, "ftell", 1, 0, 0, |
1e6808ea MG |
168 | (SCM fd_port), |
169 | "Return an integer representing the current position of\n" | |
170 | "@var{fd/port}, measured from the beginning. Equivalent to:\n" | |
171 | "\n" | |
172 | "@lisp\n" | |
b380b885 | 173 | "(seek port 0 SEEK_CUR)\n" |
1e6808ea | 174 | "@end lisp") |
1bbd0b84 | 175 | #define FUNC_NAME s_scm_ftell |
0f2d19dd | 176 | { |
1e6808ea | 177 | return scm_seek (fd_port, SCM_INUM0, SCM_MAKINUM (SEEK_CUR)); |
ee149d03 | 178 | } |
1bbd0b84 | 179 | #undef FUNC_NAME |
0f2d19dd | 180 | |
f5f2dcff DH |
181 | |
182 | #if (SCM_DEBUG_DEPRECATED == 0) | |
183 | ||
a1ec6916 | 184 | SCM_DEFINE (scm_fseek, "fseek", 3, 0, 0, |
1e6808ea MG |
185 | (SCM fd_port, SCM offset, SCM whence), |
186 | "Obsolete. Almost the same as @code{seek}, but the return value\n" | |
187 | "is unspecified.") | |
1bbd0b84 | 188 | #define FUNC_NAME s_scm_fseek |
0f2d19dd | 189 | { |
1e6808ea | 190 | scm_seek (fd_port, offset, whence); |
02b754d3 | 191 | return SCM_UNSPECIFIED; |
0f2d19dd | 192 | } |
1bbd0b84 | 193 | #undef FUNC_NAME |
0f2d19dd | 194 | |
f5f2dcff DH |
195 | #endif /* SCM_DEBUG_DEPRECATED == 0 */ |
196 | ||
197 | ||
a1ec6916 | 198 | SCM_DEFINE (scm_redirect_port, "redirect-port", 2, 0, 0, |
1bbd0b84 | 199 | (SCM old, SCM new), |
b380b885 MD |
200 | "This procedure takes two ports and duplicates the underlying file\n" |
201 | "descriptor from @var{old-port} into @var{new-port}. The\n" | |
202 | "current file descriptor in @var{new-port} will be closed.\n" | |
203 | "After the redirection the two ports will share a file position\n" | |
204 | "and file status flags.\n\n" | |
205 | "The return value is unspecified.\n\n" | |
206 | "Unexpected behaviour can result if both ports are subsequently used\n" | |
207 | "and the original and/or duplicate ports are buffered.\n\n" | |
208 | "This procedure does not have any side effects on other ports or\n" | |
209 | "revealed counts.") | |
1bbd0b84 | 210 | #define FUNC_NAME s_scm_redirect_port |
0f2d19dd JB |
211 | { |
212 | int ans, oldfd, newfd; | |
ee149d03 | 213 | struct scm_fport *fp; |
9c29ac66 | 214 | |
78446828 MV |
215 | old = SCM_COERCE_OUTPORT (old); |
216 | new = SCM_COERCE_OUTPORT (new); | |
1bbd0b84 | 217 | |
3b3b36dd GB |
218 | SCM_VALIDATE_OPFPORT (1,old); |
219 | SCM_VALIDATE_OPFPORT (2,new); | |
ee149d03 JB |
220 | oldfd = SCM_FPORT_FDES (old); |
221 | fp = SCM_FSTREAM (new); | |
222 | newfd = fp->fdes; | |
223 | if (oldfd != newfd) | |
224 | { | |
afc5764c JB |
225 | scm_port *pt = SCM_PTAB_ENTRY (new); |
226 | scm_port *old_pt = SCM_PTAB_ENTRY (old); | |
16019956 | 227 | scm_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (new)]; |
afc5764c JB |
228 | |
229 | /* must flush to old fdes. */ | |
230 | if (pt->rw_active == SCM_PORT_WRITE) | |
affc96b5 | 231 | ptob->flush (new); |
afc5764c | 232 | else if (pt->rw_active == SCM_PORT_READ) |
affc96b5 | 233 | scm_end_input (new); |
ee149d03 JB |
234 | ans = dup2 (oldfd, newfd); |
235 | if (ans == -1) | |
1bbd0b84 | 236 | SCM_SYSERROR; |
afc5764c | 237 | pt->rw_random = old_pt->rw_random; |
ee149d03 | 238 | /* continue using existing buffers, even if inappropriate. */ |
ee149d03 | 239 | } |
02b754d3 | 240 | return SCM_UNSPECIFIED; |
0f2d19dd | 241 | } |
1bbd0b84 | 242 | #undef FUNC_NAME |
0f2d19dd | 243 | |
a1ec6916 | 244 | SCM_DEFINE (scm_dup_to_fdes, "dup->fdes", 1, 1, 0, |
1bbd0b84 | 245 | (SCM fd_or_port, SCM fd), |
1e6808ea MG |
246 | "Return a new integer file descriptor referring to the open file\n" |
247 | "designated by @var{fd_or_port}, which must be either an open\n" | |
248 | "file port or a file descriptor.") | |
1bbd0b84 | 249 | #define FUNC_NAME s_scm_dup_to_fdes |
a9488d12 GH |
250 | { |
251 | int oldfd, newfd, rv; | |
252 | ||
78446828 MV |
253 | fd_or_port = SCM_COERCE_OUTPORT (fd_or_port); |
254 | ||
a9488d12 GH |
255 | if (SCM_INUMP (fd_or_port)) |
256 | oldfd = SCM_INUM (fd_or_port); | |
257 | else | |
258 | { | |
3b3b36dd | 259 | SCM_VALIDATE_OPFPORT (1,fd_or_port); |
ee149d03 | 260 | oldfd = SCM_FPORT_FDES (fd_or_port); |
a9488d12 | 261 | } |
7a6f1ffa GH |
262 | |
263 | if (SCM_UNBNDP (fd)) | |
e38303a2 | 264 | { |
ee149d03 | 265 | newfd = dup (oldfd); |
7a6f1ffa | 266 | if (newfd == -1) |
1bbd0b84 | 267 | SCM_SYSERROR; |
7a6f1ffa GH |
268 | fd = SCM_MAKINUM (newfd); |
269 | } | |
270 | else | |
271 | { | |
c1bfcf60 | 272 | SCM_VALIDATE_INUM_COPY (2, fd, newfd); |
7a6f1ffa GH |
273 | if (oldfd != newfd) |
274 | { | |
275 | scm_evict_ports (newfd); /* see scsh manual. */ | |
ee149d03 | 276 | rv = dup2 (oldfd, newfd); |
7a6f1ffa | 277 | if (rv == -1) |
1bbd0b84 | 278 | SCM_SYSERROR; |
7a6f1ffa | 279 | } |
e38303a2 | 280 | } |
e38303a2 | 281 | return fd; |
a9488d12 | 282 | } |
1bbd0b84 | 283 | #undef FUNC_NAME |
a9488d12 | 284 | |
34526073 | 285 | |
c2ca4493 GH |
286 | SCM_DEFINE (scm_dup2, "dup2", 2, 0, 0, |
287 | (SCM oldfd, SCM newfd), | |
288 | "A simple wrapper for the @code{dup2} system call.\n" | |
289 | "Copies the file descriptor @var{oldfd} to descriptor\n" | |
290 | "number @var{newfd}, replacing the previous meaning\n" | |
291 | "of @var{newfd}. Both @var{oldfd} and @var{newfd} must\n" | |
292 | "be integers.\n" | |
293 | "Unlike for dup->fdes or primitive-move->fdes, no attempt\n" | |
34526073 | 294 | "is made to move away ports which are using @var{newfd}.\n" |
c2ca4493 GH |
295 | "The return value is unspecified.") |
296 | #define FUNC_NAME s_scm_dup2 | |
297 | { | |
298 | int c_oldfd; | |
299 | int c_newfd; | |
300 | int rv; | |
301 | ||
302 | SCM_VALIDATE_INUM_COPY (1, oldfd, c_oldfd); | |
303 | SCM_VALIDATE_INUM_COPY (2, newfd, c_newfd); | |
304 | rv = dup2 (c_oldfd, c_newfd); | |
305 | if (rv == -1) | |
306 | SCM_SYSERROR; | |
307 | return SCM_UNSPECIFIED; | |
308 | } | |
309 | #undef FUNC_NAME | |
310 | ||
a1ec6916 | 311 | SCM_DEFINE (scm_fileno, "fileno", 1, 0, 0, |
1bbd0b84 | 312 | (SCM port), |
1e6808ea MG |
313 | "Return the integer file descriptor underlying @var{port}. Does\n" |
314 | "not change its revealed count.") | |
1bbd0b84 | 315 | #define FUNC_NAME s_scm_fileno |
0f2d19dd | 316 | { |
78446828 | 317 | port = SCM_COERCE_OUTPORT (port); |
3b3b36dd | 318 | SCM_VALIDATE_OPFPORT (1,port); |
ee149d03 | 319 | return SCM_MAKINUM (SCM_FPORT_FDES (port)); |
0f2d19dd | 320 | } |
1bbd0b84 GB |
321 | #undef FUNC_NAME |
322 | ||
323 | /* GJB:FIXME:: why does this not throw | |
324 | an error if the arg is not a port? | |
325 | This proc as is would be better names isattyport? | |
326 | if it is not going to assume that the arg is a port */ | |
a1ec6916 | 327 | SCM_DEFINE (scm_isatty_p, "isatty?", 1, 0, 0, |
1bbd0b84 | 328 | (SCM port), |
1e6808ea MG |
329 | "Return @code{#t} if @var{port} is using a serial non--file\n" |
330 | "device, otherwise @code{#f}.") | |
1bbd0b84 | 331 | #define FUNC_NAME s_scm_isatty_p |
0f2d19dd JB |
332 | { |
333 | int rv; | |
78446828 MV |
334 | |
335 | port = SCM_COERCE_OUTPORT (port); | |
336 | ||
0c95b57d | 337 | if (!SCM_OPFPORTP (port)) |
10ccfad7 | 338 | return SCM_BOOL_F; |
ee149d03 JB |
339 | |
340 | rv = isatty (SCM_FPORT_FDES (port)); | |
1bbd0b84 | 341 | return SCM_BOOL(rv); |
0f2d19dd | 342 | } |
1bbd0b84 | 343 | #undef FUNC_NAME |
0f2d19dd JB |
344 | |
345 | ||
346 | ||
a1ec6916 | 347 | SCM_DEFINE (scm_fdopen, "fdopen", 2, 0, 0, |
1bbd0b84 | 348 | (SCM fdes, SCM modes), |
1e6808ea MG |
349 | "Return a new port based on the file descriptor @var{fdes}.\n" |
350 | "Modes are given by the string @var{modes}. The revealed count\n" | |
351 | "of the port is initialized to zero. The modes string is the\n" | |
352 | "same as that accepted by @ref{File Ports, open-file}.") | |
1bbd0b84 | 353 | #define FUNC_NAME s_scm_fdopen |
0f2d19dd | 354 | { |
3b3b36dd | 355 | SCM_VALIDATE_INUM (1,fdes); |
a6d9e5ab DH |
356 | SCM_VALIDATE_STRING (2, modes); |
357 | SCM_STRING_COERCE_0TERMINATION_X (modes); | |
19b27fa2 | 358 | |
a6d9e5ab | 359 | return scm_fdes_to_port (SCM_INUM (fdes), SCM_STRING_CHARS (modes), SCM_BOOL_F); |
0f2d19dd | 360 | } |
1bbd0b84 | 361 | #undef FUNC_NAME |
0f2d19dd JB |
362 | |
363 | ||
364 | ||
365 | /* Move a port's underlying file descriptor to a given value. | |
8b13c6b3 GH |
366 | * Returns #f if fdes is already the given value. |
367 | * #t if fdes moved. | |
0f2d19dd JB |
368 | * MOVE->FDES is implemented in Scheme and calls this primitive. |
369 | */ | |
a1ec6916 | 370 | SCM_DEFINE (scm_primitive_move_to_fdes, "primitive-move->fdes", 2, 0, 0, |
1bbd0b84 | 371 | (SCM port, SCM fd), |
b380b885 MD |
372 | "Moves the underlying file descriptor for @var{port} to the integer\n" |
373 | "value @var{fdes} without changing the revealed count of @var{port}.\n" | |
374 | "Any other ports already using this descriptor will be automatically\n" | |
375 | "shifted to new descriptors and their revealed counts reset to zero.\n" | |
376 | "The return value is @code{#f} if the file descriptor already had the\n" | |
377 | "required value or @code{#t} if it was moved.") | |
1bbd0b84 | 378 | #define FUNC_NAME s_scm_primitive_move_to_fdes |
0f2d19dd | 379 | { |
ee149d03 | 380 | struct scm_fport *stream; |
0f2d19dd JB |
381 | int old_fd; |
382 | int new_fd; | |
383 | int rv; | |
384 | ||
78446828 MV |
385 | port = SCM_COERCE_OUTPORT (port); |
386 | ||
3b3b36dd GB |
387 | SCM_VALIDATE_OPFPORT (1,port); |
388 | SCM_VALIDATE_INUM (2,fd); | |
ee149d03 JB |
389 | stream = SCM_FSTREAM (port); |
390 | old_fd = stream->fdes; | |
0f2d19dd JB |
391 | new_fd = SCM_INUM (fd); |
392 | if (old_fd == new_fd) | |
393 | { | |
8b13c6b3 | 394 | return SCM_BOOL_F; |
0f2d19dd JB |
395 | } |
396 | scm_evict_ports (new_fd); | |
397 | rv = dup2 (old_fd, new_fd); | |
398 | if (rv == -1) | |
1bbd0b84 | 399 | SCM_SYSERROR; |
ee149d03 | 400 | stream->fdes = new_fd; |
0f2d19dd | 401 | SCM_SYSCALL (close (old_fd)); |
8b13c6b3 | 402 | return SCM_BOOL_T; |
0f2d19dd | 403 | } |
1bbd0b84 | 404 | #undef FUNC_NAME |
0f2d19dd | 405 | |
0f2d19dd | 406 | /* Return a list of ports using a given file descriptor. */ |
3b3b36dd | 407 | SCM_DEFINE (scm_fdes_to_ports, "fdes->ports", 1, 0, 0, |
1bbd0b84 | 408 | (SCM fd), |
1e6808ea MG |
409 | "Return a list of existing ports which have @var{fdes} as an\n" |
410 | "underlying file descriptor, without changing their revealed\n" | |
411 | "counts.") | |
1bbd0b84 | 412 | #define FUNC_NAME s_scm_fdes_to_ports |
0f2d19dd JB |
413 | { |
414 | SCM result = SCM_EOL; | |
415 | int int_fd; | |
416 | int i; | |
417 | ||
3b3b36dd | 418 | SCM_VALIDATE_INUM_COPY (1,fd,int_fd); |
0f2d19dd | 419 | |
0f2d19dd JB |
420 | for (i = 0; i < scm_port_table_size; i++) |
421 | { | |
ee149d03 JB |
422 | if (SCM_OPFPORTP (scm_port_table[i]->port) |
423 | && ((struct scm_fport *) scm_port_table[i]->stream)->fdes == int_fd) | |
0f2d19dd JB |
424 | result = scm_cons (scm_port_table[i]->port, result); |
425 | } | |
0f2d19dd | 426 | return result; |
1bbd0b84 GB |
427 | } |
428 | #undef FUNC_NAME | |
0f2d19dd | 429 | |
1cc91f1b | 430 | |
0f2d19dd JB |
431 | void |
432 | scm_init_ioext () | |
0f2d19dd | 433 | { |
52cfc69b GH |
434 | scm_add_feature ("i/o-extensions"); |
435 | ||
8dc9439f | 436 | #ifndef SCM_MAGIC_SNARFER |
a0599745 | 437 | #include "libguile/ioext.x" |
8dc9439f | 438 | #endif |
0f2d19dd JB |
439 | } |
440 | ||
89e00824 ML |
441 | |
442 | /* | |
443 | Local Variables: | |
444 | c-file-style: "gnu" | |
445 | End: | |
446 | */ |