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