factor/native/fd.c

180 lines
3.1 KiB
C
Raw Normal View History

#include "factor.h"
2004-07-24 00:54:57 -04:00
void init_io(void)
{
env.user[STDIN_ENV] = port(0);
env.user[STDOUT_ENV] = port(1);
env.user[STDERR_ENV] = port(2);
2004-07-24 00:54:57 -04:00
}
int read_step(PORT* port, STRING* buf)
{
int amount = read(port->fd,buf + 1,buf->capacity * 2);
port->buf_fill = (amount < 0 ? 0 : amount);
port->buf_pos = 0;
2004-07-24 15:11:55 -04:00
return amount;
}
void primitive_read_line_fd_8(void)
{
PORT* port = untag_port(dpeek());
2004-07-24 17:37:42 -04:00
int amount;
2004-07-24 00:54:57 -04:00
int i;
int ch;
/* finished line, unicode */
SBUF* line = sbuf(LINE_SIZE);
init_buffer(port,B_READ);
for(;;)
{
if(port->buf_pos >= port->buf_fill)
{
amount = read_step(port,port->buffer);
2004-07-24 17:37:42 -04:00
if(amount < 0)
io_error(__FUNCTION__);
if(amount == 0)
{
2004-07-24 00:54:57 -04:00
if(line->top == 0)
2004-07-24 15:11:55 -04:00
{
2004-07-24 00:54:57 -04:00
/* didn't read anything before EOF */
drepl(F);
2004-07-24 15:11:55 -04:00
}
else
drepl(tag_object(line));
2004-07-24 00:54:57 -04:00
return;
}
}
for(i = port->buf_pos; i < port->buf_fill; i++)
2004-07-24 00:54:57 -04:00
{
ch = bget((CELL)port->buffer + sizeof(STRING) + i);
2004-07-24 00:54:57 -04:00
if(ch == '\n')
{
port->buf_pos = i + 1;
drepl(tag_object(line));
2004-07-24 00:54:57 -04:00
return;
}
else
set_sbuf_nth(line,line->top,ch);
}
2004-07-24 15:11:55 -04:00
/* We've reached the end of the above loop */
port->buf_pos = port->buf_fill;
2004-07-24 00:54:57 -04:00
}
}
2004-08-11 16:56:48 -04:00
/* keep writing to the stream until everything is written */
void write_fully(PORT* port, char* str, CELL len)
2004-08-11 16:56:48 -04:00
{
FIXNUM amount, written = 0, remains;
for(;;)
{
remains = len - written;
if(remains == 0)
break;
amount = write(port->fd,str + written,remains);
2004-08-11 16:56:48 -04:00
if(amount < 0)
io_error(__FUNCTION__);
written += amount;
}
}
void flush_buffer(PORT* port)
2004-08-11 16:56:48 -04:00
{
if(port->buf_mode != B_WRITE || port->buf_fill == 0)
2004-08-11 16:56:48 -04:00
return;
write_fully(port,(char*)port->buffer + sizeof(STRING),port->buf_fill);
port->buf_fill = 0;
2004-08-11 16:56:48 -04:00
}
void write_fd_char_8(PORT* port, FIXNUM ch)
{
2004-08-11 16:56:48 -04:00
char c = (char)ch;
init_buffer(port,B_WRITE);
2004-08-11 16:56:48 -04:00
/* Is the buffer full? */
if(port->buf_fill == port->buffer->capacity * CHARS)
flush_buffer(port);
2004-08-11 16:56:48 -04:00
bput((CELL)port->buffer + sizeof(STRING) + port->buf_fill,c);
port->buf_fill++;
}
void write_fd_string_8(PORT* port, STRING* str)
{
char* c_str = to_c_string(str);
2004-08-11 16:56:48 -04:00
init_buffer(port,B_WRITE);
2004-08-11 16:56:48 -04:00
/* Is the string longer than the buffer? */
if(str->capacity > port->buffer->capacity * CHARS)
2004-08-11 16:56:48 -04:00
{
/* Just write it immediately */
flush_buffer(port);
write_fully(port,c_str,str->capacity);
2004-08-11 16:56:48 -04:00
}
else
{
/* Is there enough room in the buffer? If not, flush */
if(port->buf_fill + str->capacity
> port->buffer->capacity * CHARS)
{
flush_buffer(port);
}
2004-08-11 16:56:48 -04:00
/* Append string to buffer */
memcpy((void*)((CELL)port->buffer + sizeof(STRING)
+ port->buf_fill),c_str,str->capacity);
2004-08-11 16:56:48 -04:00
port->buf_fill += str->capacity;
2004-08-11 16:56:48 -04:00
}
}
void primitive_write_fd_8(void)
{
PORT* port = untag_port(dpop());
CELL text = dpop();
CELL type = type_of(text);
switch(type)
{
case FIXNUM_TYPE:
2004-07-31 16:11:30 -04:00
case BIGNUM_TYPE:
write_fd_char_8(port,to_fixnum(text));
break;
case STRING_TYPE:
write_fd_string_8(port,untag_string(text));
break;
default:
type_error(STRING_TYPE,text);
break;
}
}
void primitive_flush_fd(void)
{
PORT* port = untag_port(dpop());
flush_buffer(port);
2004-07-24 17:37:42 -04:00
}
2004-08-11 16:56:48 -04:00
void primitive_close_fd(void)
2004-07-24 17:37:42 -04:00
{
PORT* port = untag_port(dpop());
flush_buffer(port);
close(port->fd);
}