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