5 env.user[STDIN_ENV] = tag_object(port(0));
6 env.user[STDOUT_ENV] = tag_object(port(1));
7 env.user[STDERR_ENV] = tag_object(port(2));
10 bool can_read_line(PORT* port)
15 void primitive_can_read_line(void)
17 PORT* port = untag_port(dpop());
18 dpush(tag_boolean(can_read_line(port)));
21 /* Return true if something was read */
22 bool read_step(PORT* port)
24 FIXNUM amount = read(port->fd,
26 port->buffer->capacity * 2);
31 io_error(port,__FUNCTION__);
36 port->buf_fill = (amount < 0 ? 0 : amount);
42 bool read_line_step(PORT* port)
47 SBUF* line = untag_sbuf(port->line);
49 for(i = port->buf_pos; i < port->buf_fill; i++)
51 ch = bget((CELL)port->buffer + sizeof(STRING) + i);
54 port->buf_pos = i + 1;
58 set_sbuf_nth(line,line->top,ch);
61 port->buf_pos = port->buf_fill;
63 /* We've reached the end of the above loop, without seeing a newline
64 or EOF, so read again */
68 void primitive_read_line_fd_8(void)
70 PORT* port = untag_port(dpeek());
76 /* Return true if write was done */
77 bool write_step(PORT* port)
79 char* chars = (char*)port->buffer + sizeof(STRING);
81 FIXNUM amount = write(port->fd,chars + port->buf_pos,
82 port->buf_fill - port->buf_pos);
87 io_error(port,__FUNCTION__);
92 port->buf_pos += amount;
97 bool can_write(PORT* port, FIXNUM len)
101 switch(port->buf_mode)
108 buf_capacity = port->buffer->capacity * CHARS;
109 /* Is the string longer than the buffer? */
110 if(port->buf_fill == 0 && len > buf_capacity)
112 /* Increase the buffer to fit the string */
113 port->buffer = allot_string(len / CHARS + 1);
117 return (port->buf_fill + len <= buf_capacity);
119 critical_error("Bad buf_mode",port->buf_mode);
124 void primitive_can_write(void)
126 PORT* port = untag_port(dpop());
127 FIXNUM len = to_fixnum(dpop());
128 dpush(tag_boolean(can_write(port,len)));
131 void write_fd_char_8(PORT* port, FIXNUM ch)
135 if(!can_write(port,1))
136 io_error(port,__FUNCTION__);
138 init_buffer(port,B_WRITE);
140 bput((CELL)port->buffer + sizeof(STRING) + port->buf_fill,c);
144 void write_fd_string_8(PORT* port, STRING* str)
148 /* Note this ensures the buffer is large enough to fit the string */
149 if(!can_write(port,str->capacity))
150 io_error(port,__FUNCTION__);
152 init_buffer(port,B_WRITE);
154 c_str = to_c_string(str);
156 /* Append string to buffer */
157 memcpy((void*)((CELL)port->buffer + sizeof(STRING)
158 + port->buf_fill),c_str,str->capacity);
160 port->buf_fill += str->capacity;
163 void primitive_write_fd_8(void)
165 PORT* port = untag_port(dpop());
168 CELL type = type_of(text);
174 write_fd_char_8(port,to_fixnum(text));
177 write_fd_string_8(port,untag_string(text));
180 type_error(STRING_TYPE,text);
185 void primitive_close_fd(void)
187 /* This does not flush. */
188 PORT* port = untag_port(dpop());
192 void io_error(PORT* port, const char* func)
194 STRING* function = from_c_string(func);
195 STRING* error = from_c_string(strerror(errno));
197 CONS* c = cons(tag_object(function),tag_cons(
198 cons(tag_object(error),F)));
200 general_error(ERROR_IO,tag_cons(c));