5 /* Simple wrappers for ANSI C I/O functions, used for bootstrapping.
7 Note the ugly loop logic in almost every function; we have to handle EINTR
8 and restart the operation if the system call was interrupted. Naive
9 applications don't do this, but then they quickly fail if one enables
10 itimer()s or other signals.
12 The Factor library provides platform-specific code for Unix and Windows
13 with many more capabilities so these words are not usually used in
16 void factor_vm::init_c_io() {
17 special_objects[OBJ_STDIN] = allot_alien(false_object, (cell)stdin);
18 special_objects[OBJ_STDOUT] = allot_alien(false_object, (cell)stdout);
19 special_objects[OBJ_STDERR] = allot_alien(false_object, (cell)stderr);
22 void factor_vm::io_error() {
26 general_error(ERROR_IO, tag_fixnum(errno), false_object);
29 FILE* factor_vm::safe_fopen(char* filename, char* mode) {
32 file = fopen(filename, mode);
41 int factor_vm::safe_fgetc(FILE* stream) {
56 size_t factor_vm::safe_fread(void* ptr, size_t size, size_t nitems,
58 size_t items_read = 0;
62 ret = fread((void*)((int*)ptr + items_read * size), size,
63 nitems - items_read, stream);
71 } while (items_read != nitems);
76 void factor_vm::safe_fputc(int c, FILE* stream) {
78 if (putc(c, stream) == EOF)
85 size_t factor_vm::safe_fwrite(void* ptr, size_t size, size_t nitems,
87 size_t items_written = 0;
91 ret = fwrite((void*)((int*)ptr + items_written * size), size,
92 nitems - items_written, stream);
96 } while (items_written != nitems);
101 int factor_vm::safe_ftell(FILE* stream) {
104 if ((offset = FTELL(stream)) == -1)
112 void factor_vm::safe_fseek(FILE* stream, off_t offset, int whence) {
124 critical_error("Bad value for whence", whence);
128 if (FSEEK(stream, offset, whence) == -1)
135 void factor_vm::safe_fflush(FILE* stream) {
137 if (fflush(stream) == EOF)
144 void factor_vm::safe_fclose(FILE* stream) {
146 if (fclose(stream) == EOF)
153 void factor_vm::primitive_fopen() {
154 data_root<byte_array> mode(ctx->pop(), this);
155 data_root<byte_array> path(ctx->pop(), this);
156 mode.untag_check(this);
157 path.untag_check(this);
160 file = safe_fopen((char*)(path.untagged() + 1), (char*)(mode.untagged() + 1));
161 ctx->push(allot_alien(file));
164 FILE* factor_vm::pop_file_handle() { return (FILE*)alien_offset(ctx->pop()); }
166 FILE* factor_vm::peek_file_handle() { return (FILE*)alien_offset(ctx->peek()); }
168 void factor_vm::primitive_fgetc() {
169 FILE* file = peek_file_handle();
171 int c = safe_fgetc(file);
172 if (c == EOF && feof(file)) {
174 ctx->replace(false_object);
176 ctx->replace(tag_fixnum(c));
179 /* Allocates memory */
180 void factor_vm::primitive_fread() {
181 FILE* file = pop_file_handle();
182 void* buf = (void*)alien_offset(ctx->pop());
183 fixnum size = unbox_array_size();
186 ctx->push(from_unsigned_cell(0));
190 size_t c = safe_fread(buf, 1, size, file);
191 if (c == 0 || feof(file))
193 ctx->push(from_unsigned_cell(c));
196 void factor_vm::primitive_fputc() {
197 FILE* file = pop_file_handle();
198 fixnum ch = to_fixnum(ctx->pop());
199 safe_fputc((int)ch, file);
202 void factor_vm::primitive_fwrite() {
203 FILE* file = pop_file_handle();
204 cell length = to_cell(ctx->pop());
205 char* text = alien_offset(ctx->pop());
210 size_t written = safe_fwrite(text, 1, length, file);
211 if (written != length)
215 void factor_vm::primitive_ftell() {
216 FILE* file = peek_file_handle();
217 ctx->replace(from_signed_8(safe_ftell(file)));
220 void factor_vm::primitive_fseek() {
221 FILE* file = pop_file_handle();
222 int whence = (int)to_fixnum(ctx->pop());
223 off_t offset = (off_t)to_signed_8(ctx->pop());
224 safe_fseek(file, offset, whence);
227 void factor_vm::primitive_fflush() {
228 FILE* file = pop_file_handle();
232 void factor_vm::primitive_fclose() {
233 FILE* file = pop_file_handle();
237 /* This function is used by FFI I/O. Accessing the errno global directly is
238 not portable, since on some libc's errno is not a global but a funky macro that
239 reads thread-local storage. */
240 VM_C_API int err_no() { return errno; }
242 VM_C_API void set_err_no(int err) { errno = err; }