*** empty log message ***
[bpt/guile.git] / libguile / ioext.c
CommitLineData
1146b6cd 1/* Copyright (C) 1995, 1996, 1997 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
15 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
19 *
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
25 *
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
28 *
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
36 *
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
40 */
41\f
42
43
44#include <stdio.h>
0f2d19dd
JB
45#include "fd.h"
46#include "_scm.h"
1146b6cd
GH
47#include "genio.h"
48#include "read.h"
20e6290e 49#include "fports.h"
1146b6cd
GH
50#include "unif.h"
51#include "chars.h"
20e6290e
JB
52
53#include "ioext.h"
0f2d19dd 54
95b88819
GH
55#ifdef HAVE_STRING_H
56#include <string.h>
57#endif
58#ifdef HAVE_UNISTD_H
59#include <unistd.h>
60#endif
0f2d19dd
JB
61\f
62
1146b6cd
GH
63SCM_PROC (s_read_delimited_x, "%read-delimited!", 3, 3, 0, scm_read_delimited_x);
64
65SCM
66scm_read_delimited_x (delims, buf, gobble, port, start, end)
67 SCM delims;
68 SCM buf;
69 SCM gobble;
70 SCM port;
71 SCM start;
72 SCM end;
73{
74 long j;
75 char *cbuf;
76 long cstart;
77 long cend;
78 int c;
79 char *cdelims;
80 int num_delims;
81
82 SCM_ASSERT (SCM_NIMP (delims) && SCM_STRINGP (delims),
83 delims, SCM_ARG1, s_read_delimited_x);
84 cdelims = SCM_CHARS (delims);
85 num_delims = SCM_LENGTH (delims);
86 SCM_ASSERT (SCM_NIMP (buf) && SCM_STRINGP (buf),
87 buf, SCM_ARG2, s_read_delimited_x);
88 cbuf = SCM_CHARS (buf);
89 cend = SCM_LENGTH (buf);
90 if (SCM_UNBNDP (port))
91 port = scm_cur_inp;
92 else
93 {
94 SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port),
95 port, SCM_ARG1, s_read_delimited_x);
96 }
97
98 if (SCM_UNBNDP (start))
99 cstart = 0;
100 else
101 {
102 cstart = scm_num2long (start,
103 (char *) SCM_ARG5, s_read_delimited_x);
104 if (cstart < 0 || cstart >= cend)
105 scm_out_of_range (s_read_delimited_x, start);
106
107 if (!SCM_UNBNDP (end))
108 {
109 long tend = scm_num2long (end, (char *) SCM_ARG6,
110 s_read_delimited_x);
111 if (tend <= cstart || tend > cend)
112 scm_out_of_range (s_read_delimited_x, end);
113 cend = tend;
114 }
115 }
116
117 for (j = cstart; j < cend; j++)
118 {
119 int k;
120
121 c = scm_gen_getc (port);
122 for (k = 0; k < num_delims; k++)
123 {
124 if (cdelims[k] == c)
125 {
126 if (SCM_FALSEP (gobble))
127 scm_gen_ungetc (c, port);
128
129 return scm_cons (SCM_MAKICHR (c),
130 scm_long2num (j - cstart));
131 }
132 }
133 if (c == EOF)
134 return scm_cons (SCM_EOF_VAL,
135 scm_long2num (j - cstart));
136
137 cbuf[j] = c;
138 }
139 return scm_cons (SCM_BOOL_F, scm_long2num (j - cstart));
140}
141
142SCM_PROC (s_write_line, "write-line", 1, 1, 0, scm_write_line);
143
144SCM
145scm_write_line (obj, port)
146 SCM obj;
147 SCM port;
148{
149 scm_display (obj, port);
150 return scm_newline (port);
151}
152
063e05be 153SCM_PROC (s_ftell, "ftell", 1, 0, 0, scm_ftell);
1cc91f1b 154
0f2d19dd 155SCM
063e05be 156scm_ftell (port)
0f2d19dd 157 SCM port;
0f2d19dd
JB
158{
159 long pos;
063e05be 160 SCM_ASSERT (SCM_NIMP (port) && SCM_OPFPORTP (port), port, SCM_ARG1, s_ftell);
0f2d19dd
JB
161 SCM_SYSCALL (pos = ftell ((FILE *)SCM_STREAM (port)));
162 if (pos < 0)
063e05be 163 scm_syserror (s_ftell);
0f2d19dd
JB
164 if (pos > 0 && SCM_CRDYP (port))
165 pos--;
8588fa12 166 return scm_long2num (pos);
0f2d19dd
JB
167}
168
169
170
063e05be 171SCM_PROC (s_fseek, "fseek", 3, 0, 0, scm_fseek);
1cc91f1b 172
0f2d19dd 173SCM
063e05be 174scm_fseek (port, offset, whence)
0f2d19dd
JB
175 SCM port;
176 SCM offset;
177 SCM whence;
0f2d19dd
JB
178{
179 int rv;
8588fa12
GH
180 long loff;
181
063e05be
GH
182 SCM_ASSERT (SCM_NIMP (port) && SCM_OPFPORTP (port), port, SCM_ARG1, s_fseek);
183 loff = scm_num2long (offset, (char *)SCM_ARG2, s_fseek);
0f2d19dd 184 SCM_ASSERT (SCM_INUMP (whence) && (SCM_INUM (whence) < 3) && (SCM_INUM (whence) >= 0),
063e05be 185 whence, SCM_ARG3, s_fseek);
8588fa12 186
0f2d19dd
JB
187 SCM_CLRDY (port); /* Clear ungetted char */
188 /* Values of whence are interned in scm_init_ioext. */
8588fa12 189 rv = fseek ((FILE *)SCM_STREAM (port), loff, SCM_INUM (whence));
02b754d3 190 if (rv != 0)
063e05be 191 scm_syserror (s_fseek);
02b754d3 192 return SCM_UNSPECIFIED;
0f2d19dd
JB
193}
194
195
196
063e05be 197SCM_PROC (s_freopen, "freopen", 3, 0, 0, scm_freopen);
1cc91f1b 198
0f2d19dd 199SCM
063e05be 200scm_freopen (filename, modes, port)
0f2d19dd
JB
201 SCM filename;
202 SCM modes;
203 SCM port;
0f2d19dd
JB
204{
205 FILE *f;
063e05be
GH
206 SCM_ASSERT (SCM_NIMP (filename) && SCM_STRINGP (filename), filename, SCM_ARG1, s_freopen);
207 SCM_ASSERT (SCM_NIMP (modes) && SCM_STRINGP (modes), modes, SCM_ARG2, s_freopen);
0f2d19dd 208 SCM_DEFER_INTS;
063e05be 209 SCM_ASSERT (SCM_NIMP (port) && SCM_FPORTP (port), port, SCM_ARG3, s_freopen);
0f2d19dd
JB
210 SCM_SYSCALL (f = freopen (SCM_CHARS (filename), SCM_CHARS (modes), (FILE *)SCM_STREAM (port)));
211 if (!f)
212 {
213 SCM p;
214 p = port;
215 port = SCM_MAKINUM (errno);
898a256f 216 SCM_SETAND_CAR (p, ~SCM_OPN);
0f2d19dd
JB
217 scm_remove_from_port_table (p);
218 }
219 else
220 {
898a256f 221 SCM_SETCAR (port, scm_tc16_fport | scm_mode_bits (SCM_CHARS (modes)));
0f2d19dd 222 SCM_SETSTREAM (port, (SCM)f);
898a256f
MD
223 SCM_SETCAR (port, scm_tc16_fport | scm_mode_bits (SCM_CHARS (modes)));
224 if (SCM_BUF0 & SCM_CAR (port))
0f2d19dd
JB
225 scm_setbuf0 (port);
226 }
227 SCM_ALLOW_INTS;
228 return port;
229}
230
231
232
063e05be 233SCM_PROC (s_duplicate_port, "duplicate-port", 2, 0, 0, scm_duplicate_port);
1cc91f1b 234
0f2d19dd 235SCM
063e05be 236scm_duplicate_port (oldpt, modes)
0f2d19dd
JB
237 SCM oldpt;
238 SCM modes;
0f2d19dd
JB
239{
240 int oldfd;
241 int newfd;
242 FILE *f;
243 SCM newpt;
063e05be
GH
244 SCM_ASSERT (SCM_NIMP (oldpt) && SCM_OPPORTP (oldpt), oldpt, SCM_ARG1, s_duplicate_port);
245 SCM_ASSERT (SCM_NIMP (modes) && SCM_STRINGP (modes), modes, SCM_ARG2, s_duplicate_port);
0f2d19dd
JB
246 SCM_NEWCELL (newpt);
247 SCM_DEFER_INTS;
248 oldfd = fileno ((FILE *)SCM_STREAM (oldpt));
249 if (oldfd == -1)
063e05be 250 scm_syserror (s_duplicate_port);
0f2d19dd
JB
251 SCM_SYSCALL (newfd = dup (oldfd));
252 if (newfd == -1)
063e05be 253 scm_syserror (s_duplicate_port);
0f2d19dd
JB
254 f = fdopen (newfd, SCM_CHARS (modes));
255 if (!f)
256 {
257 SCM_SYSCALL (close (newfd));
063e05be 258 scm_syserror (s_duplicate_port);
0f2d19dd
JB
259 }
260 {
261 struct scm_port_table * pt;
262 pt = scm_add_to_port_table (newpt);
263 SCM_SETPTAB_ENTRY (newpt, pt);
898a256f
MD
264 SCM_SETCAR (newpt, scm_tc16_fport | scm_mode_bits (SCM_CHARS (modes)));
265 if (SCM_BUF0 & SCM_CAR (newpt))
0f2d19dd
JB
266 scm_setbuf0 (newpt);
267 SCM_SETSTREAM (newpt, (SCM)f);
ebf7394e 268 SCM_PTAB_ENTRY (newpt)->file_name = SCM_PTAB_ENTRY (oldpt)->file_name;
0f2d19dd
JB
269 }
270 SCM_ALLOW_INTS;
271 return newpt;
272}
273
274
275
063e05be 276SCM_PROC (s_redirect_port, "redirect-port", 2, 0, 0, scm_redirect_port);
1cc91f1b 277
0f2d19dd 278SCM
063e05be 279scm_redirect_port (into_pt, from_pt)
0f2d19dd
JB
280 SCM into_pt;
281 SCM from_pt;
0f2d19dd
JB
282{
283 int ans, oldfd, newfd;
284 SCM_DEFER_INTS;
063e05be
GH
285 SCM_ASSERT (SCM_NIMP (into_pt) && SCM_OPPORTP (into_pt), into_pt, SCM_ARG1, s_redirect_port);
286 SCM_ASSERT (SCM_NIMP (from_pt) && SCM_OPPORTP (from_pt), from_pt, SCM_ARG2, s_redirect_port);
0f2d19dd 287 oldfd = fileno ((FILE *)SCM_STREAM (into_pt));
02b754d3 288 if (oldfd == -1)
063e05be 289 scm_syserror (s_redirect_port);
0f2d19dd 290 newfd = fileno ((FILE *)SCM_STREAM (from_pt));
02b754d3 291 if (newfd == -1)
063e05be 292 scm_syserror (s_redirect_port);
02b754d3
GH
293 SCM_SYSCALL (ans = dup2 (oldfd, newfd));
294 if (ans == -1)
063e05be 295 scm_syserror (s_redirect_port);
0f2d19dd 296 SCM_ALLOW_INTS;
02b754d3 297 return SCM_UNSPECIFIED;
0f2d19dd
JB
298}
299
063e05be 300SCM_PROC (s_fileno, "fileno", 1, 0, 0, scm_fileno);
1cc91f1b 301
0f2d19dd 302SCM
063e05be 303scm_fileno (port)
0f2d19dd 304 SCM port;
0f2d19dd
JB
305{
306 int fd;
063e05be 307 SCM_ASSERT (SCM_NIMP (port) && SCM_OPFPORTP (port), port, SCM_ARG1, s_fileno);
0f2d19dd 308 fd = fileno ((FILE *)SCM_STREAM (port));
02b754d3 309 if (fd == -1)
063e05be 310 scm_syserror (s_fileno);
02b754d3 311 return SCM_MAKINUM (fd);
0f2d19dd
JB
312}
313
063e05be 314SCM_PROC (s_isatty, "isatty?", 1, 0, 0, scm_isatty_p);
1cc91f1b 315
0f2d19dd 316SCM
063e05be 317scm_isatty_p (port)
0f2d19dd 318 SCM port;
0f2d19dd
JB
319{
320 int rv;
063e05be 321 SCM_ASSERT (SCM_NIMP (port) && SCM_OPFPORTP (port), port, SCM_ARG1, s_isatty);
0f2d19dd
JB
322 rv = fileno ((FILE *)SCM_STREAM (port));
323 if (rv == -1)
063e05be 324 scm_syserror (s_isatty);
02b754d3
GH
325 rv = isatty (rv);
326 return rv ? SCM_BOOL_T : SCM_BOOL_F;
0f2d19dd
JB
327}
328
329
330
063e05be 331SCM_PROC (s_fdopen, "fdopen", 2, 0, 0, scm_fdopen);
1cc91f1b 332
0f2d19dd 333SCM
063e05be 334scm_fdopen (fdes, modes)
0f2d19dd
JB
335 SCM fdes;
336 SCM modes;
0f2d19dd
JB
337{
338 FILE *f;
339 SCM port;
8b13c6b3 340 struct scm_port_table * pt;
0f2d19dd 341
063e05be
GH
342 SCM_ASSERT (SCM_INUMP (fdes), fdes, SCM_ARG1, s_fdopen);
343 SCM_ASSERT (SCM_NIMP (modes) && SCM_STRINGP (modes), modes, SCM_ARG2, s_fdopen);
8b13c6b3 344 SCM_NEWCELL (port);
0f2d19dd
JB
345 SCM_DEFER_INTS;
346 f = fdopen (SCM_INUM (fdes), SCM_CHARS (modes));
347 if (f == NULL)
063e05be 348 scm_syserror (s_fdopen);
8b13c6b3
GH
349 pt = scm_add_to_port_table (port);
350 SCM_SETPTAB_ENTRY (port, pt);
898a256f
MD
351 SCM_SETCAR (port, scm_tc16_fport | scm_mode_bits (SCM_CHARS (modes)));
352 if (SCM_BUF0 & SCM_CAR (port))
8b13c6b3
GH
353 scm_setbuf0 (port);
354 SCM_SETSTREAM (port, (SCM)f);
0f2d19dd
JB
355 SCM_ALLOW_INTS;
356 return port;
357}
358
359
360
361/* Move a port's underlying file descriptor to a given value.
8b13c6b3
GH
362 * Returns #f if fdes is already the given value.
363 * #t if fdes moved.
0f2d19dd
JB
364 * MOVE->FDES is implemented in Scheme and calls this primitive.
365 */
063e05be 366SCM_PROC (s_primitive_move_to_fdes, "primitive-move->fdes", 2, 0, 0, scm_primitive_move_to_fdes);
1cc91f1b 367
0f2d19dd 368SCM
063e05be 369scm_primitive_move_to_fdes (port, fd)
0f2d19dd
JB
370 SCM port;
371 SCM fd;
0f2d19dd
JB
372{
373 FILE *stream;
374 int old_fd;
375 int new_fd;
376 int rv;
377
063e05be
GH
378 SCM_ASSERT (SCM_NIMP (port) && SCM_OPFPORTP (port), port, SCM_ARG1, s_primitive_move_to_fdes);
379 SCM_ASSERT (SCM_INUMP (fd), fd, SCM_ARG2, s_primitive_move_to_fdes);
0f2d19dd
JB
380 SCM_DEFER_INTS;
381 stream = (FILE *)SCM_STREAM (port);
382 old_fd = fileno (stream);
383 new_fd = SCM_INUM (fd);
384 if (old_fd == new_fd)
385 {
386 SCM_ALLOW_INTS;
8b13c6b3 387 return SCM_BOOL_F;
0f2d19dd
JB
388 }
389 scm_evict_ports (new_fd);
390 rv = dup2 (old_fd, new_fd);
391 if (rv == -1)
063e05be 392 scm_syserror (s_primitive_move_to_fdes);
0f2d19dd
JB
393 scm_setfileno (stream, new_fd);
394 SCM_SYSCALL (close (old_fd));
395 SCM_ALLOW_INTS;
8b13c6b3 396 return SCM_BOOL_T;
0f2d19dd
JB
397}
398
1cc91f1b 399
0f2d19dd
JB
400void
401scm_setfileno (fs, fd)
402 FILE *fs;
403 int fd;
0f2d19dd
JB
404{
405#ifdef SET_FILE_FD_FIELD
406 SET_FILE_FD_FIELD(fs, fd);
407#else
408 Configure could not guess the name of the correct field in a FILE *.
409
410 This function needs to be ported to your system.
411
412 SET_FILE_FD_FIELD should change the descriptor refered to by a stdio
413 stream, and nothing else.
414
415 The way to port this file is to add cases to configure.in. Search
416 that file for "SET_FILE_FD_FIELD" and follow the examples there.
417#endif
418}
419
420/* Move ports with the specified file descriptor to new descriptors,
421 * reseting the revealed count to 0.
422 * Should be called with SCM_DEFER_INTS active.
423 */
1cc91f1b 424
0f2d19dd
JB
425void
426scm_evict_ports (fd)
427 int fd;
0f2d19dd
JB
428{
429 int i;
430
431 for (i = 0; i < scm_port_table_size; i++)
432 {
433 if (SCM_FPORTP (scm_port_table[i]->port)
434 && fileno ((FILE *)SCM_STREAM (scm_port_table[i]->port)) == fd)
435 {
436 scm_setfileno ((FILE *)SCM_STREAM (scm_port_table[i]->port), dup (fd));
437 scm_set_port_revealed_x (scm_port_table[i]->port, SCM_MAKINUM (0));
438 }
439 }
440}
441
442/* Return a list of ports using a given file descriptor. */
443SCM_PROC(s_fdes_to_ports, "fdes->ports", 1, 0, 0, scm_fdes_to_ports);
1cc91f1b 444
0f2d19dd
JB
445SCM
446scm_fdes_to_ports (fd)
447 SCM fd;
0f2d19dd
JB
448{
449 SCM result = SCM_EOL;
450 int int_fd;
451 int i;
452
453 SCM_ASSERT (SCM_INUMP (fd), fd, SCM_ARG1, s_fdes_to_ports);
454 int_fd = SCM_INUM (fd);
455
456 SCM_DEFER_INTS;
457 for (i = 0; i < scm_port_table_size; i++)
458 {
459 if (SCM_FPORTP (scm_port_table[i]->port)
460 && fileno ((FILE *)SCM_STREAM (scm_port_table[i]->port)) == int_fd)
461 result = scm_cons (scm_port_table[i]->port, result);
462 }
463 SCM_ALLOW_INTS;
464 return result;
465}
466
1cc91f1b 467
0f2d19dd
JB
468void
469scm_init_ioext ()
0f2d19dd
JB
470{
471 /* fseek() symbols. */
472 scm_sysintern ("SEEK_SET", SCM_MAKINUM (SEEK_SET));
473 scm_sysintern ("SEEK_CUR", SCM_MAKINUM (SEEK_CUR));
474 scm_sysintern ("SEEK_END", SCM_MAKINUM (SEEK_END));
475
0f2d19dd
JB
476#include "ioext.x"
477}
478