*** empty log message ***
[bpt/guile.git] / libguile / ioext.c
CommitLineData
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. */
0f2d19dd
JB
41\f
42
43
44#include <stdio.h>
0f2d19dd 45#include "_scm.h"
ee149d03 46#include "ports.h"
1146b6cd 47#include "read.h"
20e6290e 48#include "fports.h"
1146b6cd
GH
49#include "unif.h"
50#include "chars.h"
20e6290e
JB
51
52#include "ioext.h"
0f2d19dd 53
ee149d03
JB
54#include <fcntl.h>
55
95b88819
GH
56#ifdef HAVE_STRING_H
57#include <string.h>
58#endif
59#ifdef HAVE_UNISTD_H
60#include <unistd.h>
61#endif
0f2d19dd
JB
62\f
63
1146b6cd
GH
64SCM_PROC (s_read_delimited_x, "%read-delimited!", 3, 3, 0, scm_read_delimited_x);
65
66SCM
67scm_read_delimited_x (delims, buf, gobble, port, start, end)
68 SCM delims;
69 SCM buf;
70 SCM gobble;
71 SCM port;
72 SCM start;
73 SCM end;
74{
75 long j;
76 char *cbuf;
77 long cstart;
78 long cend;
79 int c;
80 char *cdelims;
81 int num_delims;
82
ae2fa5bc 83 SCM_ASSERT (SCM_NIMP (delims) && SCM_ROSTRINGP (delims),
1146b6cd 84 delims, SCM_ARG1, s_read_delimited_x);
ae2fa5bc
GH
85 cdelims = SCM_ROCHARS (delims);
86 num_delims = SCM_ROLENGTH (delims);
1146b6cd
GH
87 SCM_ASSERT (SCM_NIMP (buf) && SCM_STRINGP (buf),
88 buf, SCM_ARG2, s_read_delimited_x);
89 cbuf = SCM_CHARS (buf);
90 cend = SCM_LENGTH (buf);
91 if (SCM_UNBNDP (port))
92 port = scm_cur_inp;
93 else
94 {
95 SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port),
96 port, SCM_ARG1, s_read_delimited_x);
97 }
98
99 if (SCM_UNBNDP (start))
100 cstart = 0;
101 else
102 {
103 cstart = scm_num2long (start,
104 (char *) SCM_ARG5, s_read_delimited_x);
105 if (cstart < 0 || cstart >= cend)
106 scm_out_of_range (s_read_delimited_x, start);
107
108 if (!SCM_UNBNDP (end))
109 {
110 long tend = scm_num2long (end, (char *) SCM_ARG6,
111 s_read_delimited_x);
112 if (tend <= cstart || tend > cend)
113 scm_out_of_range (s_read_delimited_x, end);
114 cend = tend;
115 }
116 }
117
118 for (j = cstart; j < cend; j++)
119 {
120 int k;
121
b7f3516f 122 c = scm_getc (port);
1146b6cd
GH
123 for (k = 0; k < num_delims; k++)
124 {
125 if (cdelims[k] == c)
126 {
127 if (SCM_FALSEP (gobble))
b7f3516f 128 scm_ungetc (c, port);
1146b6cd
GH
129
130 return scm_cons (SCM_MAKICHR (c),
131 scm_long2num (j - cstart));
132 }
133 }
134 if (c == EOF)
135 return scm_cons (SCM_EOF_VAL,
136 scm_long2num (j - cstart));
137
138 cbuf[j] = c;
139 }
140 return scm_cons (SCM_BOOL_F, scm_long2num (j - cstart));
141}
142
ee149d03
JB
143static unsigned char *
144scm_do_read_line (SCM port, int *len_p)
145{
afc5764c 146 scm_port *pt = SCM_PTAB_ENTRY (port);
ee149d03
JB
147 unsigned char *end;
148
149 /* I thought reading lines was simple. Mercy me. */
150
6c951427
GH
151 /* The common case: the buffer contains a complete line.
152 This needs to be fast. */
ee149d03
JB
153 if ((end = memchr (pt->read_pos, '\n', (pt->read_end - pt->read_pos)))
154 != 0)
155 {
156 int buf_len = (end + 1) - pt->read_pos;
157 /* Allocate a buffer of the perfect size. */
158 unsigned char *buf = malloc (buf_len + 1);
159
160 memcpy (buf, pt->read_pos, buf_len);
161 pt->read_pos += buf_len;
162
163 buf[buf_len] = '\0';
164
165 *len_p = buf_len;
166 return buf;
167 }
168
6c951427 169 /* The buffer contains no newlines. */
ee149d03
JB
170 {
171 /* When live, len is always the number of characters in the
172 current buffer that are part of the current line. */
173 int len = (pt->read_end - pt->read_pos);
174 int buf_size = (len < 50) ? 60 : len * 2;
175 /* Invariant: buf always has buf_size + 1 characters allocated;
176 the `+ 1' is for the final '\0'. */
177 unsigned char *buf = malloc (buf_size + 1);
178 int buf_len = 0;
ee149d03
JB
179
180 for (;;)
181 {
182 if (buf_len + len > buf_size)
183 {
184 int new_size = (buf_len + len) * 2;
185 buf = realloc (buf, new_size + 1);
186 buf_size = new_size;
187 }
188
189 /* Copy what we've got out of the port, into our buffer. */
190 memcpy (buf + buf_len, pt->read_pos, len);
191 buf_len += len;
192 pt->read_pos += len;
193
194 /* If we had seen a newline, we're done now. */
195 if (end)
196 break;
197
5c070ca7
GH
198 /* Get more characters. */
199 if (scm_fill_buffer (port) == EOF)
ee149d03
JB
200 {
201 /* If we're missing a final newline in the file, return
202 what we did get, sans newline. */
203 if (buf_len > 0)
204 break;
205
206 free (buf);
207 return 0;
208 }
209
ee149d03
JB
210 /* Search the buffer for newlines. */
211 if ((end = memchr (pt->read_pos, '\n',
212 (len = (pt->read_end - pt->read_pos))))
213 != 0)
214 len = (end - pt->read_pos) + 1;
215 }
216
217 /* I wonder how expensive this realloc is. */
218 buf = realloc (buf, buf_len + 1);
219 buf[buf_len] = '\0';
220 *len_p = buf_len;
221 return buf;
222 }
223}
224
225
3e2043c4 226/*
ee149d03 227 * %read-line
848f2a01
TP
228 * truncates any terminating newline from its input, and returns
229 * a cons of the string read and its terminating character. Doing
230 * so makes it easy to implement the hairy `read-line' options
231 * efficiently in Scheme.
3e2043c4
TP
232 */
233
3cb988bd
TP
234SCM_PROC (s_read_line, "%read-line", 0, 1, 0, scm_read_line);
235
236SCM
237scm_read_line (port)
238 SCM port;
239{
afc5764c 240 scm_port *pt;
3cb988bd 241 char *s;
848f2a01
TP
242 int slen;
243 SCM line, term;
3cb988bd
TP
244
245 if (SCM_UNBNDP (port))
246 port = scm_cur_inp;
247 else
248 {
249 SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port),
250 port, SCM_ARG1, s_read_line);
251 }
252
afc5764c
JB
253 pt = SCM_PTAB_ENTRY (port);
254 if (pt->rw_active == SCM_PORT_WRITE)
255 scm_ptobs[SCM_PTOBNUM (port)].fflush (port);
256
848f2a01
TP
257 s = scm_do_read_line (port, &slen);
258
3e2043c4 259 if (s == NULL)
848f2a01 260 term = line = SCM_EOF_VAL;
3e2043c4
TP
261 else
262 {
848f2a01
TP
263 if (s[slen-1] == '\n')
264 {
265 term = SCM_MAKICHR ('\n');
ee149d03
JB
266 s[slen-1] = '\0';
267 line = scm_take_str (s, slen-1);
268 SCM_INCLINE (port);
848f2a01
TP
269 }
270 else
271 {
272 /* Fix: we should check for eof on the port before assuming this. */
273 term = SCM_EOF_VAL;
ee149d03 274 line = scm_take_str (s, slen);
afc5764c 275 SCM_COL (port) += slen;
848f2a01 276 }
3e2043c4 277 }
848f2a01 278
afc5764c
JB
279 if (pt->rw_random)
280 pt->rw_active = SCM_PORT_READ;
281
848f2a01 282 return scm_cons (line, term);
3cb988bd
TP
283}
284
1146b6cd
GH
285SCM_PROC (s_write_line, "write-line", 1, 1, 0, scm_write_line);
286
287SCM
288scm_write_line (obj, port)
289 SCM obj;
290 SCM port;
291{
292 scm_display (obj, port);
293 return scm_newline (port);
294}
295
063e05be 296SCM_PROC (s_ftell, "ftell", 1, 0, 0, scm_ftell);
1cc91f1b 297
0f2d19dd 298SCM
6afcd3b2
GH
299scm_ftell (object)
300 SCM object;
0f2d19dd 301{
afc5764c 302 return scm_lseek (object, SCM_INUM0, SCM_MAKINUM (SEEK_CUR));
ee149d03 303}
0f2d19dd 304
063e05be 305SCM_PROC (s_fseek, "fseek", 3, 0, 0, scm_fseek);
1cc91f1b 306
0f2d19dd 307SCM
6afcd3b2
GH
308scm_fseek (object, offset, whence)
309 SCM object;
0f2d19dd
JB
310 SCM offset;
311 SCM whence;
0f2d19dd 312{
afc5764c 313 scm_lseek (object, offset, whence);
78446828 314
02b754d3 315 return SCM_UNSPECIFIED;
0f2d19dd
JB
316}
317
063e05be 318SCM_PROC (s_redirect_port, "redirect-port", 2, 0, 0, scm_redirect_port);
1cc91f1b 319
0f2d19dd 320SCM
9c29ac66
GH
321scm_redirect_port (old, new)
322 SCM old;
323 SCM new;
0f2d19dd
JB
324{
325 int ans, oldfd, newfd;
ee149d03 326 struct scm_fport *fp;
9c29ac66 327
78446828
MV
328 old = SCM_COERCE_OUTPORT (old);
329 new = SCM_COERCE_OUTPORT (new);
330
ee149d03
JB
331 SCM_ASSERT (SCM_NIMP (old) && SCM_OPFPORTP (old), old, SCM_ARG1, s_redirect_port);
332 SCM_ASSERT (SCM_NIMP (new) && SCM_OPFPORTP (new), new, SCM_ARG2, s_redirect_port);
333 oldfd = SCM_FPORT_FDES (old);
334 fp = SCM_FSTREAM (new);
335 newfd = fp->fdes;
336 if (oldfd != newfd)
337 {
afc5764c
JB
338 scm_port *pt = SCM_PTAB_ENTRY (new);
339 scm_port *old_pt = SCM_PTAB_ENTRY (old);
16019956 340 scm_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (new)];
afc5764c
JB
341
342 /* must flush to old fdes. */
343 if (pt->rw_active == SCM_PORT_WRITE)
344 ptob->fflush (new);
345 else if (pt->rw_active == SCM_PORT_READ)
283a1a0e 346 scm_read_flush (new);
ee149d03
JB
347 ans = dup2 (oldfd, newfd);
348 if (ans == -1)
349 scm_syserror (s_redirect_port);
afc5764c 350 pt->rw_random = old_pt->rw_random;
ee149d03 351 /* continue using existing buffers, even if inappropriate. */
ee149d03 352 }
02b754d3 353 return SCM_UNSPECIFIED;
0f2d19dd
JB
354}
355
7a6f1ffa 356SCM_PROC (s_dup_to_fdes, "dup->fdes", 1, 1, 0, scm_dup_to_fdes);
a9488d12 357SCM
7a6f1ffa 358scm_dup_to_fdes (SCM fd_or_port, SCM fd)
a9488d12
GH
359{
360 int oldfd, newfd, rv;
361
78446828
MV
362 fd_or_port = SCM_COERCE_OUTPORT (fd_or_port);
363
a9488d12
GH
364 if (SCM_INUMP (fd_or_port))
365 oldfd = SCM_INUM (fd_or_port);
366 else
367 {
ee149d03 368 SCM_ASSERT (SCM_NIMP (fd_or_port) && SCM_OPFPORTP (fd_or_port),
7a6f1ffa 369 fd_or_port, SCM_ARG1, s_dup_to_fdes);
ee149d03 370 oldfd = SCM_FPORT_FDES (fd_or_port);
a9488d12 371 }
7a6f1ffa
GH
372
373 if (SCM_UNBNDP (fd))
e38303a2 374 {
ee149d03 375 newfd = dup (oldfd);
7a6f1ffa 376 if (newfd == -1)
468b2cf3 377 scm_syserror (s_dup_to_fdes);
7a6f1ffa
GH
378 fd = SCM_MAKINUM (newfd);
379 }
380 else
381 {
382 SCM_ASSERT (SCM_INUMP (fd), fd, SCM_ARG2, s_dup_to_fdes);
383 newfd = SCM_INUM (fd);
384 if (oldfd != newfd)
385 {
386 scm_evict_ports (newfd); /* see scsh manual. */
ee149d03 387 rv = dup2 (oldfd, newfd);
7a6f1ffa
GH
388 if (rv == -1)
389 scm_syserror (s_dup_to_fdes);
390 }
e38303a2 391 }
e38303a2 392 return fd;
a9488d12
GH
393}
394
063e05be 395SCM_PROC (s_fileno, "fileno", 1, 0, 0, scm_fileno);
1cc91f1b 396
0f2d19dd 397SCM
063e05be 398scm_fileno (port)
0f2d19dd 399 SCM port;
0f2d19dd 400{
78446828 401 port = SCM_COERCE_OUTPORT (port);
ee149d03
JB
402 SCM_ASSERT (SCM_NIMP (port) && SCM_OPFPORTP (port), port, SCM_ARG1,
403 s_fileno);
404 return SCM_MAKINUM (SCM_FPORT_FDES (port));
0f2d19dd
JB
405}
406
063e05be 407SCM_PROC (s_isatty, "isatty?", 1, 0, 0, scm_isatty_p);
1cc91f1b 408
0f2d19dd 409SCM
063e05be 410scm_isatty_p (port)
0f2d19dd 411 SCM port;
0f2d19dd
JB
412{
413 int rv;
78446828
MV
414
415 port = SCM_COERCE_OUTPORT (port);
416
10ccfad7
MD
417 if (!(SCM_NIMP (port) && SCM_OPFPORTP (port)))
418 return SCM_BOOL_F;
ee149d03
JB
419
420 rv = isatty (SCM_FPORT_FDES (port));
02b754d3 421 return rv ? SCM_BOOL_T : SCM_BOOL_F;
0f2d19dd
JB
422}
423
424
425
063e05be 426SCM_PROC (s_fdopen, "fdopen", 2, 0, 0, scm_fdopen);
1cc91f1b 427
0f2d19dd 428SCM
063e05be 429scm_fdopen (fdes, modes)
0f2d19dd
JB
430 SCM fdes;
431 SCM modes;
0f2d19dd 432{
0f2d19dd
JB
433 SCM port;
434
063e05be 435 SCM_ASSERT (SCM_INUMP (fdes), fdes, SCM_ARG1, s_fdopen);
ae2fa5bc
GH
436 SCM_ASSERT (SCM_NIMP (modes) && SCM_ROSTRINGP (modes), modes, SCM_ARG2,
437 s_fdopen);
89958ad0 438 SCM_COERCE_SUBSTR (modes);
ee149d03 439 port = scm_fdes_to_port (SCM_INUM (fdes), SCM_ROCHARS (modes), SCM_BOOL_F);
0f2d19dd
JB
440 return port;
441}
442
443
444
445/* Move a port's underlying file descriptor to a given value.
8b13c6b3
GH
446 * Returns #f if fdes is already the given value.
447 * #t if fdes moved.
0f2d19dd
JB
448 * MOVE->FDES is implemented in Scheme and calls this primitive.
449 */
063e05be 450SCM_PROC (s_primitive_move_to_fdes, "primitive-move->fdes", 2, 0, 0, scm_primitive_move_to_fdes);
1cc91f1b 451
0f2d19dd 452SCM
063e05be 453scm_primitive_move_to_fdes (port, fd)
0f2d19dd
JB
454 SCM port;
455 SCM fd;
0f2d19dd 456{
ee149d03 457 struct scm_fport *stream;
0f2d19dd
JB
458 int old_fd;
459 int new_fd;
460 int rv;
461
78446828
MV
462 port = SCM_COERCE_OUTPORT (port);
463
063e05be
GH
464 SCM_ASSERT (SCM_NIMP (port) && SCM_OPFPORTP (port), port, SCM_ARG1, s_primitive_move_to_fdes);
465 SCM_ASSERT (SCM_INUMP (fd), fd, SCM_ARG2, s_primitive_move_to_fdes);
ee149d03
JB
466 stream = SCM_FSTREAM (port);
467 old_fd = stream->fdes;
0f2d19dd
JB
468 new_fd = SCM_INUM (fd);
469 if (old_fd == new_fd)
470 {
8b13c6b3 471 return SCM_BOOL_F;
0f2d19dd
JB
472 }
473 scm_evict_ports (new_fd);
474 rv = dup2 (old_fd, new_fd);
475 if (rv == -1)
063e05be 476 scm_syserror (s_primitive_move_to_fdes);
ee149d03 477 stream->fdes = new_fd;
0f2d19dd 478 SCM_SYSCALL (close (old_fd));
8b13c6b3 479 return SCM_BOOL_T;
0f2d19dd
JB
480}
481
0f2d19dd
JB
482/* Return a list of ports using a given file descriptor. */
483SCM_PROC(s_fdes_to_ports, "fdes->ports", 1, 0, 0, scm_fdes_to_ports);
1cc91f1b 484
0f2d19dd
JB
485SCM
486scm_fdes_to_ports (fd)
487 SCM fd;
0f2d19dd
JB
488{
489 SCM result = SCM_EOL;
490 int int_fd;
491 int i;
492
493 SCM_ASSERT (SCM_INUMP (fd), fd, SCM_ARG1, s_fdes_to_ports);
494 int_fd = SCM_INUM (fd);
495
0f2d19dd
JB
496 for (i = 0; i < scm_port_table_size; i++)
497 {
ee149d03
JB
498 if (SCM_OPFPORTP (scm_port_table[i]->port)
499 && ((struct scm_fport *) scm_port_table[i]->stream)->fdes == int_fd)
0f2d19dd
JB
500 result = scm_cons (scm_port_table[i]->port, result);
501 }
0f2d19dd
JB
502 return result;
503}
504
1cc91f1b 505
0f2d19dd
JB
506void
507scm_init_ioext ()
0f2d19dd 508{
0f2d19dd
JB
509#include "ioext.x"
510}
511