1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Objective Caml */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../LICENSE. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
/* Buffered input/output. */
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
1998-10-07 12:01:42 -07:00
|
|
|
#include <limits.h>
|
1995-05-04 03:15:53 -07:00
|
|
|
#include <string.h>
|
1996-02-21 02:49:46 -08:00
|
|
|
#include "config.h"
|
|
|
|
#ifdef HAS_UNISTD
|
1995-05-04 03:15:53 -07:00
|
|
|
#include <unistd.h>
|
1996-02-21 02:49:46 -08:00
|
|
|
#endif
|
1995-05-04 03:15:53 -07:00
|
|
|
#include "alloc.h"
|
2000-02-10 06:04:59 -08:00
|
|
|
#include "custom.h"
|
1995-05-04 03:15:53 -07:00
|
|
|
#include "fail.h"
|
|
|
|
#include "io.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mlvalues.h"
|
|
|
|
#include "signals.h"
|
|
|
|
#include "sys.h"
|
|
|
|
#ifdef HAS_UI
|
|
|
|
#include "ui.h"
|
|
|
|
#endif
|
|
|
|
|
1996-04-18 09:29:57 -07:00
|
|
|
#ifndef SEEK_SET
|
|
|
|
#define SEEK_SET 0
|
|
|
|
#define SEEK_CUR 1
|
|
|
|
#define SEEK_END 2
|
|
|
|
#endif
|
|
|
|
|
1997-08-29 08:37:22 -07:00
|
|
|
/* Hooks for locking channels */
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport void (*channel_mutex_free) (struct channel *) = NULL;
|
|
|
|
CAMLexport void (*channel_mutex_lock) (struct channel *) = NULL;
|
|
|
|
CAMLexport void (*channel_mutex_unlock) (struct channel *) = NULL;
|
|
|
|
CAMLexport void (*channel_mutex_unlock_exn) (void) = NULL;
|
1997-08-29 08:37:22 -07:00
|
|
|
|
2001-10-09 08:14:01 -07:00
|
|
|
/* List of opened channels */
|
|
|
|
CAMLexport struct channel * all_opened_channels = NULL;
|
|
|
|
|
1997-08-29 08:37:22 -07:00
|
|
|
/* Basic functions over type struct channel *.
|
|
|
|
These functions can be called directly from C.
|
|
|
|
No locking is performed. */
|
1997-05-13 07:05:44 -07:00
|
|
|
|
1998-02-26 04:51:39 -08:00
|
|
|
/* Functions shared between input and output */
|
2000-01-07 08:05:19 -08:00
|
|
|
|
2001-10-09 08:14:01 -07:00
|
|
|
CAMLexport struct channel * open_descriptor_in(int fd)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
struct channel * channel;
|
|
|
|
|
1997-08-29 08:37:22 -07:00
|
|
|
channel = (struct channel *) stat_alloc(sizeof(struct channel));
|
1995-05-04 03:15:53 -07:00
|
|
|
channel->fd = fd;
|
|
|
|
channel->offset = 0;
|
1997-08-29 08:37:22 -07:00
|
|
|
channel->curr = channel->max = channel->buff;
|
|
|
|
channel->end = channel->buff + IO_BUFFER_SIZE;
|
|
|
|
channel->mutex = NULL;
|
2001-10-09 08:14:01 -07:00
|
|
|
channel->revealed = 0;
|
|
|
|
channel->old_revealed = 0;
|
|
|
|
channel->refcount = 0;
|
|
|
|
channel->next = all_opened_channels;
|
|
|
|
all_opened_channels = channel;
|
1995-05-04 03:15:53 -07:00
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
2001-10-09 08:14:01 -07:00
|
|
|
CAMLexport struct channel * open_descriptor_out(int fd)
|
|
|
|
{
|
|
|
|
struct channel * channel;
|
|
|
|
|
|
|
|
channel = open_descriptor_in(fd);
|
|
|
|
channel->max = NULL;
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unlink_channel(struct channel *channel)
|
|
|
|
{
|
|
|
|
struct channel ** cp = &all_opened_channels;
|
|
|
|
|
|
|
|
while (*cp != channel && *cp != NULL)
|
|
|
|
cp = &(*cp)->next;
|
|
|
|
if (*cp != NULL)
|
|
|
|
*cp = (*cp)->next;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport void close_channel(struct channel *channel)
|
1996-04-18 09:29:57 -07:00
|
|
|
{
|
|
|
|
close(channel->fd);
|
2001-10-09 08:14:01 -07:00
|
|
|
if (channel->refcount > 0) return;
|
1997-08-29 08:37:22 -07:00
|
|
|
if (channel_mutex_free != NULL) (*channel_mutex_free)(channel);
|
2001-10-09 08:14:01 -07:00
|
|
|
unlink_channel(channel);
|
1997-11-20 07:30:43 -08:00
|
|
|
stat_free(channel);
|
2000-01-07 08:05:19 -08:00
|
|
|
}
|
1996-04-18 09:29:57 -07:00
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport long channel_size(struct channel *channel)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
long end;
|
|
|
|
|
1996-04-18 09:29:57 -07:00
|
|
|
end = lseek(channel->fd, 0, SEEK_END);
|
1997-08-29 08:37:22 -07:00
|
|
|
if (end == -1 ||
|
|
|
|
lseek(channel->fd, channel->offset, SEEK_SET) != channel->offset) {
|
1996-10-01 02:46:42 -07:00
|
|
|
sys_error(NO_ARG);
|
1997-08-29 08:37:22 -07:00
|
|
|
}
|
|
|
|
return end;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport int channel_binary_mode(struct channel *channel)
|
1998-02-26 04:51:39 -08:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
1998-05-12 04:23:15 -07:00
|
|
|
int oldmode = setmode(channel->fd, O_BINARY);
|
|
|
|
if (oldmode == O_TEXT) setmode(channel->fd, O_TEXT);
|
1998-02-26 04:51:39 -08:00
|
|
|
return oldmode == O_BINARY;
|
|
|
|
#else
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
/* Output */
|
|
|
|
|
1996-04-29 06:18:36 -07:00
|
|
|
#ifndef EINTR
|
|
|
|
#define EINTR (-1)
|
|
|
|
#endif
|
|
|
|
#ifndef EAGAIN
|
|
|
|
#define EAGAIN (-1)
|
|
|
|
#endif
|
|
|
|
#ifndef EWOULDBLOCK
|
|
|
|
#define EWOULDBLOCK (-1)
|
|
|
|
#endif
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static int do_write(int fd, char *p, int n)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
int retcode;
|
1996-04-18 09:29:57 -07:00
|
|
|
|
1996-04-01 07:24:38 -08:00
|
|
|
Assert(!Is_young(p));
|
1995-05-04 03:15:53 -07:00
|
|
|
#ifdef HAS_UI
|
1996-04-18 09:29:57 -07:00
|
|
|
retcode = ui_write(fd, p, n);
|
1995-05-04 03:15:53 -07:00
|
|
|
#else
|
1996-04-29 06:18:36 -07:00
|
|
|
again:
|
1996-07-01 05:43:28 -07:00
|
|
|
enter_blocking_section();
|
1996-04-18 09:29:57 -07:00
|
|
|
retcode = write(fd, p, n);
|
1996-07-01 05:43:28 -07:00
|
|
|
leave_blocking_section();
|
1996-04-29 06:18:36 -07:00
|
|
|
if (retcode == -1) {
|
|
|
|
if (errno == EINTR) goto again;
|
1998-11-20 07:36:27 -08:00
|
|
|
if ((errno == EAGAIN || errno == EWOULDBLOCK) && n > 1) {
|
1996-04-29 09:56:34 -07:00
|
|
|
/* We couldn't do a partial write here, probably because
|
1997-05-19 08:42:21 -07:00
|
|
|
n <= PIPE_BUF and POSIX says that writes of less than
|
|
|
|
PIPE_BUF characters must be atomic.
|
1998-11-20 07:36:27 -08:00
|
|
|
We first try again with a partial write of 1 character.
|
|
|
|
If that fails too, we'll raise Sys_blocked_io below. */
|
1998-10-01 05:31:34 -07:00
|
|
|
n = 1; goto again;
|
1996-04-29 06:18:36 -07:00
|
|
|
}
|
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
#endif
|
1996-10-01 02:46:42 -07:00
|
|
|
if (retcode == -1) sys_error(NO_ARG);
|
1996-04-18 09:29:57 -07:00
|
|
|
return retcode;
|
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1996-04-18 09:29:57 -07:00
|
|
|
/* Attempt to flush the buffer. This will make room in the buffer for
|
|
|
|
at least one character. Returns true if the buffer is empty at the
|
|
|
|
end of the flush, or false if some data remains in the buffer. */
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport int flush_partial(struct channel *channel)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1996-04-18 09:29:57 -07:00
|
|
|
int towrite, written;
|
1997-08-29 08:37:22 -07:00
|
|
|
|
1996-04-18 09:29:57 -07:00
|
|
|
towrite = channel->curr - channel->buff;
|
|
|
|
if (towrite > 0) {
|
|
|
|
written = do_write(channel->fd, channel->buff, towrite);
|
|
|
|
channel->offset += written;
|
|
|
|
if (written < towrite)
|
2000-10-12 11:05:42 -07:00
|
|
|
memmove(channel->buff, channel->buff + written, towrite - written);
|
1996-04-18 09:29:57 -07:00
|
|
|
channel->curr -= written;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
1997-08-29 08:37:22 -07:00
|
|
|
return (channel->curr == channel->buff);
|
1996-04-18 09:29:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Flush completely the buffer. */
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport void flush(struct channel *channel)
|
1996-04-18 09:29:57 -07:00
|
|
|
{
|
1997-08-29 08:37:22 -07:00
|
|
|
while (! flush_partial(channel)) /*nothing*/;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
1997-08-29 08:37:22 -07:00
|
|
|
/* Output data */
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport void putword(struct channel *channel, uint32 w)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1998-02-26 04:51:39 -08:00
|
|
|
if (! channel_binary_mode(channel))
|
|
|
|
failwith("output_binary_int: not a binary channel");
|
1995-05-04 03:15:53 -07:00
|
|
|
putch(channel, w >> 24);
|
|
|
|
putch(channel, w >> 16);
|
|
|
|
putch(channel, w >> 8);
|
|
|
|
putch(channel, w);
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport int putblock(struct channel *channel, char *p, long int len)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1996-04-18 09:29:57 -07:00
|
|
|
int n, free, towrite, written;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1996-02-13 08:26:14 -08:00
|
|
|
n = len >= INT_MAX ? INT_MAX : (int) len;
|
1996-04-18 09:29:57 -07:00
|
|
|
free = channel->end - channel->curr;
|
1997-05-14 05:01:00 -07:00
|
|
|
if (n <= free) {
|
1996-04-18 09:29:57 -07:00
|
|
|
/* Write request small enough to fit in buffer: transfer to buffer. */
|
2000-10-12 11:05:42 -07:00
|
|
|
memmove(channel->curr, p, n);
|
1995-05-04 03:15:53 -07:00
|
|
|
channel->curr += n;
|
1996-04-18 09:29:57 -07:00
|
|
|
return n;
|
1995-05-04 03:15:53 -07:00
|
|
|
} else {
|
1996-04-18 09:29:57 -07:00
|
|
|
/* Write request overflows buffer: transfer whatever fits to buffer
|
|
|
|
and write the buffer */
|
2000-10-12 11:05:42 -07:00
|
|
|
memmove(channel->curr, p, free);
|
1996-04-18 09:29:57 -07:00
|
|
|
towrite = channel->end - channel->buff;
|
|
|
|
written = do_write(channel->fd, channel->buff, towrite);
|
|
|
|
if (written < towrite)
|
2000-10-12 11:05:42 -07:00
|
|
|
memmove(channel->buff, channel->buff + written, towrite - written);
|
1996-04-18 09:29:57 -07:00
|
|
|
channel->offset += written;
|
|
|
|
channel->curr = channel->end - written;
|
|
|
|
return free;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport void really_putblock(struct channel *channel, char *p, long int len)
|
1996-04-18 09:29:57 -07:00
|
|
|
{
|
|
|
|
int written;
|
|
|
|
while (len > 0) {
|
|
|
|
written = putblock(channel, p, len);
|
|
|
|
p += written;
|
|
|
|
len -= written;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport void seek_out(struct channel *channel, long int dest)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1996-04-18 09:29:57 -07:00
|
|
|
flush(channel);
|
1996-10-01 02:46:42 -07:00
|
|
|
if (lseek(channel->fd, dest, 0) != dest) sys_error(NO_ARG);
|
1996-04-18 09:29:57 -07:00
|
|
|
channel->offset = dest;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport long pos_out(struct channel *channel)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1997-08-29 08:37:22 -07:00
|
|
|
return channel->offset + channel->curr - channel->buff;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Input */
|
|
|
|
|
2001-11-13 07:41:01 -08:00
|
|
|
CAMLexport int do_read(int fd, char *p, unsigned int n)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
int retcode;
|
|
|
|
|
1996-04-01 07:24:38 -08:00
|
|
|
Assert(!Is_young(p));
|
1995-05-04 03:15:53 -07:00
|
|
|
enter_blocking_section();
|
|
|
|
#ifdef HAS_UI
|
|
|
|
retcode = ui_read(fd, p, n);
|
|
|
|
#else
|
|
|
|
#ifdef EINTR
|
|
|
|
do { retcode = read(fd, p, n); } while (retcode == -1 && errno == EINTR);
|
|
|
|
#else
|
|
|
|
retcode = read(fd, p, n);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
leave_blocking_section();
|
1996-10-01 02:46:42 -07:00
|
|
|
if (retcode == -1) sys_error(NO_ARG);
|
1995-05-04 03:15:53 -07:00
|
|
|
return retcode;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport unsigned char refill(struct channel *channel)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
1996-04-18 09:29:57 -07:00
|
|
|
n = do_read(channel->fd, channel->buff, IO_BUFFER_SIZE);
|
1995-05-04 03:15:53 -07:00
|
|
|
if (n == 0) raise_end_of_file();
|
|
|
|
channel->offset += n;
|
|
|
|
channel->max = channel->buff + n;
|
|
|
|
channel->curr = channel->buff + 1;
|
|
|
|
return (unsigned char)(channel->buff[0]);
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport uint32 getword(struct channel *channel)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint32 res;
|
|
|
|
|
1998-02-26 04:51:39 -08:00
|
|
|
if (! channel_binary_mode(channel))
|
|
|
|
failwith("input_binary_int: not a binary channel");
|
1995-05-04 03:15:53 -07:00
|
|
|
res = 0;
|
|
|
|
for(i = 0; i < 4; i++) {
|
|
|
|
res = (res << 8) + getch(channel);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport int getblock(struct channel *channel, char *p, long int len)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1996-04-18 09:29:57 -07:00
|
|
|
int n, avail, nread;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1996-02-13 08:26:14 -08:00
|
|
|
n = len >= INT_MAX ? INT_MAX : (int) len;
|
1996-04-18 09:29:57 -07:00
|
|
|
avail = channel->max - channel->curr;
|
|
|
|
if (n <= avail) {
|
2000-10-12 11:05:42 -07:00
|
|
|
memmove(p, channel->curr, n);
|
1995-05-04 03:15:53 -07:00
|
|
|
channel->curr += n;
|
|
|
|
return n;
|
1996-04-18 09:29:57 -07:00
|
|
|
} else if (avail > 0) {
|
2000-10-12 11:05:42 -07:00
|
|
|
memmove(p, channel->curr, avail);
|
1996-04-18 09:29:57 -07:00
|
|
|
channel->curr += avail;
|
|
|
|
return avail;
|
2000-02-07 06:07:31 -08:00
|
|
|
} else {
|
1996-04-18 09:29:57 -07:00
|
|
|
nread = do_read(channel->fd, channel->buff, IO_BUFFER_SIZE);
|
|
|
|
channel->offset += nread;
|
|
|
|
channel->max = channel->buff + nread;
|
|
|
|
if (n > nread) n = nread;
|
2000-10-12 11:05:42 -07:00
|
|
|
memmove(p, channel->buff, n);
|
1995-05-04 03:15:53 -07:00
|
|
|
channel->curr = channel->buff + n;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport int really_getblock(struct channel *chan, char *p, long int n)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1996-02-13 08:26:14 -08:00
|
|
|
int r;
|
1995-05-04 03:15:53 -07:00
|
|
|
while (n > 0) {
|
1996-02-13 08:26:14 -08:00
|
|
|
r = getblock(chan, p, n);
|
1997-08-29 08:37:22 -07:00
|
|
|
if (r == 0) break;
|
1995-05-04 03:15:53 -07:00
|
|
|
p += r;
|
|
|
|
n -= r;
|
|
|
|
}
|
1997-08-29 08:37:22 -07:00
|
|
|
return (n == 0);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport void seek_in(struct channel *channel, long int dest)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
if (dest >= channel->offset - (channel->max - channel->buff) &&
|
|
|
|
dest <= channel->offset) {
|
|
|
|
channel->curr = channel->max - (channel->offset - dest);
|
|
|
|
} else {
|
1996-10-01 02:46:42 -07:00
|
|
|
if (lseek(channel->fd, dest, SEEK_SET) != dest) sys_error(NO_ARG);
|
1995-05-04 03:15:53 -07:00
|
|
|
channel->offset = dest;
|
|
|
|
channel->curr = channel->max = channel->buff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport long pos_in(struct channel *channel)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1997-08-29 08:37:22 -07:00
|
|
|
return channel->offset - (channel->max - channel->curr);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport long input_scan_line(struct channel *channel)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
char * p;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
p = channel->curr;
|
|
|
|
do {
|
|
|
|
if (p >= channel->max) {
|
|
|
|
/* No more characters available in the buffer */
|
|
|
|
if (channel->curr > channel->buff) {
|
|
|
|
/* Try to make some room in the buffer by shifting the unread
|
|
|
|
portion at the beginning */
|
2000-10-12 11:05:42 -07:00
|
|
|
memmove(channel->buff, channel->curr, channel->max - channel->curr);
|
1995-05-04 03:15:53 -07:00
|
|
|
n = channel->curr - channel->buff;
|
|
|
|
channel->curr -= n;
|
|
|
|
channel->max -= n;
|
|
|
|
p -= n;
|
|
|
|
}
|
|
|
|
if (channel->max >= channel->end) {
|
|
|
|
/* Buffer is full, no room to read more characters from the input.
|
|
|
|
Return the number of characters in the buffer, with negative
|
|
|
|
sign to indicate that no newline was encountered. */
|
1997-08-29 08:37:22 -07:00
|
|
|
return -(channel->max - channel->curr);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
/* Fill the buffer as much as possible */
|
1996-04-18 09:29:57 -07:00
|
|
|
n = do_read(channel->fd, channel->max, channel->end - channel->max);
|
1995-05-04 03:15:53 -07:00
|
|
|
if (n == 0) {
|
|
|
|
/* End-of-file encountered. Return the number of characters in the
|
2000-01-07 08:05:19 -08:00
|
|
|
buffer, with negative sign since we haven't encountered
|
1995-05-04 03:15:53 -07:00
|
|
|
a newline. */
|
1997-08-29 08:37:22 -07:00
|
|
|
return -(channel->max - channel->curr);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
channel->offset += n;
|
|
|
|
channel->max += n;
|
|
|
|
}
|
|
|
|
} while (*p++ != '\n');
|
|
|
|
/* Found a newline. Return the length of the line, newline included. */
|
1997-08-29 08:37:22 -07:00
|
|
|
return (p - channel->curr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Caml entry points for the I/O functions. Wrap struct channel *
|
2000-02-07 06:07:31 -08:00
|
|
|
objects into a heap-allocated object. Perform locking
|
1997-08-29 08:37:22 -07:00
|
|
|
and unlocking around the I/O operations. */
|
|
|
|
|
2001-10-09 08:14:01 -07:00
|
|
|
CAMLexport void finalize_channel(value vchan)
|
1997-08-29 08:37:22 -07:00
|
|
|
{
|
|
|
|
struct channel * chan = Channel(vchan);
|
2001-10-09 08:14:01 -07:00
|
|
|
if (--chan->refcount > 0) return;
|
1997-08-29 08:37:22 -07:00
|
|
|
if (channel_mutex_free != NULL) (*channel_mutex_free)(chan);
|
2001-10-09 08:14:01 -07:00
|
|
|
unlink_channel(chan);
|
1997-11-20 07:30:43 -08:00
|
|
|
stat_free(chan);
|
1997-08-29 08:37:22 -07:00
|
|
|
}
|
|
|
|
|
2000-05-16 02:11:51 -07:00
|
|
|
static int compare_channel(value vchan1, value vchan2)
|
|
|
|
{
|
|
|
|
struct channel * chan1 = Channel(vchan1);
|
|
|
|
struct channel * chan2 = Channel(vchan2);
|
|
|
|
return (chan1 == chan2) ? 0 : (chan1 < chan2) ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
2000-02-10 06:04:59 -08:00
|
|
|
static struct custom_operations channel_operations = {
|
|
|
|
"_chan",
|
|
|
|
finalize_channel,
|
2000-05-16 02:11:51 -07:00
|
|
|
compare_channel,
|
2000-02-10 06:04:59 -08:00
|
|
|
custom_hash_default,
|
|
|
|
custom_serialize_default,
|
|
|
|
custom_deserialize_default
|
|
|
|
};
|
|
|
|
|
2001-10-09 08:14:01 -07:00
|
|
|
CAMLexport value alloc_channel(struct channel *chan)
|
1997-08-29 08:37:22 -07:00
|
|
|
{
|
2000-02-10 06:04:59 -08:00
|
|
|
value res = alloc_custom(&channel_operations, sizeof(struct channel *),
|
|
|
|
1, 1000);
|
|
|
|
Channel(res) = chan;
|
2001-10-09 08:14:01 -07:00
|
|
|
chan->refcount++;
|
1997-08-29 08:37:22 -07:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-10-09 08:14:01 -07:00
|
|
|
CAMLprim value caml_open_descriptor_in(value fd)
|
|
|
|
{
|
|
|
|
return alloc_channel(open_descriptor_in(Int_val(fd)));
|
|
|
|
}
|
|
|
|
|
|
|
|
CAMLprim value caml_open_descriptor_out(value fd)
|
1997-08-29 08:37:22 -07:00
|
|
|
{
|
2001-10-09 08:14:01 -07:00
|
|
|
return alloc_channel(open_descriptor_out(Int_val(fd)));
|
|
|
|
}
|
|
|
|
|
|
|
|
#define Pair_tag 0
|
|
|
|
|
|
|
|
CAMLprim value caml_out_channels_list (value unit)
|
|
|
|
{
|
|
|
|
CAMLparam0 ();
|
|
|
|
CAMLlocal3 (res, tail, chan);
|
|
|
|
struct channel * channel;
|
|
|
|
|
|
|
|
res = Val_emptylist;
|
|
|
|
for (channel = all_opened_channels;
|
|
|
|
channel != NULL;
|
|
|
|
channel = channel->next)
|
|
|
|
/* Testing channel->fd >= 0 looks unnecessary, as
|
|
|
|
caml_close_channel changes max when setting fd to -1. */
|
|
|
|
if (channel->max == NULL) {
|
|
|
|
chan = alloc_channel (channel);
|
|
|
|
tail = res;
|
|
|
|
res = alloc_small (2, Pair_tag);
|
|
|
|
Field (res, 0) = chan;
|
|
|
|
Field (res, 1) = tail;
|
|
|
|
}
|
|
|
|
CAMLreturn (res);
|
1997-08-29 08:37:22 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value channel_descriptor(value vchannel)
|
1997-08-29 08:37:22 -07:00
|
|
|
{
|
1998-10-29 07:54:39 -08:00
|
|
|
int fd = Channel(vchannel)->fd;
|
|
|
|
if (fd == -1) { errno = EBADF; sys_error(NO_ARG); }
|
|
|
|
return Val_int(fd);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
1997-08-29 08:37:22 -07:00
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value caml_close_channel(value vchannel)
|
1997-08-29 08:37:22 -07:00
|
|
|
{
|
|
|
|
/* For output channels, must have flushed before */
|
|
|
|
struct channel * channel = Channel(vchannel);
|
|
|
|
close(channel->fd);
|
|
|
|
channel->fd = -1;
|
2001-02-06 07:21:50 -08:00
|
|
|
/* Ensure that every read or write on the channel will cause an
|
|
|
|
immediate flush_partial or refill, thus raising a Sys_error
|
|
|
|
exception */
|
|
|
|
channel->curr = channel->max = channel->end;
|
1997-08-29 08:37:22 -07:00
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value caml_channel_size(value vchannel)
|
1997-08-29 08:37:22 -07:00
|
|
|
{
|
|
|
|
return Val_long(channel_size(Channel(vchannel)));
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value caml_set_binary_mode(value vchannel, value mode)
|
1998-07-02 02:51:50 -07:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
struct channel * channel = Channel(vchannel);
|
|
|
|
if (setmode(channel->fd, Bool_val(mode) ? O_BINARY : O_TEXT) == -1)
|
|
|
|
sys_error(NO_ARG);
|
|
|
|
#endif
|
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value caml_flush_partial(value vchannel)
|
1997-08-29 08:37:22 -07:00
|
|
|
{
|
1998-06-11 05:53:08 -07:00
|
|
|
struct channel * channel = Channel(vchannel);
|
|
|
|
int res;
|
2001-10-09 08:14:01 -07:00
|
|
|
|
1998-06-11 05:53:08 -07:00
|
|
|
Lock(channel);
|
|
|
|
res = flush_partial(channel);
|
|
|
|
Unlock(channel);
|
|
|
|
return Val_bool(res);
|
1997-08-29 08:37:22 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value caml_flush(value vchannel)
|
1997-08-29 08:37:22 -07:00
|
|
|
{
|
1998-06-11 05:53:08 -07:00
|
|
|
struct channel * channel = Channel(vchannel);
|
2001-10-09 08:14:01 -07:00
|
|
|
|
1998-06-11 05:53:08 -07:00
|
|
|
Lock(channel);
|
|
|
|
flush(channel);
|
|
|
|
Unlock(channel);
|
1997-08-29 08:37:22 -07:00
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value caml_output_char(value vchannel, value ch)
|
1997-08-29 08:37:22 -07:00
|
|
|
{
|
|
|
|
struct channel * channel = Channel(vchannel);
|
|
|
|
Lock(channel);
|
|
|
|
putch(channel, Long_val(ch));
|
|
|
|
Unlock(channel);
|
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value caml_output_int(value vchannel, value w)
|
1997-08-29 08:37:22 -07:00
|
|
|
{
|
|
|
|
struct channel * channel = Channel(vchannel);
|
|
|
|
Lock(channel);
|
|
|
|
putword(channel, Long_val(w));
|
|
|
|
Unlock(channel);
|
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value caml_output_partial(value vchannel, value buff, value start, value length)
|
1997-08-29 08:37:22 -07:00
|
|
|
{
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLparam4 (vchannel, buff, start, length);
|
1997-08-29 08:37:22 -07:00
|
|
|
struct channel * channel = Channel(vchannel);
|
|
|
|
int res;
|
1999-11-29 11:03:05 -08:00
|
|
|
|
|
|
|
Lock(channel);
|
|
|
|
res = putblock(channel, &Byte(buff, Long_val(start)), Long_val(length));
|
|
|
|
Unlock(channel);
|
|
|
|
CAMLreturn (Val_int(res));
|
1997-08-29 08:37:22 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value caml_output(value vchannel, value buff, value start, value length)
|
1997-08-29 08:37:22 -07:00
|
|
|
{
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLparam4 (vchannel, buff, start, length);
|
1997-08-29 08:37:22 -07:00
|
|
|
struct channel * channel = Channel(vchannel);
|
|
|
|
long pos = Long_val(start);
|
|
|
|
long len = Long_val(length);
|
|
|
|
|
1999-11-29 11:03:05 -08:00
|
|
|
Lock(channel);
|
2000-01-07 08:05:19 -08:00
|
|
|
while (len > 0) {
|
|
|
|
int written = putblock(channel, &Byte(buff, pos), len);
|
|
|
|
pos += written;
|
|
|
|
len -= written;
|
|
|
|
}
|
1999-11-29 11:03:05 -08:00
|
|
|
Unlock(channel);
|
|
|
|
CAMLreturn (Val_unit);
|
1997-08-29 08:37:22 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value caml_seek_out(value vchannel, value pos)
|
1997-08-29 08:37:22 -07:00
|
|
|
{
|
|
|
|
struct channel * channel = Channel(vchannel);
|
|
|
|
Lock(channel);
|
|
|
|
seek_out(channel, Long_val(pos));
|
|
|
|
Unlock(channel);
|
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value caml_pos_out(value vchannel)
|
1997-08-29 08:37:22 -07:00
|
|
|
{
|
|
|
|
return Val_long(pos_out(Channel(vchannel)));
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value caml_input_char(value vchannel)
|
1997-08-29 08:37:22 -07:00
|
|
|
{
|
|
|
|
struct channel * channel = Channel(vchannel);
|
|
|
|
unsigned char c;
|
|
|
|
|
|
|
|
Lock(channel);
|
|
|
|
c = getch(channel);
|
|
|
|
Unlock(channel);
|
|
|
|
return Val_long(c);
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value caml_input_int(value vchannel)
|
1997-08-29 08:37:22 -07:00
|
|
|
{
|
|
|
|
struct channel * channel = Channel(vchannel);
|
|
|
|
long i;
|
|
|
|
|
|
|
|
Lock(channel);
|
|
|
|
i = getword(channel);
|
|
|
|
Unlock(channel);
|
|
|
|
#ifdef ARCH_SIXTYFOUR
|
|
|
|
i = (i << 32) >> 32; /* Force sign extension */
|
|
|
|
#endif
|
|
|
|
return Val_long(i);
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value caml_input(value vchannel,value buff,value vstart,value vlength)
|
1997-08-29 08:37:22 -07:00
|
|
|
{
|
2000-02-07 06:07:31 -08:00
|
|
|
CAMLparam4 (vchannel, buff, vstart, vlength);
|
1997-08-29 08:37:22 -07:00
|
|
|
struct channel * channel = Channel(vchannel);
|
2000-02-07 06:07:31 -08:00
|
|
|
long start, len;
|
|
|
|
int n, avail, nread;
|
1997-08-29 08:37:22 -07:00
|
|
|
|
1999-11-29 11:03:05 -08:00
|
|
|
Lock(channel);
|
2000-02-07 06:07:31 -08:00
|
|
|
/* We cannot call getblock here because buff may move during do_read */
|
|
|
|
start = Long_val(vstart);
|
|
|
|
len = Long_val(vlength);
|
|
|
|
n = len >= INT_MAX ? INT_MAX : (int) len;
|
|
|
|
avail = channel->max - channel->curr;
|
|
|
|
if (n <= avail) {
|
2000-10-12 11:05:42 -07:00
|
|
|
memmove(&Byte(buff, start), channel->curr, n);
|
2000-02-07 06:07:31 -08:00
|
|
|
channel->curr += n;
|
|
|
|
} else if (avail > 0) {
|
2000-10-12 11:05:42 -07:00
|
|
|
memmove(&Byte(buff, start), channel->curr, avail);
|
2000-02-07 06:07:31 -08:00
|
|
|
channel->curr += avail;
|
|
|
|
n = avail;
|
|
|
|
} else {
|
|
|
|
nread = do_read(channel->fd, channel->buff, IO_BUFFER_SIZE);
|
|
|
|
channel->offset += nread;
|
|
|
|
channel->max = channel->buff + nread;
|
|
|
|
if (n > nread) n = nread;
|
2000-10-12 11:05:42 -07:00
|
|
|
memmove(&Byte(buff, start), channel->buff, n);
|
2000-02-07 06:07:31 -08:00
|
|
|
channel->curr = channel->buff + n;
|
|
|
|
}
|
1999-11-29 11:03:05 -08:00
|
|
|
Unlock(channel);
|
2000-02-07 06:07:31 -08:00
|
|
|
CAMLreturn (Val_long(n));
|
1997-08-29 08:37:22 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value caml_seek_in(value vchannel, value pos)
|
1997-08-29 08:37:22 -07:00
|
|
|
{
|
|
|
|
struct channel * channel = Channel(vchannel);
|
|
|
|
Lock(channel);
|
|
|
|
seek_in(channel, Long_val(pos));
|
|
|
|
Unlock(channel);
|
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value caml_pos_in(value vchannel)
|
1997-08-29 08:37:22 -07:00
|
|
|
{
|
|
|
|
return Val_long(pos_in(Channel(vchannel)));
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value caml_input_scan_line(value vchannel)
|
1997-08-29 08:37:22 -07:00
|
|
|
{
|
|
|
|
struct channel * channel = Channel(vchannel);
|
|
|
|
long res;
|
|
|
|
|
|
|
|
Lock(channel);
|
|
|
|
res = input_scan_line(channel);
|
|
|
|
Unlock(channel);
|
|
|
|
return Val_long(res);
|
|
|
|
}
|