add standard io ports

This commit is contained in:
Yuichi Nishiwaki 2014-01-12 23:57:50 +09:00
parent f4202a7555
commit 0af0ecafb6
1 changed files with 16 additions and 0 deletions

View File

@ -155,6 +155,18 @@ pic_debug(pic_state *pic, pic_value obj)
write(pic, obj);
}
static pic_value
make_port_from_fp(pic_state *pic, FILE *fp, short flags)
{
struct pic_port *port;
port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TT_PORT);
port->file = fp;
port->flags = flags;
port->status = PIC_PORT_OPEN;
return pic_obj_value(port);
}
static pic_value
pic_port_input_port_p(pic_state *pic)
{
@ -327,6 +339,10 @@ pic_port_close_port(pic_state *pic)
void
pic_init_port(pic_state *pic)
{
pic_defvar(pic, "current-input-port", make_port_from_fp(pic, stdin, PIC_PORT_IN | PIC_PORT_TEXT));
pic_defvar(pic, "current-output-port", make_port_from_fp(pic, stdout, PIC_PORT_OUT | PIC_PORT_TEXT));
pic_defvar(pic, "current-error-port", make_port_from_fp(pic, stderr, PIC_PORT_OUT | PIC_PORT_TEXT));
pic_defun(pic, "input-port?", pic_port_input_port_p);
pic_defun(pic, "output-port?", pic_port_output_port_p);
pic_defun(pic, "textual-port?", pic_port_textual_port_p);