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