Commit | Line | Data |
---|---|---|
afc5764c | 1 | /* Copyright (C) 1995, 1996, 1997, 1998, 1999 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 | ||
47 | ||
48 | #include <stdio.h> | |
0f2d19dd | 49 | #include "_scm.h" |
ee149d03 | 50 | #include "ports.h" |
1146b6cd | 51 | #include "read.h" |
20e6290e | 52 | #include "fports.h" |
1146b6cd GH |
53 | #include "unif.h" |
54 | #include "chars.h" | |
c8589747 | 55 | #include "feature.h" |
20e6290e | 56 | |
b6791b2e | 57 | #include "validate.h" |
20e6290e | 58 | #include "ioext.h" |
0f2d19dd | 59 | |
ee149d03 JB |
60 | #include <fcntl.h> |
61 | ||
95b88819 GH |
62 | #ifdef HAVE_STRING_H |
63 | #include <string.h> | |
64 | #endif | |
65 | #ifdef HAVE_UNISTD_H | |
66 | #include <unistd.h> | |
67 | #endif | |
0f2d19dd JB |
68 | \f |
69 | ||
a1ec6916 | 70 | SCM_DEFINE (scm_read_delimited_x, "%read-delimited!", 3, 3, 0, |
1bbd0b84 | 71 | (SCM delims, SCM buf, SCM gobble, SCM port, SCM start, SCM end), |
b380b885 MD |
72 | "Read characters from @var{port} into @var{buf} until one of the\n" |
73 | "characters in the @var{delims} string is encountered. If @var{gobble?}\n" | |
74 | "is true, store the delimiter character in @var{buf} as well; otherwise,\n" | |
75 | "discard it. If @var{port} is not specified, use the value of\n" | |
76 | "@code{(current-input-port)}. If @var{start} or @var{end} are specified,\n" | |
77 | "store data only into the substring of @var{buf} bounded by @var{start}\n" | |
78 | "and @var{end} (which default to the beginning and end of the buffer,\n" | |
79 | "respectively).\n\n" | |
80 | "Return a pair consisting of the delimiter that terminated the string and\n" | |
81 | "the number of characters read. If reading stopped at the end of file,\n" | |
82 | "the delimiter returned is the @var{eof-object}; if the buffer was filled\n" | |
83 | "without encountering a delimiter, this value is @var{#f}.") | |
1bbd0b84 | 84 | #define FUNC_NAME s_scm_read_delimited_x |
1146b6cd GH |
85 | { |
86 | long j; | |
87 | char *cbuf; | |
88 | long cstart; | |
1bbd0b84 | 89 | long cend, tend; |
1146b6cd GH |
90 | int c; |
91 | char *cdelims; | |
92 | int num_delims; | |
93 | ||
3b3b36dd | 94 | SCM_VALIDATE_ROSTRING_COPY (1,delims,cdelims); |
ae2fa5bc | 95 | num_delims = SCM_ROLENGTH (delims); |
3b3b36dd | 96 | SCM_VALIDATE_STRING_COPY (2,buf,cbuf); |
1146b6cd GH |
97 | cend = SCM_LENGTH (buf); |
98 | if (SCM_UNBNDP (port)) | |
99 | port = scm_cur_inp; | |
100 | else | |
3b3b36dd | 101 | SCM_VALIDATE_OPINPORT (4,port); |
1146b6cd | 102 | |
3b3b36dd | 103 | SCM_VALIDATE_INUM_DEF_COPY (5,start,0,cstart); |
c1bfcf60 | 104 | SCM_ASSERT_RANGE(5, start, cstart >= 0 && cstart < cend); |
1146b6cd | 105 | |
3b3b36dd | 106 | SCM_VALIDATE_INUM_DEF_COPY (6,end,cend,tend); |
c1bfcf60 GB |
107 | SCM_ASSERT_RANGE(6, end, tend > cstart && tend <= cend); |
108 | ||
1bbd0b84 | 109 | cend = tend; |
1146b6cd GH |
110 | |
111 | for (j = cstart; j < cend; j++) | |
112 | { | |
113 | int k; | |
114 | ||
b7f3516f | 115 | c = scm_getc (port); |
1146b6cd GH |
116 | for (k = 0; k < num_delims; k++) |
117 | { | |
118 | if (cdelims[k] == c) | |
119 | { | |
120 | if (SCM_FALSEP (gobble)) | |
b7f3516f | 121 | scm_ungetc (c, port); |
1146b6cd | 122 | |
7866a09b | 123 | return scm_cons (SCM_MAKE_CHAR (c), |
1146b6cd GH |
124 | scm_long2num (j - cstart)); |
125 | } | |
126 | } | |
127 | if (c == EOF) | |
128 | return scm_cons (SCM_EOF_VAL, | |
129 | scm_long2num (j - cstart)); | |
130 | ||
131 | cbuf[j] = c; | |
132 | } | |
133 | return scm_cons (SCM_BOOL_F, scm_long2num (j - cstart)); | |
134 | } | |
1bbd0b84 | 135 | #undef FUNC_NAME |
1146b6cd | 136 | |
ee149d03 JB |
137 | static unsigned char * |
138 | scm_do_read_line (SCM port, int *len_p) | |
139 | { | |
afc5764c | 140 | scm_port *pt = SCM_PTAB_ENTRY (port); |
ee149d03 JB |
141 | unsigned char *end; |
142 | ||
143 | /* I thought reading lines was simple. Mercy me. */ | |
144 | ||
6c951427 GH |
145 | /* The common case: the buffer contains a complete line. |
146 | This needs to be fast. */ | |
ee149d03 JB |
147 | if ((end = memchr (pt->read_pos, '\n', (pt->read_end - pt->read_pos))) |
148 | != 0) | |
149 | { | |
150 | int buf_len = (end + 1) - pt->read_pos; | |
151 | /* Allocate a buffer of the perfect size. */ | |
66a10575 | 152 | unsigned char *buf = scm_must_malloc (buf_len + 1, "%read-line"); |
ee149d03 JB |
153 | |
154 | memcpy (buf, pt->read_pos, buf_len); | |
155 | pt->read_pos += buf_len; | |
156 | ||
157 | buf[buf_len] = '\0'; | |
158 | ||
159 | *len_p = buf_len; | |
160 | return buf; | |
161 | } | |
162 | ||
6c951427 | 163 | /* The buffer contains no newlines. */ |
ee149d03 JB |
164 | { |
165 | /* When live, len is always the number of characters in the | |
166 | current buffer that are part of the current line. */ | |
167 | int len = (pt->read_end - pt->read_pos); | |
168 | int buf_size = (len < 50) ? 60 : len * 2; | |
169 | /* Invariant: buf always has buf_size + 1 characters allocated; | |
170 | the `+ 1' is for the final '\0'. */ | |
66a10575 | 171 | unsigned char *buf = scm_must_malloc (buf_size + 1, "%read-line"); |
ee149d03 | 172 | int buf_len = 0; |
ee149d03 JB |
173 | |
174 | for (;;) | |
175 | { | |
176 | if (buf_len + len > buf_size) | |
177 | { | |
178 | int new_size = (buf_len + len) * 2; | |
66a10575 JB |
179 | buf = scm_must_realloc (buf, buf_size + 1, new_size + 1, |
180 | "%read-line"); | |
ee149d03 JB |
181 | buf_size = new_size; |
182 | } | |
183 | ||
184 | /* Copy what we've got out of the port, into our buffer. */ | |
185 | memcpy (buf + buf_len, pt->read_pos, len); | |
186 | buf_len += len; | |
187 | pt->read_pos += len; | |
188 | ||
189 | /* If we had seen a newline, we're done now. */ | |
190 | if (end) | |
191 | break; | |
192 | ||
5c070ca7 | 193 | /* Get more characters. */ |
affc96b5 | 194 | if (scm_fill_input (port) == EOF) |
ee149d03 JB |
195 | { |
196 | /* If we're missing a final newline in the file, return | |
197 | what we did get, sans newline. */ | |
198 | if (buf_len > 0) | |
199 | break; | |
200 | ||
201 | free (buf); | |
202 | return 0; | |
203 | } | |
204 | ||
ee149d03 JB |
205 | /* Search the buffer for newlines. */ |
206 | if ((end = memchr (pt->read_pos, '\n', | |
207 | (len = (pt->read_end - pt->read_pos)))) | |
208 | != 0) | |
209 | len = (end - pt->read_pos) + 1; | |
210 | } | |
211 | ||
212 | /* I wonder how expensive this realloc is. */ | |
66a10575 | 213 | buf = scm_must_realloc (buf, buf_size + 1, buf_len + 1, "%read-line"); |
ee149d03 JB |
214 | buf[buf_len] = '\0'; |
215 | *len_p = buf_len; | |
216 | return buf; | |
217 | } | |
218 | } | |
219 | ||
220 | ||
3e2043c4 | 221 | /* |
ee149d03 | 222 | * %read-line |
848f2a01 TP |
223 | * truncates any terminating newline from its input, and returns |
224 | * a cons of the string read and its terminating character. Doing | |
225 | * so makes it easy to implement the hairy `read-line' options | |
226 | * efficiently in Scheme. | |
3e2043c4 TP |
227 | */ |
228 | ||
a1ec6916 | 229 | SCM_DEFINE (scm_read_line, "%read-line", 0, 1, 0, |
1bbd0b84 | 230 | (SCM port), |
b380b885 MD |
231 | "Read a newline-terminated line from @var{port}, allocating storage as\n" |
232 | "necessary. The newline terminator (if any) is removed from the string,\n" | |
233 | "and a pair consisting of the line and its delimiter is returned. The\n" | |
234 | "delimiter may be either a newline or the @var{eof-object}; if\n" | |
235 | "@code{%read-line} is called at the end of file, it returns the pair\n" | |
236 | "@code{(#<eof> . #<eof>)}.") | |
1bbd0b84 | 237 | #define FUNC_NAME s_scm_read_line |
3cb988bd | 238 | { |
afc5764c | 239 | scm_port *pt; |
3cb988bd | 240 | char *s; |
848f2a01 TP |
241 | int slen; |
242 | SCM line, term; | |
3cb988bd TP |
243 | |
244 | if (SCM_UNBNDP (port)) | |
245 | port = scm_cur_inp; | |
3b3b36dd | 246 | SCM_VALIDATE_OPINPORT (1,port); |
3cb988bd | 247 | |
afc5764c JB |
248 | pt = SCM_PTAB_ENTRY (port); |
249 | if (pt->rw_active == SCM_PORT_WRITE) | |
affc96b5 | 250 | scm_ptobs[SCM_PTOBNUM (port)].flush (port); |
afc5764c | 251 | |
848f2a01 TP |
252 | s = scm_do_read_line (port, &slen); |
253 | ||
3e2043c4 | 254 | if (s == NULL) |
848f2a01 | 255 | term = line = SCM_EOF_VAL; |
3e2043c4 TP |
256 | else |
257 | { | |
848f2a01 TP |
258 | if (s[slen-1] == '\n') |
259 | { | |
7866a09b | 260 | term = SCM_MAKE_CHAR ('\n'); |
ee149d03 JB |
261 | s[slen-1] = '\0'; |
262 | line = scm_take_str (s, slen-1); | |
66a10575 | 263 | scm_done_malloc (-1); |
ee149d03 | 264 | SCM_INCLINE (port); |
848f2a01 TP |
265 | } |
266 | else | |
267 | { | |
268 | /* Fix: we should check for eof on the port before assuming this. */ | |
269 | term = SCM_EOF_VAL; | |
ee149d03 | 270 | line = scm_take_str (s, slen); |
afc5764c | 271 | SCM_COL (port) += slen; |
848f2a01 | 272 | } |
3e2043c4 | 273 | } |
848f2a01 | 274 | |
afc5764c JB |
275 | if (pt->rw_random) |
276 | pt->rw_active = SCM_PORT_READ; | |
277 | ||
848f2a01 | 278 | return scm_cons (line, term); |
3cb988bd | 279 | } |
1bbd0b84 | 280 | #undef FUNC_NAME |
3cb988bd | 281 | |
a1ec6916 | 282 | SCM_DEFINE (scm_write_line, "write-line", 1, 1, 0, |
1bbd0b84 | 283 | (SCM obj, SCM port), |
b380b885 MD |
284 | "Display @var{obj} and a newline character to @var{port}. If @var{port}\n" |
285 | "is not specified, @code{(current-output-port)} is used. This function\n" | |
286 | "is equivalent to:\n\n" | |
287 | "@smalllisp\n" | |
288 | "(display obj [port])\n" | |
289 | "(newline [port])\n" | |
290 | "@end smalllisp") | |
1bbd0b84 | 291 | #define FUNC_NAME s_scm_write_line |
1146b6cd GH |
292 | { |
293 | scm_display (obj, port); | |
294 | return scm_newline (port); | |
295 | } | |
1bbd0b84 | 296 | #undef FUNC_NAME |
1146b6cd | 297 | |
a1ec6916 | 298 | SCM_DEFINE (scm_ftell, "ftell", 1, 0, 0, |
1bbd0b84 | 299 | (SCM object), |
b380b885 MD |
300 | "Returns an integer representing the current position of @var{fd/port},\n" |
301 | "measured from the beginning. Equivalent to:\n" | |
302 | "@smalllisp\n" | |
303 | "(seek port 0 SEEK_CUR)\n" | |
304 | "@end smalllisp") | |
1bbd0b84 | 305 | #define FUNC_NAME s_scm_ftell |
0f2d19dd | 306 | { |
c94577b4 | 307 | return scm_seek (object, SCM_INUM0, SCM_MAKINUM (SEEK_CUR)); |
ee149d03 | 308 | } |
1bbd0b84 | 309 | #undef FUNC_NAME |
0f2d19dd | 310 | |
a1ec6916 | 311 | SCM_DEFINE (scm_fseek, "fseek", 3, 0, 0, |
1bbd0b84 | 312 | (SCM object, SCM offset, SCM whence), |
b380b885 MD |
313 | "Obsolete. Almost the same as seek, above, but the return value is\n" |
314 | "unspecified.") | |
1bbd0b84 | 315 | #define FUNC_NAME s_scm_fseek |
0f2d19dd | 316 | { |
c94577b4 | 317 | scm_seek (object, offset, whence); |
02b754d3 | 318 | return SCM_UNSPECIFIED; |
0f2d19dd | 319 | } |
1bbd0b84 | 320 | #undef FUNC_NAME |
0f2d19dd | 321 | |
a1ec6916 | 322 | SCM_DEFINE (scm_redirect_port, "redirect-port", 2, 0, 0, |
1bbd0b84 | 323 | (SCM old, SCM new), |
b380b885 MD |
324 | "This procedure takes two ports and duplicates the underlying file\n" |
325 | "descriptor from @var{old-port} into @var{new-port}. The\n" | |
326 | "current file descriptor in @var{new-port} will be closed.\n" | |
327 | "After the redirection the two ports will share a file position\n" | |
328 | "and file status flags.\n\n" | |
329 | "The return value is unspecified.\n\n" | |
330 | "Unexpected behaviour can result if both ports are subsequently used\n" | |
331 | "and the original and/or duplicate ports are buffered.\n\n" | |
332 | "This procedure does not have any side effects on other ports or\n" | |
333 | "revealed counts.") | |
1bbd0b84 | 334 | #define FUNC_NAME s_scm_redirect_port |
0f2d19dd JB |
335 | { |
336 | int ans, oldfd, newfd; | |
ee149d03 | 337 | struct scm_fport *fp; |
9c29ac66 | 338 | |
78446828 MV |
339 | old = SCM_COERCE_OUTPORT (old); |
340 | new = SCM_COERCE_OUTPORT (new); | |
1bbd0b84 | 341 | |
3b3b36dd GB |
342 | SCM_VALIDATE_OPFPORT (1,old); |
343 | SCM_VALIDATE_OPFPORT (2,new); | |
ee149d03 JB |
344 | oldfd = SCM_FPORT_FDES (old); |
345 | fp = SCM_FSTREAM (new); | |
346 | newfd = fp->fdes; | |
347 | if (oldfd != newfd) | |
348 | { | |
afc5764c JB |
349 | scm_port *pt = SCM_PTAB_ENTRY (new); |
350 | scm_port *old_pt = SCM_PTAB_ENTRY (old); | |
16019956 | 351 | scm_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (new)]; |
afc5764c JB |
352 | |
353 | /* must flush to old fdes. */ | |
354 | if (pt->rw_active == SCM_PORT_WRITE) | |
affc96b5 | 355 | ptob->flush (new); |
afc5764c | 356 | else if (pt->rw_active == SCM_PORT_READ) |
affc96b5 | 357 | scm_end_input (new); |
ee149d03 JB |
358 | ans = dup2 (oldfd, newfd); |
359 | if (ans == -1) | |
1bbd0b84 | 360 | SCM_SYSERROR; |
afc5764c | 361 | pt->rw_random = old_pt->rw_random; |
ee149d03 | 362 | /* continue using existing buffers, even if inappropriate. */ |
ee149d03 | 363 | } |
02b754d3 | 364 | return SCM_UNSPECIFIED; |
0f2d19dd | 365 | } |
1bbd0b84 | 366 | #undef FUNC_NAME |
0f2d19dd | 367 | |
a1ec6916 | 368 | SCM_DEFINE (scm_dup_to_fdes, "dup->fdes", 1, 1, 0, |
1bbd0b84 | 369 | (SCM fd_or_port, SCM fd), |
b380b885 | 370 | "Returns an integer file descriptor.") |
1bbd0b84 | 371 | #define FUNC_NAME s_scm_dup_to_fdes |
a9488d12 GH |
372 | { |
373 | int oldfd, newfd, rv; | |
374 | ||
78446828 MV |
375 | fd_or_port = SCM_COERCE_OUTPORT (fd_or_port); |
376 | ||
a9488d12 GH |
377 | if (SCM_INUMP (fd_or_port)) |
378 | oldfd = SCM_INUM (fd_or_port); | |
379 | else | |
380 | { | |
3b3b36dd | 381 | SCM_VALIDATE_OPFPORT (1,fd_or_port); |
ee149d03 | 382 | oldfd = SCM_FPORT_FDES (fd_or_port); |
a9488d12 | 383 | } |
7a6f1ffa GH |
384 | |
385 | if (SCM_UNBNDP (fd)) | |
e38303a2 | 386 | { |
ee149d03 | 387 | newfd = dup (oldfd); |
7a6f1ffa | 388 | if (newfd == -1) |
1bbd0b84 | 389 | SCM_SYSERROR; |
7a6f1ffa GH |
390 | fd = SCM_MAKINUM (newfd); |
391 | } | |
392 | else | |
393 | { | |
c1bfcf60 | 394 | SCM_VALIDATE_INUM_COPY (2, fd, newfd); |
7a6f1ffa GH |
395 | if (oldfd != newfd) |
396 | { | |
397 | scm_evict_ports (newfd); /* see scsh manual. */ | |
ee149d03 | 398 | rv = dup2 (oldfd, newfd); |
7a6f1ffa | 399 | if (rv == -1) |
1bbd0b84 | 400 | SCM_SYSERROR; |
7a6f1ffa | 401 | } |
e38303a2 | 402 | } |
e38303a2 | 403 | return fd; |
a9488d12 | 404 | } |
1bbd0b84 | 405 | #undef FUNC_NAME |
a9488d12 | 406 | |
a1ec6916 | 407 | SCM_DEFINE (scm_fileno, "fileno", 1, 0, 0, |
1bbd0b84 | 408 | (SCM port), |
b380b885 MD |
409 | "Returns the integer file descriptor underlying @var{port}.\n" |
410 | "Does not change its revealed count.") | |
1bbd0b84 | 411 | #define FUNC_NAME s_scm_fileno |
0f2d19dd | 412 | { |
78446828 | 413 | port = SCM_COERCE_OUTPORT (port); |
3b3b36dd | 414 | SCM_VALIDATE_OPFPORT (1,port); |
ee149d03 | 415 | return SCM_MAKINUM (SCM_FPORT_FDES (port)); |
0f2d19dd | 416 | } |
1bbd0b84 GB |
417 | #undef FUNC_NAME |
418 | ||
419 | /* GJB:FIXME:: why does this not throw | |
420 | an error if the arg is not a port? | |
421 | This proc as is would be better names isattyport? | |
422 | if it is not going to assume that the arg is a port */ | |
a1ec6916 | 423 | SCM_DEFINE (scm_isatty_p, "isatty?", 1, 0, 0, |
1bbd0b84 | 424 | (SCM port), |
b380b885 MD |
425 | "Returns @code{#t} if @var{port} is using a serial\n" |
426 | "non-file device, otherwise @code{#f}.") | |
1bbd0b84 | 427 | #define FUNC_NAME s_scm_isatty_p |
0f2d19dd JB |
428 | { |
429 | int rv; | |
78446828 MV |
430 | |
431 | port = SCM_COERCE_OUTPORT (port); | |
432 | ||
0c95b57d | 433 | if (!SCM_OPFPORTP (port)) |
10ccfad7 | 434 | return SCM_BOOL_F; |
ee149d03 JB |
435 | |
436 | rv = isatty (SCM_FPORT_FDES (port)); | |
1bbd0b84 | 437 | return SCM_BOOL(rv); |
0f2d19dd | 438 | } |
1bbd0b84 | 439 | #undef FUNC_NAME |
0f2d19dd JB |
440 | |
441 | ||
442 | ||
a1ec6916 | 443 | SCM_DEFINE (scm_fdopen, "fdopen", 2, 0, 0, |
1bbd0b84 | 444 | (SCM fdes, SCM modes), |
b380b885 MD |
445 | "Returns a new port based on the file descriptor @var{fdes}.\n" |
446 | "Modes are given by the string @var{modes}. The revealed count of the port\n" | |
447 | "is initialized to zero. The modes string is the same as that accepted\n" | |
448 | "by @ref{File Ports, open-file}.") | |
1bbd0b84 | 449 | #define FUNC_NAME s_scm_fdopen |
0f2d19dd | 450 | { |
0f2d19dd JB |
451 | SCM port; |
452 | ||
3b3b36dd GB |
453 | SCM_VALIDATE_INUM (1,fdes); |
454 | SCM_VALIDATE_ROSTRING (2,modes); | |
89958ad0 | 455 | SCM_COERCE_SUBSTR (modes); |
ee149d03 | 456 | port = scm_fdes_to_port (SCM_INUM (fdes), SCM_ROCHARS (modes), SCM_BOOL_F); |
0f2d19dd JB |
457 | return port; |
458 | } | |
1bbd0b84 | 459 | #undef FUNC_NAME |
0f2d19dd JB |
460 | |
461 | ||
462 | ||
463 | /* Move a port's underlying file descriptor to a given value. | |
8b13c6b3 GH |
464 | * Returns #f if fdes is already the given value. |
465 | * #t if fdes moved. | |
0f2d19dd JB |
466 | * MOVE->FDES is implemented in Scheme and calls this primitive. |
467 | */ | |
a1ec6916 | 468 | SCM_DEFINE (scm_primitive_move_to_fdes, "primitive-move->fdes", 2, 0, 0, |
1bbd0b84 | 469 | (SCM port, SCM fd), |
b380b885 MD |
470 | "Moves the underlying file descriptor for @var{port} to the integer\n" |
471 | "value @var{fdes} without changing the revealed count of @var{port}.\n" | |
472 | "Any other ports already using this descriptor will be automatically\n" | |
473 | "shifted to new descriptors and their revealed counts reset to zero.\n" | |
474 | "The return value is @code{#f} if the file descriptor already had the\n" | |
475 | "required value or @code{#t} if it was moved.") | |
1bbd0b84 | 476 | #define FUNC_NAME s_scm_primitive_move_to_fdes |
0f2d19dd | 477 | { |
ee149d03 | 478 | struct scm_fport *stream; |
0f2d19dd JB |
479 | int old_fd; |
480 | int new_fd; | |
481 | int rv; | |
482 | ||
78446828 MV |
483 | port = SCM_COERCE_OUTPORT (port); |
484 | ||
3b3b36dd GB |
485 | SCM_VALIDATE_OPFPORT (1,port); |
486 | SCM_VALIDATE_INUM (2,fd); | |
ee149d03 JB |
487 | stream = SCM_FSTREAM (port); |
488 | old_fd = stream->fdes; | |
0f2d19dd JB |
489 | new_fd = SCM_INUM (fd); |
490 | if (old_fd == new_fd) | |
491 | { | |
8b13c6b3 | 492 | return SCM_BOOL_F; |
0f2d19dd JB |
493 | } |
494 | scm_evict_ports (new_fd); | |
495 | rv = dup2 (old_fd, new_fd); | |
496 | if (rv == -1) | |
1bbd0b84 | 497 | SCM_SYSERROR; |
ee149d03 | 498 | stream->fdes = new_fd; |
0f2d19dd | 499 | SCM_SYSCALL (close (old_fd)); |
8b13c6b3 | 500 | return SCM_BOOL_T; |
0f2d19dd | 501 | } |
1bbd0b84 | 502 | #undef FUNC_NAME |
0f2d19dd | 503 | |
0f2d19dd | 504 | /* Return a list of ports using a given file descriptor. */ |
3b3b36dd | 505 | SCM_DEFINE (scm_fdes_to_ports, "fdes->ports", 1, 0, 0, |
1bbd0b84 | 506 | (SCM fd), |
b380b885 MD |
507 | "Returns a list of existing ports which have @var{fdes} as an\n" |
508 | "underlying file descriptor, without changing their revealed counts.") | |
1bbd0b84 | 509 | #define FUNC_NAME s_scm_fdes_to_ports |
0f2d19dd JB |
510 | { |
511 | SCM result = SCM_EOL; | |
512 | int int_fd; | |
513 | int i; | |
514 | ||
3b3b36dd | 515 | SCM_VALIDATE_INUM_COPY (1,fd,int_fd); |
0f2d19dd | 516 | |
0f2d19dd JB |
517 | for (i = 0; i < scm_port_table_size; i++) |
518 | { | |
ee149d03 JB |
519 | if (SCM_OPFPORTP (scm_port_table[i]->port) |
520 | && ((struct scm_fport *) scm_port_table[i]->stream)->fdes == int_fd) | |
0f2d19dd JB |
521 | result = scm_cons (scm_port_table[i]->port, result); |
522 | } | |
0f2d19dd | 523 | return result; |
1bbd0b84 GB |
524 | } |
525 | #undef FUNC_NAME | |
0f2d19dd | 526 | |
1cc91f1b | 527 | |
0f2d19dd JB |
528 | void |
529 | scm_init_ioext () | |
0f2d19dd | 530 | { |
52cfc69b GH |
531 | scm_add_feature ("i/o-extensions"); |
532 | ||
0f2d19dd JB |
533 | #include "ioext.x" |
534 | } | |
535 |