2004-08-12 17:36:36 -04:00
|
|
|
#include "factor.h"
|
|
|
|
|
|
|
|
PORT* untag_port(CELL tagged)
|
|
|
|
{
|
|
|
|
PORT* p;
|
|
|
|
type_check(PORT_TYPE,tagged);
|
|
|
|
p = (PORT*)UNTAG(tagged);
|
|
|
|
/* after image load & save, ports are no longer valid */
|
|
|
|
if(p->fd == -1)
|
|
|
|
general_error(ERROR_PORT_EXPIRED,tagged);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2004-08-16 19:29:07 -04:00
|
|
|
PORT* port(PORT_MODE type, CELL fd)
|
2004-08-12 17:36:36 -04:00
|
|
|
{
|
|
|
|
PORT* port = allot_object(PORT_TYPE,sizeof(PORT));
|
2004-08-16 19:29:07 -04:00
|
|
|
port->type = type;
|
2004-08-12 17:36:36 -04:00
|
|
|
port->fd = fd;
|
2004-08-12 23:40:28 -04:00
|
|
|
port->buffer = NULL;
|
2004-08-15 21:50:44 -04:00
|
|
|
port->line = F;
|
2004-08-15 22:45:08 -04:00
|
|
|
port->client_host = F;
|
|
|
|
port->client_port = F;
|
|
|
|
port->client_socket = F;
|
2004-08-16 19:29:07 -04:00
|
|
|
port->line = F;
|
2004-08-16 20:42:30 -04:00
|
|
|
port->line_ready = false;
|
2004-08-12 17:36:36 -04:00
|
|
|
port->buf_fill = 0;
|
|
|
|
port->buf_pos = 0;
|
2004-08-20 21:16:47 -04:00
|
|
|
port->io_error = F;
|
2004-08-15 21:50:44 -04:00
|
|
|
|
2004-08-16 19:29:07 -04:00
|
|
|
if(type == PORT_SPECIAL)
|
|
|
|
port->buffer = NULL;
|
|
|
|
else
|
|
|
|
port->buffer = string(BUF_SIZE,'\0');
|
|
|
|
|
2004-08-15 21:50:44 -04:00
|
|
|
if(fcntl(port->fd,F_SETFL,O_NONBLOCK,1) == -1)
|
2004-08-18 15:23:42 -04:00
|
|
|
io_error(__FUNCTION__);
|
2004-08-15 21:50:44 -04:00
|
|
|
|
|
|
|
return port;
|
2004-08-12 17:36:36 -04:00
|
|
|
}
|
|
|
|
|
2004-08-20 01:49:14 -04:00
|
|
|
void init_line_buffer(PORT* port, FIXNUM count)
|
|
|
|
{
|
|
|
|
if(port->line == F)
|
|
|
|
port->line = tag_object(sbuf(LINE_SIZE));
|
|
|
|
}
|
|
|
|
|
2004-08-12 17:36:36 -04:00
|
|
|
void primitive_portp(void)
|
|
|
|
{
|
|
|
|
drepl(tag_boolean(typep(PORT_TYPE,dpeek())));
|
|
|
|
}
|
|
|
|
|
|
|
|
void fixup_port(PORT* port)
|
|
|
|
{
|
|
|
|
port->fd = -1;
|
|
|
|
if(port->buffer != 0)
|
2004-08-12 23:40:28 -04:00
|
|
|
port->buffer = fixup_untagged_string(port->buffer);
|
2004-08-15 21:50:44 -04:00
|
|
|
fixup(&port->line);
|
2004-08-15 22:45:08 -04:00
|
|
|
fixup(&port->client_host);
|
|
|
|
fixup(&port->client_port);
|
2004-08-20 21:16:47 -04:00
|
|
|
fixup(&port->io_error);
|
2004-08-12 17:36:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void collect_port(PORT* port)
|
|
|
|
{
|
|
|
|
if(port->buffer != 0)
|
2004-08-12 23:40:28 -04:00
|
|
|
port->buffer = copy_untagged_string(port->buffer);
|
2004-08-15 21:50:44 -04:00
|
|
|
copy_object(&port->line);
|
2004-08-15 22:45:08 -04:00
|
|
|
copy_object(&port->client_host);
|
|
|
|
copy_object(&port->client_port);
|
2004-08-20 21:16:47 -04:00
|
|
|
copy_object(&port->io_error);
|
|
|
|
}
|
|
|
|
|
|
|
|
CELL make_io_error(const char* func)
|
|
|
|
{
|
|
|
|
STRING* function = from_c_string(func);
|
|
|
|
STRING* error = from_c_string(strerror(errno));
|
|
|
|
|
|
|
|
CONS* c = cons(tag_object(function),tag_cons(
|
|
|
|
cons(tag_object(error),F)));
|
|
|
|
|
|
|
|
return tag_cons(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void postpone_io_error(PORT* port, const char* func)
|
|
|
|
{
|
|
|
|
port->io_error = make_io_error(func);
|
|
|
|
}
|
|
|
|
|
|
|
|
void io_error(const char* func)
|
|
|
|
{
|
|
|
|
general_error(ERROR_IO,make_io_error(func));
|
|
|
|
}
|
|
|
|
|
|
|
|
void pending_io_error(PORT* port)
|
|
|
|
{
|
|
|
|
CELL io_error = port->io_error;
|
|
|
|
if(io_error != F)
|
|
|
|
{
|
|
|
|
port->io_error = F;
|
|
|
|
general_error(ERROR_IO,io_error);
|
|
|
|
}
|
2004-08-12 17:36:36 -04:00
|
|
|
}
|
2004-09-02 21:51:19 -04:00
|
|
|
|
|
|
|
void primitive_pending_io_error(void)
|
|
|
|
{
|
|
|
|
pending_io_error(untag_port(dpop()));
|
|
|
|
}
|