print: Fix printing of weak vectors.
[bpt/guile.git] / libguile / ioext.c
CommitLineData
2721f918 1/* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2006, 2011 Free Software Foundation, Inc.
0f2d19dd 2 *
73be1d9e 3 * This library is free software; you can redistribute it and/or
53befeb7
NJ
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
0f2d19dd 7 *
53befeb7
NJ
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
0f2d19dd 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
53befeb7
NJ
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
73be1d9e 17 */
1bbd0b84 18
1bbd0b84 19
0f2d19dd
JB
20\f
21
dbb605f5 22#ifdef HAVE_CONFIG_H
381534b3
RB
23# include <config.h>
24#endif
25
7beabedb 26#include <stdio.h>
e6e2e95a
MD
27#include <errno.h>
28
a0599745 29#include "libguile/_scm.h"
5dbc6c06 30#include "libguile/dynwind.h"
a0599745 31#include "libguile/feature.h"
5dbc6c06
HWN
32#include "libguile/fports.h"
33#include "libguile/hashtab.h"
34#include "libguile/ioext.h"
6d36532c 35#include "libguile/ports.h"
a0599745 36#include "libguile/strings.h"
a0599745 37#include "libguile/validate.h"
0f2d19dd 38
ee149d03
JB
39#include <fcntl.h>
40
ec65f5da
MV
41#ifdef HAVE_IO_H
42#include <io.h>
43#endif
95b88819
GH
44#ifdef HAVE_UNISTD_H
45#include <unistd.h>
46#endif
0f2d19dd
JB
47\f
48
a1ec6916 49SCM_DEFINE (scm_ftell, "ftell", 1, 0, 0,
1e6808ea
MG
50 (SCM fd_port),
51 "Return an integer representing the current position of\n"
b7e64f8b 52 "@var{fd_port}, measured from the beginning. Equivalent to:\n"
1e6808ea
MG
53 "\n"
54 "@lisp\n"
b380b885 55 "(seek port 0 SEEK_CUR)\n"
1e6808ea 56 "@end lisp")
1bbd0b84 57#define FUNC_NAME s_scm_ftell
0f2d19dd 58{
e11e83f3 59 return scm_seek (fd_port, SCM_INUM0, scm_from_int (SEEK_CUR));
ee149d03 60}
1bbd0b84 61#undef FUNC_NAME
0f2d19dd 62
a1ec6916 63SCM_DEFINE (scm_redirect_port, "redirect-port", 2, 0, 0,
1bbd0b84 64 (SCM old, SCM new),
b380b885 65 "This procedure takes two ports and duplicates the underlying file\n"
b7e64f8b
BT
66 "descriptor from @var{old} into @var{new}. The\n"
67 "current file descriptor in @var{new} will be closed.\n"
b380b885
MD
68 "After the redirection the two ports will share a file position\n"
69 "and file status flags.\n\n"
70 "The return value is unspecified.\n\n"
71 "Unexpected behaviour can result if both ports are subsequently used\n"
72 "and the original and/or duplicate ports are buffered.\n\n"
73 "This procedure does not have any side effects on other ports or\n"
74 "revealed counts.")
1bbd0b84 75#define FUNC_NAME s_scm_redirect_port
0f2d19dd
JB
76{
77 int ans, oldfd, newfd;
92c2555f 78 scm_t_fport *fp;
9c29ac66 79
78446828
MV
80 old = SCM_COERCE_OUTPORT (old);
81 new = SCM_COERCE_OUTPORT (new);
1bbd0b84 82
34d19ef6
HWN
83 SCM_VALIDATE_OPFPORT (1, old);
84 SCM_VALIDATE_OPFPORT (2, new);
ee149d03
JB
85 oldfd = SCM_FPORT_FDES (old);
86 fp = SCM_FSTREAM (new);
87 newfd = fp->fdes;
88 if (oldfd != newfd)
89 {
92c2555f
MV
90 scm_t_port *pt = SCM_PTAB_ENTRY (new);
91 scm_t_port *old_pt = SCM_PTAB_ENTRY (old);
62bd5d66 92 scm_t_ptob_descriptor *ptob = SCM_PORT_DESCRIPTOR (new);
afc5764c
JB
93
94 /* must flush to old fdes. */
95 if (pt->rw_active == SCM_PORT_WRITE)
affc96b5 96 ptob->flush (new);
afc5764c 97 else if (pt->rw_active == SCM_PORT_READ)
4251ae2e 98 scm_end_input_unlocked (new);
ee149d03
JB
99 ans = dup2 (oldfd, newfd);
100 if (ans == -1)
1bbd0b84 101 SCM_SYSERROR;
afc5764c 102 pt->rw_random = old_pt->rw_random;
ee149d03 103 /* continue using existing buffers, even if inappropriate. */
ee149d03 104 }
02b754d3 105 return SCM_UNSPECIFIED;
0f2d19dd 106}
1bbd0b84 107#undef FUNC_NAME
0f2d19dd 108
a1ec6916 109SCM_DEFINE (scm_dup_to_fdes, "dup->fdes", 1, 1, 0,
1bbd0b84 110 (SCM fd_or_port, SCM fd),
1e6808ea
MG
111 "Return a new integer file descriptor referring to the open file\n"
112 "designated by @var{fd_or_port}, which must be either an open\n"
113 "file port or a file descriptor.")
1bbd0b84 114#define FUNC_NAME s_scm_dup_to_fdes
a9488d12
GH
115{
116 int oldfd, newfd, rv;
117
78446828
MV
118 fd_or_port = SCM_COERCE_OUTPORT (fd_or_port);
119
e11e83f3
MV
120 if (scm_is_integer (fd_or_port))
121 oldfd = scm_to_int (fd_or_port);
a9488d12
GH
122 else
123 {
34d19ef6 124 SCM_VALIDATE_OPFPORT (1, fd_or_port);
ee149d03 125 oldfd = SCM_FPORT_FDES (fd_or_port);
a9488d12 126 }
7a6f1ffa
GH
127
128 if (SCM_UNBNDP (fd))
e38303a2 129 {
ee149d03 130 newfd = dup (oldfd);
7a6f1ffa 131 if (newfd == -1)
1bbd0b84 132 SCM_SYSERROR;
a55c2b68 133 fd = scm_from_int (newfd);
7a6f1ffa
GH
134 }
135 else
136 {
a55c2b68 137 newfd = scm_to_int (fd);
7a6f1ffa
GH
138 if (oldfd != newfd)
139 {
140 scm_evict_ports (newfd); /* see scsh manual. */
ee149d03 141 rv = dup2 (oldfd, newfd);
7a6f1ffa 142 if (rv == -1)
1bbd0b84 143 SCM_SYSERROR;
7a6f1ffa 144 }
e38303a2 145 }
e38303a2 146 return fd;
a9488d12 147}
1bbd0b84 148#undef FUNC_NAME
a9488d12 149
34526073 150
c2ca4493
GH
151SCM_DEFINE (scm_dup2, "dup2", 2, 0, 0,
152 (SCM oldfd, SCM newfd),
153 "A simple wrapper for the @code{dup2} system call.\n"
154 "Copies the file descriptor @var{oldfd} to descriptor\n"
155 "number @var{newfd}, replacing the previous meaning\n"
156 "of @var{newfd}. Both @var{oldfd} and @var{newfd} must\n"
157 "be integers.\n"
158 "Unlike for dup->fdes or primitive-move->fdes, no attempt\n"
34526073 159 "is made to move away ports which are using @var{newfd}.\n"
c2ca4493
GH
160 "The return value is unspecified.")
161#define FUNC_NAME s_scm_dup2
162{
163 int c_oldfd;
164 int c_newfd;
165 int rv;
166
a55c2b68
MV
167 c_oldfd = scm_to_int (oldfd);
168 c_newfd = scm_to_int (newfd);
c2ca4493
GH
169 rv = dup2 (c_oldfd, c_newfd);
170 if (rv == -1)
171 SCM_SYSERROR;
172 return SCM_UNSPECIFIED;
173}
174#undef FUNC_NAME
175
a1ec6916 176SCM_DEFINE (scm_fileno, "fileno", 1, 0, 0,
1bbd0b84 177 (SCM port),
1e6808ea
MG
178 "Return the integer file descriptor underlying @var{port}. Does\n"
179 "not change its revealed count.")
1bbd0b84 180#define FUNC_NAME s_scm_fileno
0f2d19dd 181{
78446828 182 port = SCM_COERCE_OUTPORT (port);
34d19ef6 183 SCM_VALIDATE_OPFPORT (1, port);
e11e83f3 184 return scm_from_int (SCM_FPORT_FDES (port));
0f2d19dd 185}
1bbd0b84
GB
186#undef FUNC_NAME
187
188/* GJB:FIXME:: why does this not throw
189 an error if the arg is not a port?
190 This proc as is would be better names isattyport?
1be6b49c
ML
191 if it is not going to assume that the arg is a port
192
193 [cmm] I don't see any problem with the above. why should a type
194 predicate assume _anything_ about its argument?
195*/
a1ec6916 196SCM_DEFINE (scm_isatty_p, "isatty?", 1, 0, 0,
1bbd0b84 197 (SCM port),
1e6808ea
MG
198 "Return @code{#t} if @var{port} is using a serial non--file\n"
199 "device, otherwise @code{#f}.")
1bbd0b84 200#define FUNC_NAME s_scm_isatty_p
0f2d19dd
JB
201{
202 int rv;
78446828
MV
203
204 port = SCM_COERCE_OUTPORT (port);
205
0c95b57d 206 if (!SCM_OPFPORTP (port))
10ccfad7 207 return SCM_BOOL_F;
ee149d03
JB
208
209 rv = isatty (SCM_FPORT_FDES (port));
7888309b 210 return scm_from_bool(rv);
0f2d19dd 211}
1bbd0b84 212#undef FUNC_NAME
0f2d19dd
JB
213
214
215
a1ec6916 216SCM_DEFINE (scm_fdopen, "fdopen", 2, 0, 0,
1bbd0b84 217 (SCM fdes, SCM modes),
1e6808ea
MG
218 "Return a new port based on the file descriptor @var{fdes}.\n"
219 "Modes are given by the string @var{modes}. The revealed count\n"
220 "of the port is initialized to zero. The modes string is the\n"
221 "same as that accepted by @ref{File Ports, open-file}.")
1bbd0b84 222#define FUNC_NAME s_scm_fdopen
0f2d19dd 223{
86e14f5c
MV
224 return scm_i_fdes_to_port (scm_to_int (fdes),
225 scm_i_mode_bits (modes), SCM_BOOL_F);
0f2d19dd 226}
1bbd0b84 227#undef FUNC_NAME
0f2d19dd
JB
228
229
230
231/* Move a port's underlying file descriptor to a given value.
8b13c6b3
GH
232 * Returns #f if fdes is already the given value.
233 * #t if fdes moved.
0f2d19dd
JB
234 * MOVE->FDES is implemented in Scheme and calls this primitive.
235 */
a1ec6916 236SCM_DEFINE (scm_primitive_move_to_fdes, "primitive-move->fdes", 2, 0, 0,
1bbd0b84 237 (SCM port, SCM fd),
b380b885 238 "Moves the underlying file descriptor for @var{port} to the integer\n"
b7e64f8b 239 "value @var{fd} without changing the revealed count of @var{port}.\n"
b380b885
MD
240 "Any other ports already using this descriptor will be automatically\n"
241 "shifted to new descriptors and their revealed counts reset to zero.\n"
242 "The return value is @code{#f} if the file descriptor already had the\n"
243 "required value or @code{#t} if it was moved.")
1bbd0b84 244#define FUNC_NAME s_scm_primitive_move_to_fdes
0f2d19dd 245{
92c2555f 246 scm_t_fport *stream;
0f2d19dd
JB
247 int old_fd;
248 int new_fd;
249 int rv;
250
78446828
MV
251 port = SCM_COERCE_OUTPORT (port);
252
34d19ef6 253 SCM_VALIDATE_OPFPORT (1, port);
ee149d03
JB
254 stream = SCM_FSTREAM (port);
255 old_fd = stream->fdes;
a55c2b68 256 new_fd = scm_to_int (fd);
0f2d19dd
JB
257 if (old_fd == new_fd)
258 {
8b13c6b3 259 return SCM_BOOL_F;
0f2d19dd
JB
260 }
261 scm_evict_ports (new_fd);
262 rv = dup2 (old_fd, new_fd);
263 if (rv == -1)
1bbd0b84 264 SCM_SYSERROR;
ee149d03 265 stream->fdes = new_fd;
0f2d19dd 266 SCM_SYSCALL (close (old_fd));
8b13c6b3 267 return SCM_BOOL_T;
0f2d19dd 268}
1bbd0b84 269#undef FUNC_NAME
0f2d19dd 270
5dbc6c06 271static SCM
2721f918 272get_matching_port (void *closure, SCM port, SCM result)
5dbc6c06
HWN
273{
274 int fd = * (int *) closure;
275 scm_t_port *entry = SCM_PTAB_ENTRY (port);
276
277 if (SCM_OPFPORTP (port)
278 && ((scm_t_fport *) entry->stream)->fdes == fd)
279 result = scm_cons (port, result);
280
281 return result;
282}
283
0f2d19dd 284/* Return a list of ports using a given file descriptor. */
3b3b36dd 285SCM_DEFINE (scm_fdes_to_ports, "fdes->ports", 1, 0, 0,
1bbd0b84 286 (SCM fd),
b7e64f8b 287 "Return a list of existing ports which have @var{fd} as an\n"
1e6808ea
MG
288 "underlying file descriptor, without changing their revealed\n"
289 "counts.")
1bbd0b84 290#define FUNC_NAME s_scm_fdes_to_ports
0f2d19dd
JB
291{
292 SCM result = SCM_EOL;
5dbc6c06 293 int int_fd = scm_to_int (fd);
0f2d19dd 294
2721f918
AW
295 result = scm_c_weak_set_fold (get_matching_port,
296 (void*) &int_fd, result,
297 scm_i_port_weak_set);
0f2d19dd 298 return result;
1bbd0b84
GB
299}
300#undef FUNC_NAME
0f2d19dd 301
1cc91f1b 302
0f2d19dd
JB
303void
304scm_init_ioext ()
0f2d19dd 305{
52cfc69b
GH
306 scm_add_feature ("i/o-extensions");
307
a0599745 308#include "libguile/ioext.x"
0f2d19dd
JB
309}
310
89e00824
ML
311
312/*
313 Local Variables:
314 c-file-style: "gnu"
315 End:
316*/