* srfi-modules.texi (SRFI-1 Fold and Map): Documented extended
[bpt/guile.git] / libguile / ioext.c
CommitLineData
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 54#include "libguile/strings.h"
a0599745 55#include "libguile/validate.h"
0f2d19dd 56
ee149d03
JB
57#include <fcntl.h>
58
95b88819
GH
59#ifdef HAVE_UNISTD_H
60#include <unistd.h>
61#endif
0f2d19dd
JB
62\f
63
a1ec6916 64SCM_DEFINE (scm_ftell, "ftell", 1, 0, 0,
1e6808ea
MG
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"
b380b885 70 "(seek port 0 SEEK_CUR)\n"
1e6808ea 71 "@end lisp")
1bbd0b84 72#define FUNC_NAME s_scm_ftell
0f2d19dd 73{
1e6808ea 74 return scm_seek (fd_port, SCM_INUM0, SCM_MAKINUM (SEEK_CUR));
ee149d03 75}
1bbd0b84 76#undef FUNC_NAME
0f2d19dd 77
a1ec6916 78SCM_DEFINE (scm_redirect_port, "redirect-port", 2, 0, 0,
1bbd0b84 79 (SCM old, SCM new),
b380b885
MD
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.")
1bbd0b84 90#define FUNC_NAME s_scm_redirect_port
0f2d19dd
JB
91{
92 int ans, oldfd, newfd;
92c2555f 93 scm_t_fport *fp;
9c29ac66 94
78446828
MV
95 old = SCM_COERCE_OUTPORT (old);
96 new = SCM_COERCE_OUTPORT (new);
1bbd0b84 97
3b3b36dd
GB
98 SCM_VALIDATE_OPFPORT (1,old);
99 SCM_VALIDATE_OPFPORT (2,new);
ee149d03
JB
100 oldfd = SCM_FPORT_FDES (old);
101 fp = SCM_FSTREAM (new);
102 newfd = fp->fdes;
103 if (oldfd != newfd)
104 {
92c2555f
MV
105 scm_t_port *pt = SCM_PTAB_ENTRY (new);
106 scm_t_port *old_pt = SCM_PTAB_ENTRY (old);
107 scm_t_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (new)];
afc5764c
JB
108
109 /* must flush to old fdes. */
110 if (pt->rw_active == SCM_PORT_WRITE)
affc96b5 111 ptob->flush (new);
afc5764c 112 else if (pt->rw_active == SCM_PORT_READ)
affc96b5 113 scm_end_input (new);
ee149d03
JB
114 ans = dup2 (oldfd, newfd);
115 if (ans == -1)
1bbd0b84 116 SCM_SYSERROR;
afc5764c 117 pt->rw_random = old_pt->rw_random;
ee149d03 118 /* continue using existing buffers, even if inappropriate. */
ee149d03 119 }
02b754d3 120 return SCM_UNSPECIFIED;
0f2d19dd 121}
1bbd0b84 122#undef FUNC_NAME
0f2d19dd 123
a1ec6916 124SCM_DEFINE (scm_dup_to_fdes, "dup->fdes", 1, 1, 0,
1bbd0b84 125 (SCM fd_or_port, SCM fd),
1e6808ea
MG
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.")
1bbd0b84 129#define FUNC_NAME s_scm_dup_to_fdes
a9488d12
GH
130{
131 int oldfd, newfd, rv;
132
78446828
MV
133 fd_or_port = SCM_COERCE_OUTPORT (fd_or_port);
134
a9488d12
GH
135 if (SCM_INUMP (fd_or_port))
136 oldfd = SCM_INUM (fd_or_port);
137 else
138 {
3b3b36dd 139 SCM_VALIDATE_OPFPORT (1,fd_or_port);
ee149d03 140 oldfd = SCM_FPORT_FDES (fd_or_port);
a9488d12 141 }
7a6f1ffa
GH
142
143 if (SCM_UNBNDP (fd))
e38303a2 144 {
ee149d03 145 newfd = dup (oldfd);
7a6f1ffa 146 if (newfd == -1)
1bbd0b84 147 SCM_SYSERROR;
7a6f1ffa
GH
148 fd = SCM_MAKINUM (newfd);
149 }
150 else
151 {
c1bfcf60 152 SCM_VALIDATE_INUM_COPY (2, fd, newfd);
7a6f1ffa
GH
153 if (oldfd != newfd)
154 {
155 scm_evict_ports (newfd); /* see scsh manual. */
ee149d03 156 rv = dup2 (oldfd, newfd);
7a6f1ffa 157 if (rv == -1)
1bbd0b84 158 SCM_SYSERROR;
7a6f1ffa 159 }
e38303a2 160 }
e38303a2 161 return fd;
a9488d12 162}
1bbd0b84 163#undef FUNC_NAME
a9488d12 164
34526073 165
c2ca4493
GH
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"
34526073 174 "is made to move away ports which are using @var{newfd}.\n"
c2ca4493
GH
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
a1ec6916 191SCM_DEFINE (scm_fileno, "fileno", 1, 0, 0,
1bbd0b84 192 (SCM port),
1e6808ea
MG
193 "Return the integer file descriptor underlying @var{port}. Does\n"
194 "not change its revealed count.")
1bbd0b84 195#define FUNC_NAME s_scm_fileno
0f2d19dd 196{
78446828 197 port = SCM_COERCE_OUTPORT (port);
3b3b36dd 198 SCM_VALIDATE_OPFPORT (1,port);
ee149d03 199 return SCM_MAKINUM (SCM_FPORT_FDES (port));
0f2d19dd 200}
1bbd0b84
GB
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?
1be6b49c
ML
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*/
a1ec6916 211SCM_DEFINE (scm_isatty_p, "isatty?", 1, 0, 0,
1bbd0b84 212 (SCM port),
1e6808ea
MG
213 "Return @code{#t} if @var{port} is using a serial non--file\n"
214 "device, otherwise @code{#f}.")
1bbd0b84 215#define FUNC_NAME s_scm_isatty_p
0f2d19dd
JB
216{
217 int rv;
78446828
MV
218
219 port = SCM_COERCE_OUTPORT (port);
220
0c95b57d 221 if (!SCM_OPFPORTP (port))
10ccfad7 222 return SCM_BOOL_F;
ee149d03
JB
223
224 rv = isatty (SCM_FPORT_FDES (port));
1bbd0b84 225 return SCM_BOOL(rv);
0f2d19dd 226}
1bbd0b84 227#undef FUNC_NAME
0f2d19dd
JB
228
229
230
a1ec6916 231SCM_DEFINE (scm_fdopen, "fdopen", 2, 0, 0,
1bbd0b84 232 (SCM fdes, SCM modes),
1e6808ea
MG
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}.")
1bbd0b84 237#define FUNC_NAME s_scm_fdopen
0f2d19dd 238{
3b3b36dd 239 SCM_VALIDATE_INUM (1,fdes);
a6d9e5ab
DH
240 SCM_VALIDATE_STRING (2, modes);
241 SCM_STRING_COERCE_0TERMINATION_X (modes);
19b27fa2 242
a6d9e5ab 243 return scm_fdes_to_port (SCM_INUM (fdes), SCM_STRING_CHARS (modes), SCM_BOOL_F);
0f2d19dd 244}
1bbd0b84 245#undef FUNC_NAME
0f2d19dd
JB
246
247
248
249/* Move a port's underlying file descriptor to a given value.
8b13c6b3
GH
250 * Returns #f if fdes is already the given value.
251 * #t if fdes moved.
0f2d19dd
JB
252 * MOVE->FDES is implemented in Scheme and calls this primitive.
253 */
a1ec6916 254SCM_DEFINE (scm_primitive_move_to_fdes, "primitive-move->fdes", 2, 0, 0,
1bbd0b84 255 (SCM port, SCM fd),
b380b885
MD
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.")
1bbd0b84 262#define FUNC_NAME s_scm_primitive_move_to_fdes
0f2d19dd 263{
92c2555f 264 scm_t_fport *stream;
0f2d19dd
JB
265 int old_fd;
266 int new_fd;
267 int rv;
268
78446828
MV
269 port = SCM_COERCE_OUTPORT (port);
270
3b3b36dd
GB
271 SCM_VALIDATE_OPFPORT (1,port);
272 SCM_VALIDATE_INUM (2,fd);
ee149d03
JB
273 stream = SCM_FSTREAM (port);
274 old_fd = stream->fdes;
0f2d19dd
JB
275 new_fd = SCM_INUM (fd);
276 if (old_fd == new_fd)
277 {
8b13c6b3 278 return SCM_BOOL_F;
0f2d19dd
JB
279 }
280 scm_evict_ports (new_fd);
281 rv = dup2 (old_fd, new_fd);
282 if (rv == -1)
1bbd0b84 283 SCM_SYSERROR;
ee149d03 284 stream->fdes = new_fd;
0f2d19dd 285 SCM_SYSCALL (close (old_fd));
8b13c6b3 286 return SCM_BOOL_T;
0f2d19dd 287}
1bbd0b84 288#undef FUNC_NAME
0f2d19dd 289
0f2d19dd 290/* Return a list of ports using a given file descriptor. */
3b3b36dd 291SCM_DEFINE (scm_fdes_to_ports, "fdes->ports", 1, 0, 0,
1bbd0b84 292 (SCM fd),
1e6808ea
MG
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.")
1bbd0b84 296#define FUNC_NAME s_scm_fdes_to_ports
0f2d19dd
JB
297{
298 SCM result = SCM_EOL;
299 int int_fd;
c014a02e 300 long i;
0f2d19dd 301
3b3b36dd 302 SCM_VALIDATE_INUM_COPY (1,fd,int_fd);
0f2d19dd 303
92c2555f 304 for (i = 0; i < scm_t_portable_size; i++)
0f2d19dd 305 {
92c2555f
MV
306 if (SCM_OPFPORTP (scm_t_portable[i]->port)
307 && ((scm_t_fport *) scm_t_portable[i]->stream)->fdes == int_fd)
308 result = scm_cons (scm_t_portable[i]->port, result);
0f2d19dd 309 }
0f2d19dd 310 return result;
1bbd0b84
GB
311}
312#undef FUNC_NAME
0f2d19dd 313
1cc91f1b 314
0f2d19dd
JB
315void
316scm_init_ioext ()
0f2d19dd 317{
52cfc69b
GH
318 scm_add_feature ("i/o-extensions");
319
8dc9439f 320#ifndef SCM_MAGIC_SNARFER
a0599745 321#include "libguile/ioext.x"
8dc9439f 322#endif
0f2d19dd
JB
323}
324
89e00824
ML
325
326/*
327 Local Variables:
328 c-file-style: "gnu"
329 End:
330*/