From c88fb98ef8406df4d92450f4d42cd686287118fd Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 11 Dec 2004 03:21:08 +0000 Subject: [PATCH] io.c merged --- native/port.c | 28 ++++++++++++++++++++++--- native/port.h | 4 ++++ native/{ => unix}/io.c | 2 +- native/win32/io.c | 46 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 4 deletions(-) rename native/{ => unix}/io.c (99%) create mode 100644 native/win32/io.c diff --git a/native/port.c b/native/port.c index 0c871b194d..7ead04f0f7 100644 --- a/native/port.c +++ b/native/port.c @@ -8,8 +8,6 @@ F_PORT* untag_port(CELL tagged) /* after image load & save, ports are no longer valid */ if(p->fd == -1) general_error(ERROR_EXPIRED,tagged); - /* if(p->closed) - general_error(ERROR_CLOSED,tagged); */ return p; } @@ -47,7 +45,7 @@ void init_line_buffer(F_PORT* port, F_FIXNUM count) void fixup_port(F_PORT* port) { - port->fd = -1; + port->fd = (F_FIXNUM)INVALID_HANDLE_VALUE; fixup(&port->buffer); fixup(&port->line); fixup(&port->client_host); @@ -64,6 +62,29 @@ void collect_port(F_PORT* port) copy_object(&port->io_error); } +#ifdef WIN32 +CELL make_io_error(const char* func) +{ + char *buffer; + F_STRING *function = from_c_string(func); + F_STRING *error; + DWORD dw = GetLastError(); + + FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM, + NULL, + dw, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR) &buffer, + 0, NULL); + + error = from_c_string(buffer); + LocalFree(buffer); + + return cons(tag_object(function),cons(tag_object(error),F)); +} +#else CELL make_io_error(const char* func) { F_STRING* function = from_c_string(func); @@ -71,6 +92,7 @@ CELL make_io_error(const char* func) return cons(tag_object(function),cons(tag_object(error),F)); } +#endif void postpone_io_error(F_PORT* port, const char* func) { diff --git a/native/port.h b/native/port.h index b398f7c69b..8bb8e378ad 100644 --- a/native/port.h +++ b/native/port.h @@ -1,5 +1,9 @@ #define BUF_SIZE (8 * 1024) +#ifndef WIN32 +#define INVALID_HANDLE_VALUE -1 +#endif + typedef enum { PORT_READ, PORT_RECV, diff --git a/native/io.c b/native/unix/io.c similarity index 99% rename from native/io.c rename to native/unix/io.c index 584616323b..894f52afff 100644 --- a/native/io.c +++ b/native/unix/io.c @@ -1,4 +1,4 @@ -#include "factor.h" +#include "../factor.h" void init_io_tasks(fd_set* fdset, IO_TASK* io_tasks) { diff --git a/native/win32/io.c b/native/win32/io.c new file mode 100644 index 0000000000..4872653562 --- /dev/null +++ b/native/win32/io.c @@ -0,0 +1,46 @@ +#include "../factor.h" + +HANDLE completion_port = INVALID_HANDLE_VALUE; +CELL callback_list = F; + +void init_io (void) +{ + completion_port = CreateIoCompletionPort( + INVALID_HANDLE_VALUE, NULL, 0, 1); + + if (completion_port == INVALID_HANDLE_VALUE) + io_error(__FUNCTION__); + + userenv[STDIN_ENV] = tag_object(port(PORT_READ, (CELL)GetStdHandle(STD_INPUT_HANDLE))); + userenv[STDOUT_ENV] = tag_object(port(PORT_WRITE, (CELL)GetStdHandle(STD_OUTPUT_HANDLE))); +} + +void primitive_add_copy_io_task (void) +{ + io_error(__FUNCTION__); +} + +void primitive_close (void) +{ + PORT *port = untag_port(dpop()); + CloseHandle((HANDLE)port->fd); + port->closed = true; +} + +void primitive_next_io_task (void) +{ + if (callback_list != F) + { + CONS *cons = untag_cons(callback_list); + CELL car = cons->car; + callback_list = cons->cdr; + dpush(car); + } + else + dpush(F); +} + +void collect_io_tasks (void) +{ + copy_object(&callback_list); +} \ No newline at end of file