1 /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
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)
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.
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
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
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.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
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.
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. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
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"
64 SCM_DEFINE (scm_ftell
, "ftell", 1, 0, 0,
66 "Return an integer representing the current position of\n"
67 "@var{fd/port}, measured from the beginning. Equivalent to:\n"
70 "(seek port 0 SEEK_CUR)\n"
72 #define FUNC_NAME s_scm_ftell
74 return scm_seek (fd_port
, SCM_INUM0
, SCM_MAKINUM (SEEK_CUR
));
78 SCM_DEFINE (scm_redirect_port
, "redirect-port", 2, 0, 0,
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"
90 #define FUNC_NAME s_scm_redirect_port
92 int ans
, oldfd
, newfd
;
95 old
= SCM_COERCE_OUTPORT (old
);
96 new = SCM_COERCE_OUTPORT (new);
98 SCM_VALIDATE_OPFPORT (1,old
);
99 SCM_VALIDATE_OPFPORT (2,new);
100 oldfd
= SCM_FPORT_FDES (old
);
101 fp
= SCM_FSTREAM (new);
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)];
109 /* must flush to old fdes. */
110 if (pt
->rw_active
== SCM_PORT_WRITE
)
112 else if (pt
->rw_active
== SCM_PORT_READ
)
114 ans
= dup2 (oldfd
, newfd
);
117 pt
->rw_random
= old_pt
->rw_random
;
118 /* continue using existing buffers, even if inappropriate. */
120 return SCM_UNSPECIFIED
;
124 SCM_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
131 int oldfd
, newfd
, rv
;
133 fd_or_port
= SCM_COERCE_OUTPORT (fd_or_port
);
135 if (SCM_INUMP (fd_or_port
))
136 oldfd
= SCM_INUM (fd_or_port
);
139 SCM_VALIDATE_OPFPORT (1,fd_or_port
);
140 oldfd
= SCM_FPORT_FDES (fd_or_port
);
148 fd
= SCM_MAKINUM (newfd
);
152 SCM_VALIDATE_INUM_COPY (2, fd
, newfd
);
155 scm_evict_ports (newfd
); /* see scsh manual. */
156 rv
= dup2 (oldfd
, newfd
);
166 SCM_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"
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
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
);
187 return SCM_UNSPECIFIED
;
191 SCM_DEFINE (scm_fileno
, "fileno", 1, 0, 0,
193 "Return the integer file descriptor underlying @var{port}. Does\n"
194 "not change its revealed count.")
195 #define FUNC_NAME s_scm_fileno
197 port
= SCM_COERCE_OUTPORT (port
);
198 SCM_VALIDATE_OPFPORT (1,port
);
199 return SCM_MAKINUM (SCM_FPORT_FDES (port
));
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
208 [cmm] I don't see any problem with the above. why should a type
209 predicate assume _anything_ about its argument?
211 SCM_DEFINE (scm_isatty_p
, "isatty?", 1, 0, 0,
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
219 port
= SCM_COERCE_OUTPORT (port
);
221 if (!SCM_OPFPORTP (port
))
224 rv
= isatty (SCM_FPORT_FDES (port
));
231 SCM_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
239 SCM_VALIDATE_INUM (1,fdes
);
240 SCM_VALIDATE_STRING (2, modes
);
241 SCM_STRING_COERCE_0TERMINATION_X (modes
);
243 return scm_fdes_to_port (SCM_INUM (fdes
), SCM_STRING_CHARS (modes
), SCM_BOOL_F
);
249 /* Move a port's underlying file descriptor to a given value.
250 * Returns #f if fdes is already the given value.
252 * MOVE->FDES is implemented in Scheme and calls this primitive.
254 SCM_DEFINE (scm_primitive_move_to_fdes
, "primitive-move->fdes", 2, 0, 0,
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
269 port
= SCM_COERCE_OUTPORT (port
);
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
)
280 scm_evict_ports (new_fd
);
281 rv
= dup2 (old_fd
, new_fd
);
284 stream
->fdes
= new_fd
;
285 SCM_SYSCALL (close (old_fd
));
290 /* Return a list of ports using a given file descriptor. */
291 SCM_DEFINE (scm_fdes_to_ports
, "fdes->ports", 1, 0, 0,
293 "Return a list of existing ports which have @var{fdes} as an\n"
294 "underlying file descriptor, without changing their revealed\n"
296 #define FUNC_NAME s_scm_fdes_to_ports
298 SCM result
= SCM_EOL
;
302 SCM_VALIDATE_INUM_COPY (1,fd
,int_fd
);
304 for (i
= 0; i
< scm_port_table_size
; i
++)
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
);
318 scm_add_feature ("i/o-extensions");
320 #ifndef SCM_MAGIC_SNARFER
321 #include "libguile/ioext.x"