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