2005-04-25 21:31:17 -04:00
|
|
|
#include "factor.h"
|
|
|
|
|
2005-04-29 02:36:32 -04:00
|
|
|
/* This function is used by FFI I/O. Accessing the errno global is
|
|
|
|
too troublesome... on some libc's its a funky macro that reads
|
|
|
|
thread-local storage. */
|
2005-06-13 01:42:16 -04:00
|
|
|
int err_no(void)
|
2005-04-29 02:36:32 -04:00
|
|
|
{
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
2005-04-25 21:31:17 -04:00
|
|
|
/* Simple wrappers for ANSI C I/O functions, used for bootstrapping.
|
|
|
|
The Factor library provides platform-specific code for Unix and Windows
|
|
|
|
with many more capabilities.
|
|
|
|
|
|
|
|
Note that c-streams are pretty limited and broken. Namely,
|
|
|
|
there is a limit of 1024 characters per line, and lines containing
|
|
|
|
\0 are not read fully.
|
|
|
|
|
|
|
|
The native FFI streams in the library don't have this limitation. */
|
|
|
|
|
|
|
|
void init_c_io(void)
|
|
|
|
{
|
2006-02-19 20:53:18 -05:00
|
|
|
userenv[IN_ENV] = tag_object(make_alien(F,(CELL)stdin));
|
|
|
|
userenv[OUT_ENV] = tag_object(make_alien(F,(CELL)stdout));
|
2005-04-25 21:31:17 -04:00
|
|
|
}
|
|
|
|
|
2005-05-01 14:30:53 -04:00
|
|
|
void io_error(void)
|
2005-04-25 21:31:17 -04:00
|
|
|
{
|
|
|
|
CELL error = tag_object(from_c_string(strerror(errno)));
|
2006-02-07 19:09:46 -05:00
|
|
|
general_error(ERROR_IO,error,true);
|
2005-04-25 21:31:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_fopen(void)
|
|
|
|
{
|
|
|
|
char *path, *mode;
|
|
|
|
FILE* file;
|
2005-06-16 18:50:49 -04:00
|
|
|
maybe_gc(0);
|
2006-02-19 20:53:18 -05:00
|
|
|
mode = pop_c_string();
|
|
|
|
path = pop_c_string();
|
2005-04-25 21:31:17 -04:00
|
|
|
file = fopen(path,mode);
|
|
|
|
if(file == NULL)
|
2005-05-01 14:30:53 -04:00
|
|
|
io_error();
|
2006-02-19 20:53:18 -05:00
|
|
|
box_alien((CELL)file);
|
2005-04-25 21:31:17 -04:00
|
|
|
}
|
|
|
|
|
2005-06-19 00:23:01 -04:00
|
|
|
void primitive_fgetc(void)
|
|
|
|
{
|
|
|
|
FILE* file = (FILE*)unbox_alien();
|
|
|
|
int c = fgetc(file);
|
|
|
|
if(c == EOF)
|
|
|
|
dpush(F);
|
|
|
|
else
|
|
|
|
dpush(tag_fixnum(c));
|
|
|
|
}
|
|
|
|
|
2005-04-25 21:31:17 -04:00
|
|
|
void primitive_fwrite(void)
|
|
|
|
{
|
|
|
|
FILE* file;
|
|
|
|
F_STRING* text;
|
2005-06-16 18:50:49 -04:00
|
|
|
maybe_gc(0);
|
2005-04-25 21:31:17 -04:00
|
|
|
file = (FILE*)unbox_alien();
|
|
|
|
text = untag_string(dpop());
|
2005-07-26 16:39:14 -04:00
|
|
|
|
|
|
|
if(string_capacity(text) == 0)
|
|
|
|
return;
|
|
|
|
|
2005-12-12 18:51:45 -05:00
|
|
|
if(fwrite(to_c_string(text,false),1,
|
2005-04-25 21:31:17 -04:00
|
|
|
untag_fixnum_fast(text->length),
|
|
|
|
file) == 0)
|
2005-05-01 14:30:53 -04:00
|
|
|
io_error();
|
2005-04-25 21:31:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_fflush(void)
|
|
|
|
{
|
|
|
|
FILE* file = (FILE*)unbox_alien();
|
|
|
|
fflush(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_fclose(void)
|
|
|
|
{
|
|
|
|
FILE* file = (FILE*)unbox_alien();
|
|
|
|
fclose(file);
|
|
|
|
}
|