mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-05-06 15:40:29 +02:00
newline. A bit faster, and definitely hairier. (scm_read_line): Count newlines here instead. * strings.c (scm_take_str): New function. (scm_take0str): Reimplement in terms of scm_take_str. * strings.h (scm_take_str): New declaration. * ioext.c (scm_read_line): Use scm_take_str, to avoid copying the string. Add some simple-minded support for line buffered ports. * ports.h (SCM_BUFLINE): New flag for ports. * init.c (scm_init_standard_ports): Request line-buffering on the standard output port. * * ports.c (scm_mode_bits): Recognize 'l' as a request for line buffering. (scm_putc, scm_puts, scm_lfwrite): If the port is line-buffered, and there's a newline to be written, flush the port. * ports.c: (scm_lseek): clear buffers even if just reading current position. * fports.c (local_fclose): call local_fflush unconditionally. (various): don't use the scm_must... memory procs. * ports.h (scm_port): make read_pos a pointer to const. strports.c: take care of rw_active and rw_randow. fports.c: scm_fport_drain_input: removed. do it all in ports.c. strports.c (scm_mkstrport): check that pos is reasonable. ioext.c (scm_ftell, scm_fseek): use lseek. (SCM_CLEAR_BUFFERS): macro deleted. ioext.c (redirect_port: use ptob fflush, read_flush. ports.h (scm_ptobfuns): add ftruncate. ports.c (scm_newptob): set ftruncate. adjust ptob tables. * ports.c (scm_ftruncate): new procedure. fports.c (local_ftrunate), strports.c (str_ftruncate): new procs. strports.c (st_seek, st_grow_port): new procs. fports.h (scm_port): change size types from int to off_t. ports.c (scm_init_ports): initialise the seek symbols here instead of in ioext.c. strports.c (scm_call_with_output_string): start with an empty string, so seek and ftruncate can be used. * ports.h (scm_ptobfuns): add a read_flush procedure which is the equivalent to fflush for the read buffer. * ports.c (scm_newptob): set read_flush. ports.c (void_port_ptob): set read_flush. fports.c (local_read_flush): new proc. add to ptob. strport.c (st_read_flush): likewise. vport.c (sf_read_flush): likewise. fports.h (struct scm_fport): remove random member. there's nothing left but fdes. leaving it as a struct to allow for future changes. fports.c: replace usage of scm_fport::random with scm_port::rw_random. ports.c: (scm_putc, scm_puts, scm_lfwrite): call the read_flush ptob proc if the read buffer is filled. * ports.h (scm_port): add a rw_random member and replace reading and writing members with rw_active member. SCM_PORT_READ/SCM_PORT_WRITE: new values. * ports.h (struct scm_port_table): add writing and reading members to replace write_needs_seek: it isn't good enough for non-fports. ports.c, ioext.c, fports.c: corresponding changes. (struct scm_port_table): give it a typedef and rename to scm_port. ports.c, fports.c, strports.c, vports.c, ioext.c, ports.h: corresponding changes. * ports.c (scm_newptob): bugfix: set seek member. * * (scm_lseek): new procedure, using code from ioext.c:scm_fseek and generalised to all port types. * scmsigs.c (scm_init_scmsigs): set the SA_RESTART flag for all signals (it was only being done for handlers installed from Scheme). Otherwise (for example) SIGSTOP followed by SIGCONT on an interpreter waiting for input caused an EINTR error from read. * ports.h (struct scm_port_table): make all the char members unsigned, so they convert to int without becoming negative if large. * fports.c (scm_fdes_wait_for_input): forgot to check compilation with threads enabled. rename this procedure to fport_wait_for_input and take a port instead of a fdes. use scm_fport_input_waiting_p instead of scm_fdes_waiting_p. * readline.c (scm_readline): Applied a patch from Greg Harvey to get readline support working again: use fdopen to get FILE objects. * gc.c (scm_init_storage): install an atexit proc to flush the ports. (cleanup): the new proc. it sets a global variable which can be checked by the ptob flush procs to avoid trying to throw exceptions during exit. not very pleasant but it seems more reliable. * fports.c (local_fflush): check terminating variable and if set don't throw exception. * CHECKME: that the atexit proc is installed if unexec used. * throw.c (scm_handle_by_message): don't flush all ports here. it still causes bus errors. * fports.h (SCM_FPORT_CLEAR_BUFFERS): rename to SCM_CLEAR_BUFFERS and move to ioext.c. * fports.c (scm_fdes_waiting_p): merged into fport_input_waiting_p. * ports.c (scm_char_ready_p): check the port buffer and call the ptob entry if needed. * ports.h (scm_ptobfuns): input_waiting_p added. change all the ptob initialisers. use it in char-ready * ioext.c (scm_do_read_line): moved from ports.c. make it static. * vports.c (sfflush): modified to write a char (since softports currently use shortbuf.) * fports.c (scm_standard_stream_to_port): moved to init.c and made static. * init.c (scm_init_standard_ports): make stdout and stderr unbuffered if connected to a terminal. with stdio they were line-buffered by default. * ports.h (scm_ptobfuns): change fflush return to void. change flush proc definitions. * strports.c (scm_call_with_output_string): get size from buffer instead of port stream. (scm_strprint_obj): likewise. (st_flush): new proc. * ports.h (struct scm_port_table): added write_end member, as an optimisation. set it where write_buf_size is set. * ports.h (struct scm_port_table): change stream from void * back to SCM. SCM presumably must be large enough to hold a pointer (and probably vice versa but who knows.) (SCM_SSTREAM): deleted. change users back to SCM_STREAM. (scm_puts): rewritten * fports.c (local_ffwrite, local_fputs): removed. * strports.c (stputc, stputs, stwrite): dyked out (FIXME) * vports.c (sfputc, sfputs, sfwrite) likewise. * ports.c (write_void_port, puts_void_port): removed. (putc_void_port, getc_void_port, fgets_void_port): likewise. * ports.c (scm_lfwrite): rewritten using fport.c version. * fports.c (local_fputc): deleted. * ports.c (scm_add_to_port_table): initialise write_needs_seek. * ports.h (scm_ptobfuns): add seek function pointer. * fports.c: set it to local_seek, new procedure. * fports.h (SCM_MAYBE_DRAIN_INPUT): moved to ports.c. use ptob for seek. take ptob instead of fport arg. * ports.h (struct scm_port_table): new member write_needs_seek, replaces reading member in fport struct. * vports.c (sfgetc): store the getted char into the buffer. rename to sf_fill_buffer and install it for fill-buffer in ptob. the Scheme interface is still a procedure that gets a char. (scm_make_soft_port): set up the port buffer (shortbuf). * fports.c (local_fgetc, local_fgets): deleted. * strports.c (stgetc): likewise. * ports.c: scm_generic_fgets: likewise. * ports.h (scm_ptobfuns): add fill_buffer. * ports.c (scm_newptob): assign it. * strports.c (scm_mkstrport): set up the buffer. put just the string into the stream, not cons (pos stream). (stfill_buffer): new proc. * ports.h: fport buffer moved into port table: to be used for all port types. * throw.c (scm_handle_by_message): flush ports at exit. * socket.c (scm_sock_fd_to_port): use scm_fdes_to_port. (scm_getsockopt, scm_setsockopt, scm_shutdown, scm_connect, scm_bind, scm_listen, scm_accept, scm_getsockname, scm_getpeername, scm_recv, scm_send, scm_recvfrom, scm_sendto, use SCM_FPORT_FDES. use SCM_OPFPORTP not SCM_FPORTP. * posix.c (scm_getgroups): use SCM_ALLOW/DEFER_INTS. (scm_ttyname): use SCM_FPORT_FDES. (scm_tcgetpgrp, scm_tcsetpgrp): likewise. * ioext.c (scm_isatty_p): use SCM_FPORT_FDES. (scm_fdes_to_ports): modified. (scm_fdopen): use scm_fdes_to_port. * ports.c (scm_init_ports): don't try to flush ports using atexit(). it's too late, errors will cause SEGV. * fports.c (scm_fport_buffer_add): new procedure. * fports.h (SCM_FDES_RANDOM_P): new macro. use it in scm_fdes_to_port and scm_redirect_port. * ioext.c (scm_redirect_port): use setvbuf to set buffers in the new port. reset fp->random. * fports.c (scm_fdes_to_port), ports.c (scm_void_port), filesys.c (scm_opendir): restore defer interrupts while the port is constructed. * (scm_setvbuf): if mode is _IOFBF and size is not supplied, derive buffer size from fdes or use a default. (scm_fdes_to_port): use setvbuf instead of creating the buffers directly. vports.c (various places): use SCM_SSTREAM. strports.c: likewise. * gdbint.c: likewise. * ports.h (SCM_SSTREAM): new macro. * fports.c (scm_input_waiting_p): use scm_return_first, since port may be removed from the stack by the tail call to scm_fdes_waiting_p. * fports.h (SCM_CLEAR_BUFFERS): new macro. * ports.c (scm_force_output): call scm_fflush. * print.c (scm_newline): don't check errno for EPIPE (it wouldn't * reach this point.) don't flush port (if scm_cur_outp). * fports.h (SCM_FPORT_FDES): new macro. * vports.c (sfflush): don't need to set errno. * ports.c: install scm_flush_all_ports to be run on exit. ports.c fports.c ioext.c posix.c socket.c net_db.c filesys.c: removed all uses of SCM_DEFER/ALLOW ints for now. they were mainly just protecting errno. some may need to be put back. * scmsigs.c (take_signal): save and restore errno while this proc runs. *fports.c (print_pipe_port, local_pclose, scm_pipob): deleted. * open-pipe, close-pipe are emulated in (ice-9 popen) ports.c (scm_ports_prehistory): don't init scm_pipob. ports.h (scm_tc16_pipe): deleted. posix.c (scm_open_pipe, scm_close_pipe): deleted. * ioext.c (scm_primitive_move_to_fdes): use fport. * fport.c (scm_fport_fill_buffer): flush write buffer if needed. change arg type from scm_fport to SCM port. fport.h (SCM_SETFDES): removed. (SCM_MAYBE_DRAIN_INPUT): new macro. * ioext.c (scm_dup_to_fdes): use SCM_FSTREAM. (scm_ftell): always use lseek and account for the buffer. (scm_fileno): use fport buffer. (scm_fseek): clear fport buffers. always use lseek. * posix.c (scm_pipe): use fport buffer. * unif.c: include fports.h instead of genio.h. * fports.c (scm_fdes_wait_for_input, scm_fport_fill_buffer): new procedures. (local_fgetc): use them. (local_ffwrite): use buffer. (local_fgets): use buffer. (scm_setbuf0): deleted. (scm_setvbuf): set the buffer. (scm_setfileno): deleted. (scm_evict_ports): set fdes directly. * (scm_freopen): deleted. doesn't seem useful in Guile. (scm_stdio_to_port): deleted. fports.h (struct scm_fport): add shortbuf member to avoid separate code for unbuffered ports. (SCM_FPORTP, SCM_OPFPORTP, SCM_OPINFPORTP, SCM_OPOUTFPORTP): moved from ports.h. * genio.c, genio.h: move contents into ports.c, ports.h. The division wasn't useful. * fports.c, fports.h (scm_fport_drain_input): new procedure. * ports.c (scm_drain_input): call scm_fport_drain_input. * scm_fdes_waiting_p: new procedure. * fports.c (scm_fdes_to_port): allocate read and/or write buffers. (scm_input_waiting_p): check the buffer. (local_fgetc, local_fflush, local_fputc): likewise. * fports.h (scm_fport): read/write_buf,_pos,_buf_end,,_buf_size: new members. * init.c (scm_init_standard_ports): pass fdes instead of FILE *. * * ports.c (scm_drain_input): new procedure. ports.h: prototype. * fports.c (FPORT_READ_SAFE, FPORT_WRITE_SAFE, FPORT_ALL_OKAY, pre_read, pre_write): removed. (local_fputc, local_fputs, local_ffwrite): use write, not stdio. (scm_standard_stream_to_port): change first arg from FILE * to int fdes. (local_fflush): flush fdes, not FILE *. * fports.h (SCM_NOFTELL): removed. * genio.c, ports.c: don't include filesys.h. * genio.c (scm_getc): don't use scm_internal_select if FPORT. do it in fports.c:local_fgetc. * genio.c: don't use SCM_SYSCALL when calling ptob procedures. do it where it's needed in the port smobs. * filesys.c (scm_input_waiting_p): moved to fports.c, stdio buffer support removed. take SCM arg, not FILE *. * filesys.h: prototype moved too. * fports.c (scm_fdes_to_port): new procedure. (local_fgetc): use read not fgetc. (local_fclose): use close, not fclose. (local_fgets): use read, not fgets * fports.h: prototype for scm_fdes_to_port. * fports.h (scm_fport): new struct. * fports.c (scm_open_file): use open, not fopen. #include fcntl.h * ports.h (struct scm_port_table): change stream from SCM to void *. * ports.c (scm_add_to_port_table): check for memory allocation error. (scm_prinport): remove MSDOS hair. (scm_void_port): set stream to 0 instead of SCM_BOOL_F. (scm_close_port): don't throw errors: do it in fports.c.
634 lines
15 KiB
C
634 lines
15 KiB
C
/* Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
|
||
*
|
||
* This program is free software; you can redistribute it and/or modify
|
||
* it under the terms of the GNU General Public License as published by
|
||
* the Free Software Foundation; either version 2, or (at your option)
|
||
* any later version.
|
||
*
|
||
* This program is distributed in the hope that it will be useful,
|
||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
* GNU General Public License for more details.
|
||
*
|
||
* You should have received a copy of the GNU General Public License
|
||
* along with this software; see the file COPYING. If not, write to
|
||
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
||
* Boston, MA 02111-1307 USA
|
||
*
|
||
* As a special exception, the Free Software Foundation gives permission
|
||
* for additional uses of the text contained in its release of GUILE.
|
||
*
|
||
* The exception is that, if you link the GUILE library with other files
|
||
* to produce an executable, this does not by itself cause the
|
||
* resulting executable to be covered by the GNU General Public License.
|
||
* Your use of that executable is in no way restricted on account of
|
||
* linking the GUILE library code into it.
|
||
*
|
||
* This exception does not however invalidate any other reasons why
|
||
* the executable file might be covered by the GNU General Public License.
|
||
*
|
||
* This exception applies only to the code released by the
|
||
* Free Software Foundation under the name GUILE. If you copy
|
||
* code from other Free Software Foundation releases into a copy of
|
||
* GUILE, as the General Public License permits, the exception does
|
||
* not apply to the code that you add in this way. To avoid misleading
|
||
* anyone as to the status of such modified files, you must delete
|
||
* this exception notice from them.
|
||
*
|
||
* If you write modifications of your own for GUILE, it is your choice
|
||
* whether to permit this exception to apply to your modifications.
|
||
* If you do not wish that, delete this exception notice. */
|
||
|
||
|
||
|
||
#include <stdio.h>
|
||
#include "_scm.h"
|
||
#include "ports.h"
|
||
#include "read.h"
|
||
#include "fports.h"
|
||
#include "unif.h"
|
||
#include "chars.h"
|
||
|
||
#include "ioext.h"
|
||
|
||
#include <fcntl.h>
|
||
|
||
#ifdef HAVE_STRING_H
|
||
#include <string.h>
|
||
#endif
|
||
#ifdef HAVE_UNISTD_H
|
||
#include <unistd.h>
|
||
#endif
|
||
|
||
|
||
SCM_PROC (s_read_delimited_x, "%read-delimited!", 3, 3, 0, scm_read_delimited_x);
|
||
|
||
SCM
|
||
scm_read_delimited_x (delims, buf, gobble, port, start, end)
|
||
SCM delims;
|
||
SCM buf;
|
||
SCM gobble;
|
||
SCM port;
|
||
SCM start;
|
||
SCM end;
|
||
{
|
||
long j;
|
||
char *cbuf;
|
||
long cstart;
|
||
long cend;
|
||
int c;
|
||
char *cdelims;
|
||
int num_delims;
|
||
|
||
SCM_ASSERT (SCM_NIMP (delims) && SCM_ROSTRINGP (delims),
|
||
delims, SCM_ARG1, s_read_delimited_x);
|
||
cdelims = SCM_ROCHARS (delims);
|
||
num_delims = SCM_ROLENGTH (delims);
|
||
SCM_ASSERT (SCM_NIMP (buf) && SCM_STRINGP (buf),
|
||
buf, SCM_ARG2, s_read_delimited_x);
|
||
cbuf = SCM_CHARS (buf);
|
||
cend = SCM_LENGTH (buf);
|
||
if (SCM_UNBNDP (port))
|
||
port = scm_cur_inp;
|
||
else
|
||
{
|
||
SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port),
|
||
port, SCM_ARG1, s_read_delimited_x);
|
||
}
|
||
|
||
if (SCM_UNBNDP (start))
|
||
cstart = 0;
|
||
else
|
||
{
|
||
cstart = scm_num2long (start,
|
||
(char *) SCM_ARG5, s_read_delimited_x);
|
||
if (cstart < 0 || cstart >= cend)
|
||
scm_out_of_range (s_read_delimited_x, start);
|
||
|
||
if (!SCM_UNBNDP (end))
|
||
{
|
||
long tend = scm_num2long (end, (char *) SCM_ARG6,
|
||
s_read_delimited_x);
|
||
if (tend <= cstart || tend > cend)
|
||
scm_out_of_range (s_read_delimited_x, end);
|
||
cend = tend;
|
||
}
|
||
}
|
||
|
||
for (j = cstart; j < cend; j++)
|
||
{
|
||
int k;
|
||
|
||
c = scm_getc (port);
|
||
for (k = 0; k < num_delims; k++)
|
||
{
|
||
if (cdelims[k] == c)
|
||
{
|
||
if (SCM_FALSEP (gobble))
|
||
scm_ungetc (c, port);
|
||
|
||
return scm_cons (SCM_MAKICHR (c),
|
||
scm_long2num (j - cstart));
|
||
}
|
||
}
|
||
if (c == EOF)
|
||
return scm_cons (SCM_EOF_VAL,
|
||
scm_long2num (j - cstart));
|
||
|
||
cbuf[j] = c;
|
||
}
|
||
return scm_cons (SCM_BOOL_F, scm_long2num (j - cstart));
|
||
}
|
||
|
||
static unsigned char *
|
||
scm_do_read_line (SCM port, int *len_p)
|
||
{
|
||
struct scm_port_table *pt = SCM_PTAB_ENTRY (port);
|
||
unsigned char *end;
|
||
|
||
/* I thought reading lines was simple. Mercy me. */
|
||
|
||
/* If there are any pushed-back characters, read the line character
|
||
by character. */
|
||
if (SCM_CRDYP (port))
|
||
{
|
||
int buf_size = 60;
|
||
/* Invariant: buf always has buf_size + 1 characters allocated;
|
||
the `+ 1' is for the final '\0'. */
|
||
unsigned char *buf = malloc (buf_size + 1);
|
||
int buf_len = 0;
|
||
int c;
|
||
|
||
while ((c = scm_getc (port)) != EOF)
|
||
{
|
||
if (buf_len >= buf_size)
|
||
{
|
||
buf = realloc (buf, buf_size * 2 + 1);
|
||
buf_size *= 2;
|
||
}
|
||
|
||
buf[buf_len++] = c;
|
||
|
||
if (c == '\n')
|
||
break;
|
||
}
|
||
|
||
/* Since SCM_CRDYP returned true, we ought to have gotten at
|
||
least one character. */
|
||
if (buf_len == 0)
|
||
abort ();
|
||
|
||
buf[buf_len] = '\0';
|
||
|
||
*len_p = buf_len;
|
||
return buf;
|
||
}
|
||
|
||
/* The common case: no unread characters, and the buffer contains
|
||
a complete line. This needs to be fast. */
|
||
if ((end = memchr (pt->read_pos, '\n', (pt->read_end - pt->read_pos)))
|
||
!= 0)
|
||
{
|
||
int buf_len = (end + 1) - pt->read_pos;
|
||
/* Allocate a buffer of the perfect size. */
|
||
unsigned char *buf = malloc (buf_len + 1);
|
||
|
||
memcpy (buf, pt->read_pos, buf_len);
|
||
pt->read_pos += buf_len;
|
||
|
||
buf[buf_len] = '\0';
|
||
|
||
*len_p = buf_len;
|
||
return buf;
|
||
}
|
||
|
||
/* There are no unread characters, and the buffer contains no newlines. */
|
||
{
|
||
/* When live, len is always the number of characters in the
|
||
current buffer that are part of the current line. */
|
||
int len = (pt->read_end - pt->read_pos);
|
||
int buf_size = (len < 50) ? 60 : len * 2;
|
||
/* Invariant: buf always has buf_size + 1 characters allocated;
|
||
the `+ 1' is for the final '\0'. */
|
||
unsigned char *buf = malloc (buf_size + 1);
|
||
int buf_len = 0;
|
||
int c;
|
||
|
||
for (;;)
|
||
{
|
||
if (buf_len + len > buf_size)
|
||
{
|
||
int new_size = (buf_len + len) * 2;
|
||
buf = realloc (buf, new_size + 1);
|
||
buf_size = new_size;
|
||
}
|
||
|
||
/* Copy what we've got out of the port, into our buffer. */
|
||
memcpy (buf + buf_len, pt->read_pos, len);
|
||
buf_len += len;
|
||
pt->read_pos += len;
|
||
|
||
/* If we had seen a newline, we're done now. */
|
||
if (end)
|
||
break;
|
||
|
||
/* Get more characters. I think having fill_buffer return a
|
||
character is not terribly graceful... */
|
||
c = (scm_ptobs[SCM_PTOBNUM (port)].fill_buffer) (port);
|
||
if (c == EOF)
|
||
{
|
||
/* If we're missing a final newline in the file, return
|
||
what we did get, sans newline. */
|
||
if (buf_len > 0)
|
||
break;
|
||
|
||
free (buf);
|
||
return 0;
|
||
}
|
||
|
||
/* ... because it makes us duplicate code here... */
|
||
if (buf_len + 1 > buf_size)
|
||
{
|
||
int new_size = buf_size * 2;
|
||
buf = realloc (buf, new_size + 1);
|
||
buf_size = new_size;
|
||
}
|
||
|
||
/* ... and this is really a duplication of the memcpy and
|
||
memchr calls, on a single-byte buffer. */
|
||
buf[buf_len++] = c;
|
||
if (c == '\n')
|
||
break;
|
||
|
||
/* Search the buffer for newlines. */
|
||
if ((end = memchr (pt->read_pos, '\n',
|
||
(len = (pt->read_end - pt->read_pos))))
|
||
!= 0)
|
||
len = (end - pt->read_pos) + 1;
|
||
}
|
||
|
||
/* I wonder how expensive this realloc is. */
|
||
buf = realloc (buf, buf_len + 1);
|
||
buf[buf_len] = '\0';
|
||
*len_p = buf_len;
|
||
return buf;
|
||
}
|
||
}
|
||
|
||
|
||
/*
|
||
* %read-line
|
||
* truncates any terminating newline from its input, and returns
|
||
* a cons of the string read and its terminating character. Doing
|
||
* so makes it easy to implement the hairy `read-line' options
|
||
* efficiently in Scheme.
|
||
*/
|
||
|
||
SCM_PROC (s_read_line, "%read-line", 0, 1, 0, scm_read_line);
|
||
|
||
SCM
|
||
scm_read_line (port)
|
||
SCM port;
|
||
{
|
||
char *s;
|
||
int slen;
|
||
SCM line, term;
|
||
|
||
if (SCM_UNBNDP (port))
|
||
port = scm_cur_inp;
|
||
else
|
||
{
|
||
SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port),
|
||
port, SCM_ARG1, s_read_line);
|
||
}
|
||
|
||
s = scm_do_read_line (port, &slen);
|
||
|
||
if (s == NULL)
|
||
term = line = SCM_EOF_VAL;
|
||
else
|
||
{
|
||
if (s[slen-1] == '\n')
|
||
{
|
||
term = SCM_MAKICHR ('\n');
|
||
s[slen-1] = '\0';
|
||
line = scm_take_str (s, slen-1);
|
||
SCM_INCLINE (port);
|
||
}
|
||
else
|
||
{
|
||
/* Fix: we should check for eof on the port before assuming this. */
|
||
term = SCM_EOF_VAL;
|
||
line = scm_take_str (s, slen);
|
||
}
|
||
}
|
||
|
||
return scm_cons (line, term);
|
||
}
|
||
|
||
SCM_PROC (s_write_line, "write-line", 1, 1, 0, scm_write_line);
|
||
|
||
SCM
|
||
scm_write_line (obj, port)
|
||
SCM obj;
|
||
SCM port;
|
||
{
|
||
scm_display (obj, port);
|
||
return scm_newline (port);
|
||
}
|
||
|
||
SCM_PROC (s_ftell, "ftell", 1, 0, 0, scm_ftell);
|
||
|
||
SCM
|
||
scm_ftell (object)
|
||
SCM object;
|
||
{
|
||
if (SCM_INUMP (object))
|
||
{
|
||
int fdes = SCM_INUM (object);
|
||
fpos_t pos;
|
||
|
||
pos = lseek (fdes, 0, SEEK_CUR);
|
||
if (pos == -1)
|
||
scm_syserror (s_ftell);
|
||
return scm_long2num (pos);
|
||
}
|
||
else
|
||
{
|
||
struct scm_fport *fp;
|
||
struct scm_port_table *pt;
|
||
int fdes;
|
||
fpos_t pos;
|
||
|
||
object = SCM_COERCE_OUTPORT (object);
|
||
SCM_ASSERT (SCM_NIMP (object) && SCM_OPFPORTP (object),
|
||
object, SCM_ARG1, s_ftell);
|
||
fp = SCM_FSTREAM (object);
|
||
pt = SCM_PTAB_ENTRY (object);
|
||
fdes = fp->fdes;
|
||
pos = lseek (fdes, 0, SEEK_CUR);
|
||
if (pos == -1)
|
||
scm_syserror (s_ftell);
|
||
/* the seek will only have succeeded if fdes is random access,
|
||
in which case only one buffer can be filled. */
|
||
if (pt->write_pos > pt->write_buf)
|
||
{
|
||
pos += pt->write_pos - pt->write_buf;
|
||
}
|
||
else
|
||
{
|
||
pos -= pt->read_end - pt->read_pos;
|
||
if (SCM_CRDYP (object))
|
||
pos -= SCM_N_READY_CHARS (object);
|
||
}
|
||
return scm_long2num (pos);
|
||
}
|
||
}
|
||
|
||
/* clear the three buffers in a port. */
|
||
#define SCM_CLEAR_BUFFERS(port, pt)\
|
||
{\
|
||
if (pt->write_pos > pt->write_buf)\
|
||
scm_fflush (port);\
|
||
pt->read_pos = pt->read_end = pt->read_buf;\
|
||
pt->write_needs_seek = 0;\
|
||
SCM_CLRDY (port);\
|
||
}
|
||
|
||
SCM_PROC (s_fseek, "fseek", 3, 0, 0, scm_fseek);
|
||
|
||
SCM
|
||
scm_fseek (object, offset, whence)
|
||
SCM object;
|
||
SCM offset;
|
||
SCM whence;
|
||
{
|
||
int rv;
|
||
long loff;
|
||
|
||
object = SCM_COERCE_OUTPORT (object);
|
||
|
||
loff = scm_num2long (offset, (char *)SCM_ARG2, s_fseek);
|
||
SCM_ASSERT (SCM_INUMP (whence), whence, SCM_ARG3, s_fseek);
|
||
if (SCM_NIMP (object) && SCM_OPFPORTP (object))
|
||
{
|
||
struct scm_fport *fp = SCM_FSTREAM (object);
|
||
struct scm_port_table *pt = SCM_PTAB_ENTRY (object);
|
||
|
||
/* clear the three buffers. the write buffer should be flushed
|
||
before changing the position. */
|
||
if (fp->random)
|
||
{
|
||
SCM_CLEAR_BUFFERS (object, pt);
|
||
} /* if not random, lseek will fail. */
|
||
rv = lseek (fp->fdes, loff, SCM_INUM (whence));
|
||
if (rv == -1)
|
||
scm_syserror (s_fseek);
|
||
}
|
||
else
|
||
{
|
||
SCM_ASSERT (SCM_INUMP (object), object, SCM_ARG1, s_fseek);
|
||
rv = lseek (SCM_INUM (object), loff, SCM_INUM (whence));
|
||
if (rv == -1)
|
||
scm_syserror (s_fseek);
|
||
}
|
||
return SCM_UNSPECIFIED;
|
||
}
|
||
|
||
SCM_PROC (s_redirect_port, "redirect-port", 2, 0, 0, scm_redirect_port);
|
||
|
||
SCM
|
||
scm_redirect_port (old, new)
|
||
SCM old;
|
||
SCM new;
|
||
{
|
||
int ans, oldfd, newfd;
|
||
struct scm_fport *fp;
|
||
|
||
old = SCM_COERCE_OUTPORT (old);
|
||
new = SCM_COERCE_OUTPORT (new);
|
||
|
||
SCM_ASSERT (SCM_NIMP (old) && SCM_OPFPORTP (old), old, SCM_ARG1, s_redirect_port);
|
||
SCM_ASSERT (SCM_NIMP (new) && SCM_OPFPORTP (new), new, SCM_ARG2, s_redirect_port);
|
||
oldfd = SCM_FPORT_FDES (old);
|
||
fp = SCM_FSTREAM (new);
|
||
newfd = fp->fdes;
|
||
if (oldfd != newfd)
|
||
{
|
||
struct scm_port_table *pt = SCM_PTAB_ENTRY (new);
|
||
|
||
/* must flush to old fdes. don't clear all buffers here
|
||
in case dup2 fails. */
|
||
if (pt->write_pos > pt->write_buf)
|
||
scm_fflush (new);
|
||
ans = dup2 (oldfd, newfd);
|
||
if (ans == -1)
|
||
scm_syserror (s_redirect_port);
|
||
fp->random = SCM_FDES_RANDOM_P (fp->fdes);
|
||
/* continue using existing buffers, even if inappropriate. */
|
||
SCM_CLEAR_BUFFERS (new, pt);
|
||
}
|
||
return SCM_UNSPECIFIED;
|
||
}
|
||
|
||
SCM_PROC (s_dup_to_fdes, "dup->fdes", 1, 1, 0, scm_dup_to_fdes);
|
||
SCM
|
||
scm_dup_to_fdes (SCM fd_or_port, SCM fd)
|
||
{
|
||
int oldfd, newfd, rv;
|
||
|
||
fd_or_port = SCM_COERCE_OUTPORT (fd_or_port);
|
||
|
||
if (SCM_INUMP (fd_or_port))
|
||
oldfd = SCM_INUM (fd_or_port);
|
||
else
|
||
{
|
||
SCM_ASSERT (SCM_NIMP (fd_or_port) && SCM_OPFPORTP (fd_or_port),
|
||
fd_or_port, SCM_ARG1, s_dup_to_fdes);
|
||
oldfd = SCM_FPORT_FDES (fd_or_port);
|
||
}
|
||
|
||
if (SCM_UNBNDP (fd))
|
||
{
|
||
newfd = dup (oldfd);
|
||
if (newfd == -1)
|
||
scm_syserror (s_dup_to_fdes);
|
||
fd = SCM_MAKINUM (newfd);
|
||
}
|
||
else
|
||
{
|
||
SCM_ASSERT (SCM_INUMP (fd), fd, SCM_ARG2, s_dup_to_fdes);
|
||
newfd = SCM_INUM (fd);
|
||
if (oldfd != newfd)
|
||
{
|
||
scm_evict_ports (newfd); /* see scsh manual. */
|
||
rv = dup2 (oldfd, newfd);
|
||
if (rv == -1)
|
||
scm_syserror (s_dup_to_fdes);
|
||
}
|
||
}
|
||
return fd;
|
||
}
|
||
|
||
SCM_PROC (s_fileno, "fileno", 1, 0, 0, scm_fileno);
|
||
|
||
SCM
|
||
scm_fileno (port)
|
||
SCM port;
|
||
{
|
||
port = SCM_COERCE_OUTPORT (port);
|
||
SCM_ASSERT (SCM_NIMP (port) && SCM_OPFPORTP (port), port, SCM_ARG1,
|
||
s_fileno);
|
||
return SCM_MAKINUM (SCM_FPORT_FDES (port));
|
||
}
|
||
|
||
SCM_PROC (s_isatty, "isatty?", 1, 0, 0, scm_isatty_p);
|
||
|
||
SCM
|
||
scm_isatty_p (port)
|
||
SCM port;
|
||
{
|
||
int rv;
|
||
|
||
port = SCM_COERCE_OUTPORT (port);
|
||
|
||
if (!(SCM_NIMP (port) && SCM_OPFPORTP (port)))
|
||
return SCM_BOOL_F;
|
||
|
||
rv = isatty (SCM_FPORT_FDES (port));
|
||
return rv ? SCM_BOOL_T : SCM_BOOL_F;
|
||
}
|
||
|
||
|
||
|
||
SCM_PROC (s_fdopen, "fdopen", 2, 0, 0, scm_fdopen);
|
||
|
||
SCM
|
||
scm_fdopen (fdes, modes)
|
||
SCM fdes;
|
||
SCM modes;
|
||
{
|
||
SCM port;
|
||
|
||
SCM_ASSERT (SCM_INUMP (fdes), fdes, SCM_ARG1, s_fdopen);
|
||
SCM_ASSERT (SCM_NIMP (modes) && SCM_ROSTRINGP (modes), modes, SCM_ARG2,
|
||
s_fdopen);
|
||
SCM_COERCE_SUBSTR (modes);
|
||
port = scm_fdes_to_port (SCM_INUM (fdes), SCM_ROCHARS (modes), SCM_BOOL_F);
|
||
return port;
|
||
}
|
||
|
||
|
||
|
||
/* Move a port's underlying file descriptor to a given value.
|
||
* Returns #f if fdes is already the given value.
|
||
* #t if fdes moved.
|
||
* MOVE->FDES is implemented in Scheme and calls this primitive.
|
||
*/
|
||
SCM_PROC (s_primitive_move_to_fdes, "primitive-move->fdes", 2, 0, 0, scm_primitive_move_to_fdes);
|
||
|
||
SCM
|
||
scm_primitive_move_to_fdes (port, fd)
|
||
SCM port;
|
||
SCM fd;
|
||
{
|
||
struct scm_fport *stream;
|
||
int old_fd;
|
||
int new_fd;
|
||
int rv;
|
||
|
||
port = SCM_COERCE_OUTPORT (port);
|
||
|
||
SCM_ASSERT (SCM_NIMP (port) && SCM_OPFPORTP (port), port, SCM_ARG1, s_primitive_move_to_fdes);
|
||
SCM_ASSERT (SCM_INUMP (fd), fd, SCM_ARG2, s_primitive_move_to_fdes);
|
||
stream = SCM_FSTREAM (port);
|
||
old_fd = stream->fdes;
|
||
new_fd = SCM_INUM (fd);
|
||
if (old_fd == new_fd)
|
||
{
|
||
return SCM_BOOL_F;
|
||
}
|
||
scm_evict_ports (new_fd);
|
||
rv = dup2 (old_fd, new_fd);
|
||
if (rv == -1)
|
||
scm_syserror (s_primitive_move_to_fdes);
|
||
stream->fdes = new_fd;
|
||
SCM_SYSCALL (close (old_fd));
|
||
return SCM_BOOL_T;
|
||
}
|
||
|
||
/* Return a list of ports using a given file descriptor. */
|
||
SCM_PROC(s_fdes_to_ports, "fdes->ports", 1, 0, 0, scm_fdes_to_ports);
|
||
|
||
SCM
|
||
scm_fdes_to_ports (fd)
|
||
SCM fd;
|
||
{
|
||
SCM result = SCM_EOL;
|
||
int int_fd;
|
||
int i;
|
||
|
||
SCM_ASSERT (SCM_INUMP (fd), fd, SCM_ARG1, s_fdes_to_ports);
|
||
int_fd = SCM_INUM (fd);
|
||
|
||
for (i = 0; i < scm_port_table_size; i++)
|
||
{
|
||
if (SCM_OPFPORTP (scm_port_table[i]->port)
|
||
&& ((struct scm_fport *) scm_port_table[i]->stream)->fdes == int_fd)
|
||
result = scm_cons (scm_port_table[i]->port, result);
|
||
}
|
||
return result;
|
||
}
|
||
|
||
|
||
void
|
||
scm_init_ioext ()
|
||
{
|
||
/* fseek() symbols. */
|
||
scm_sysintern ("SEEK_SET", SCM_MAKINUM (SEEK_SET));
|
||
scm_sysintern ("SEEK_CUR", SCM_MAKINUM (SEEK_CUR));
|
||
scm_sysintern ("SEEK_END", SCM_MAKINUM (SEEK_END));
|
||
|
||
#include "ioext.x"
|
||
}
|
||
|