* validate.h, deprecated.h (SCM_VALIDATE_INUM, SCM_VALIDATE_INUM_COPY,
[bpt/guile.git] / libguile / ioext.c
1 /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
2 *
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.
7 *
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.
12 *
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
16 */
17
18
19 \f
20
21 #if HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #include <errno.h>
27
28 #include "libguile/_scm.h"
29 #include "libguile/ioext.h"
30 #include "libguile/fports.h"
31 #include "libguile/feature.h"
32 #include "libguile/ports.h"
33 #include "libguile/strings.h"
34 #include "libguile/validate.h"
35
36 #include <fcntl.h>
37
38 #ifdef HAVE_IO_H
39 #include <io.h>
40 #endif
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44 \f
45
46 SCM_DEFINE (scm_ftell, "ftell", 1, 0, 0,
47 (SCM fd_port),
48 "Return an integer representing the current position of\n"
49 "@var{fd/port}, measured from the beginning. Equivalent to:\n"
50 "\n"
51 "@lisp\n"
52 "(seek port 0 SEEK_CUR)\n"
53 "@end lisp")
54 #define FUNC_NAME s_scm_ftell
55 {
56 return scm_seek (fd_port, SCM_INUM0, SCM_I_MAKINUM (SEEK_CUR));
57 }
58 #undef FUNC_NAME
59
60 SCM_DEFINE (scm_redirect_port, "redirect-port", 2, 0, 0,
61 (SCM old, SCM new),
62 "This procedure takes two ports and duplicates the underlying file\n"
63 "descriptor from @var{old-port} into @var{new-port}. The\n"
64 "current file descriptor in @var{new-port} will be closed.\n"
65 "After the redirection the two ports will share a file position\n"
66 "and file status flags.\n\n"
67 "The return value is unspecified.\n\n"
68 "Unexpected behaviour can result if both ports are subsequently used\n"
69 "and the original and/or duplicate ports are buffered.\n\n"
70 "This procedure does not have any side effects on other ports or\n"
71 "revealed counts.")
72 #define FUNC_NAME s_scm_redirect_port
73 {
74 int ans, oldfd, newfd;
75 scm_t_fport *fp;
76
77 old = SCM_COERCE_OUTPORT (old);
78 new = SCM_COERCE_OUTPORT (new);
79
80 SCM_VALIDATE_OPFPORT (1, old);
81 SCM_VALIDATE_OPFPORT (2, new);
82 oldfd = SCM_FPORT_FDES (old);
83 fp = SCM_FSTREAM (new);
84 newfd = fp->fdes;
85 if (oldfd != newfd)
86 {
87 scm_t_port *pt = SCM_PTAB_ENTRY (new);
88 scm_t_port *old_pt = SCM_PTAB_ENTRY (old);
89 scm_t_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (new)];
90
91 /* must flush to old fdes. */
92 if (pt->rw_active == SCM_PORT_WRITE)
93 ptob->flush (new);
94 else if (pt->rw_active == SCM_PORT_READ)
95 scm_end_input (new);
96 ans = dup2 (oldfd, newfd);
97 if (ans == -1)
98 SCM_SYSERROR;
99 pt->rw_random = old_pt->rw_random;
100 /* continue using existing buffers, even if inappropriate. */
101 }
102 return SCM_UNSPECIFIED;
103 }
104 #undef FUNC_NAME
105
106 SCM_DEFINE (scm_dup_to_fdes, "dup->fdes", 1, 1, 0,
107 (SCM fd_or_port, SCM fd),
108 "Return a new integer file descriptor referring to the open file\n"
109 "designated by @var{fd_or_port}, which must be either an open\n"
110 "file port or a file descriptor.")
111 #define FUNC_NAME s_scm_dup_to_fdes
112 {
113 int oldfd, newfd, rv;
114
115 fd_or_port = SCM_COERCE_OUTPORT (fd_or_port);
116
117 if (SCM_INUMP (fd_or_port))
118 oldfd = SCM_INUM (fd_or_port);
119 else
120 {
121 SCM_VALIDATE_OPFPORT (1, fd_or_port);
122 oldfd = SCM_FPORT_FDES (fd_or_port);
123 }
124
125 if (SCM_UNBNDP (fd))
126 {
127 newfd = dup (oldfd);
128 if (newfd == -1)
129 SCM_SYSERROR;
130 fd = scm_from_int (newfd);
131 }
132 else
133 {
134 newfd = scm_to_int (fd);
135 if (oldfd != newfd)
136 {
137 scm_evict_ports (newfd); /* see scsh manual. */
138 rv = dup2 (oldfd, newfd);
139 if (rv == -1)
140 SCM_SYSERROR;
141 }
142 }
143 return fd;
144 }
145 #undef FUNC_NAME
146
147
148 SCM_DEFINE (scm_dup2, "dup2", 2, 0, 0,
149 (SCM oldfd, SCM newfd),
150 "A simple wrapper for the @code{dup2} system call.\n"
151 "Copies the file descriptor @var{oldfd} to descriptor\n"
152 "number @var{newfd}, replacing the previous meaning\n"
153 "of @var{newfd}. Both @var{oldfd} and @var{newfd} must\n"
154 "be integers.\n"
155 "Unlike for dup->fdes or primitive-move->fdes, no attempt\n"
156 "is made to move away ports which are using @var{newfd}.\n"
157 "The return value is unspecified.")
158 #define FUNC_NAME s_scm_dup2
159 {
160 int c_oldfd;
161 int c_newfd;
162 int rv;
163
164 c_oldfd = scm_to_int (oldfd);
165 c_newfd = scm_to_int (newfd);
166 rv = dup2 (c_oldfd, c_newfd);
167 if (rv == -1)
168 SCM_SYSERROR;
169 return SCM_UNSPECIFIED;
170 }
171 #undef FUNC_NAME
172
173 SCM_DEFINE (scm_fileno, "fileno", 1, 0, 0,
174 (SCM port),
175 "Return the integer file descriptor underlying @var{port}. Does\n"
176 "not change its revealed count.")
177 #define FUNC_NAME s_scm_fileno
178 {
179 port = SCM_COERCE_OUTPORT (port);
180 SCM_VALIDATE_OPFPORT (1, port);
181 return SCM_I_MAKINUM (SCM_FPORT_FDES (port));
182 }
183 #undef FUNC_NAME
184
185 /* GJB:FIXME:: why does this not throw
186 an error if the arg is not a port?
187 This proc as is would be better names isattyport?
188 if it is not going to assume that the arg is a port
189
190 [cmm] I don't see any problem with the above. why should a type
191 predicate assume _anything_ about its argument?
192 */
193 SCM_DEFINE (scm_isatty_p, "isatty?", 1, 0, 0,
194 (SCM port),
195 "Return @code{#t} if @var{port} is using a serial non--file\n"
196 "device, otherwise @code{#f}.")
197 #define FUNC_NAME s_scm_isatty_p
198 {
199 int rv;
200
201 port = SCM_COERCE_OUTPORT (port);
202
203 if (!SCM_OPFPORTP (port))
204 return SCM_BOOL_F;
205
206 rv = isatty (SCM_FPORT_FDES (port));
207 return scm_from_bool(rv);
208 }
209 #undef FUNC_NAME
210
211
212
213 SCM_DEFINE (scm_fdopen, "fdopen", 2, 0, 0,
214 (SCM fdes, SCM modes),
215 "Return a new port based on the file descriptor @var{fdes}.\n"
216 "Modes are given by the string @var{modes}. The revealed count\n"
217 "of the port is initialized to zero. The modes string is the\n"
218 "same as that accepted by @ref{File Ports, open-file}.")
219 #define FUNC_NAME s_scm_fdopen
220 {
221 SCM_VALIDATE_STRING (2, modes);
222 return scm_fdes_to_port (scm_to_int (fdes),
223 SCM_STRING_CHARS (modes), SCM_BOOL_F);
224 }
225 #undef FUNC_NAME
226
227
228
229 /* Move a port's underlying file descriptor to a given value.
230 * Returns #f if fdes is already the given value.
231 * #t if fdes moved.
232 * MOVE->FDES is implemented in Scheme and calls this primitive.
233 */
234 SCM_DEFINE (scm_primitive_move_to_fdes, "primitive-move->fdes", 2, 0, 0,
235 (SCM port, SCM fd),
236 "Moves the underlying file descriptor for @var{port} to the integer\n"
237 "value @var{fdes} without changing the revealed count of @var{port}.\n"
238 "Any other ports already using this descriptor will be automatically\n"
239 "shifted to new descriptors and their revealed counts reset to zero.\n"
240 "The return value is @code{#f} if the file descriptor already had the\n"
241 "required value or @code{#t} if it was moved.")
242 #define FUNC_NAME s_scm_primitive_move_to_fdes
243 {
244 scm_t_fport *stream;
245 int old_fd;
246 int new_fd;
247 int rv;
248
249 port = SCM_COERCE_OUTPORT (port);
250
251 SCM_VALIDATE_OPFPORT (1, port);
252 stream = SCM_FSTREAM (port);
253 old_fd = stream->fdes;
254 new_fd = scm_to_int (fd);
255 if (old_fd == new_fd)
256 {
257 return SCM_BOOL_F;
258 }
259 scm_evict_ports (new_fd);
260 rv = dup2 (old_fd, new_fd);
261 if (rv == -1)
262 SCM_SYSERROR;
263 stream->fdes = new_fd;
264 SCM_SYSCALL (close (old_fd));
265 return SCM_BOOL_T;
266 }
267 #undef FUNC_NAME
268
269 /* Return a list of ports using a given file descriptor. */
270 SCM_DEFINE (scm_fdes_to_ports, "fdes->ports", 1, 0, 0,
271 (SCM fd),
272 "Return a list of existing ports which have @var{fdes} as an\n"
273 "underlying file descriptor, without changing their revealed\n"
274 "counts.")
275 #define FUNC_NAME s_scm_fdes_to_ports
276 {
277 SCM result = SCM_EOL;
278 int int_fd;
279 long i;
280
281 int_fd = scm_to_int (fd);
282
283 scm_mutex_lock (&scm_i_port_table_mutex);
284 for (i = 0; i < scm_i_port_table_size; i++)
285 {
286 if (SCM_OPFPORTP (scm_i_port_table[i]->port)
287 && ((scm_t_fport *) scm_i_port_table[i]->stream)->fdes == int_fd)
288 result = scm_cons (scm_i_port_table[i]->port, result);
289 }
290 scm_mutex_unlock (&scm_i_port_table_mutex);
291 return result;
292 }
293 #undef FUNC_NAME
294
295
296 void
297 scm_init_ioext ()
298 {
299 scm_add_feature ("i/o-extensions");
300
301 #include "libguile/ioext.x"
302 }
303
304
305 /*
306 Local Variables:
307 c-file-style: "gnu"
308 End:
309 */