]> gitweb.factorcode.org Git - factor.git/blob - native/fd.c
working on native image output
[factor.git] / native / fd.c
1 #include "factor.h"
2
3 void init_io(void)
4 {
5         env.user[STDIN_ENV]  = handle(HANDLE_FD,0);
6         env.user[STDOUT_ENV] = handle(HANDLE_FD,1);
7         env.user[STDERR_ENV] = handle(HANDLE_FD,2);
8 }
9
10 void primitive_close_fd(void)
11 {
12         HANDLE* h = untag_handle(HANDLE_FD,env.dt);
13         close(h->object);
14         env.dt = dpop();
15 }
16
17 int fill_buffer(HANDLE* h, int fd, STRING* buf)
18 {
19         int amount = read(fd,buf + 1,buf->capacity * 2);
20
21         h->buf_fill = (amount < 0 ? 0 : amount);
22         h->buf_pos = 0;
23
24         return amount;
25 }
26
27 void primitive_read_line_fd_8(void)
28 {
29         HANDLE* h = untag_handle(HANDLE_FD,env.dt);
30         int fd = h->object;
31
32         int amount;
33         int i;
34         int ch;
35
36         /* finished line, unicode */
37         SBUF* line = sbuf(LINE_SIZE);
38
39         /* read ascii from fd */
40         STRING* buf;
41         if(h->buf_mode != B_READ)
42         {
43                 h->buf_mode = B_READ;
44                 h->buffer = tag_object(string(BUF_SIZE,'\0'));
45         }
46         buf = untag_string(h->buffer);
47
48         for(;;)
49         {
50                 if(h->buf_pos >= h->buf_fill)
51                 {
52                         amount = fill_buffer(h,fd,buf);
53
54                         if(amount < 0)
55                                 io_error(__FUNCTION__);
56
57                         if(amount == 0)
58                         {
59                                 if(line->top == 0)
60                                 {
61                                         /* didn't read anything before EOF */
62                                         env.dt = F;
63                                 }
64                                 else
65                                         env.dt = tag_object(line);
66                                 return;
67                         }
68                 }
69
70                 for(i = h->buf_pos; i < h->buf_fill; i++)
71                 {
72                         ch = bget((CELL)buf + sizeof(STRING) + i);
73                         if(ch == '\n')
74                         {
75                                 h->buf_pos = i + 1;
76                                 env.dt = tag_object(line);
77                                 return;
78                         }
79                         else
80                                 set_sbuf_nth(line,line->top,ch);
81                 }
82                 
83                 /* We've reached the end of the above loop */
84                 h->buf_pos = h->buf_fill;
85         }
86 }
87
88 void write_fd_char_8(HANDLE* h, FIXNUM ch)
89 {
90         BYTE c = (BYTE)ch;
91
92         int amount = write(h->object,&c,1);
93
94         if(amount < 0)
95                 io_error(__FUNCTION__);
96 }
97
98 void write_fd_string_8(HANDLE* h, STRING* str)
99 {
100         char* c_str = to_c_string(str);
101         
102         int amount = write(h->object,c_str,str->capacity);
103         
104         if(amount < 0)
105                 io_error(__FUNCTION__);
106 }
107
108 void primitive_write_fd_8(void)
109 {
110         HANDLE* h = untag_handle(HANDLE_FD,env.dt);
111
112         CELL text = dpop();
113         CELL type = type_of(text);
114
115         switch(type)
116         {
117         case FIXNUM_TYPE:
118                 write_fd_char_8(h,to_fixnum(text));
119                 break;
120         case STRING_TYPE:
121                 write_fd_string_8(h,untag_string(text));
122                 break;
123         default:
124                 type_error(STRING_TYPE,text);
125                 break;
126         }
127
128         env.dt = dpop();
129 }
130
131 void primitive_flush_fd(void)
132 {
133         HANDLE* h = untag_handle(HANDLE_FD,env.dt);
134
135         if(h->buf_mode == B_WRITE)
136         {
137                 
138         }
139
140         /* int fd = h->object;
141
142         if(fsync(fd) < 0)
143                 io_error(__FUNCTION__); */
144
145         env.dt = dpop();
146 }
147
148 void primitive_shutdown_fd(void)
149 {
150         /* HANDLE* h = untag_handle(HANDLE_FD,env.dt);
151         int fd = h->object;
152
153         if(shutdown(fd,SHUT_RDWR) < 0)
154                 io_error(__FUNCTION__); */
155
156         env.dt = dpop();
157 }