Skip to content

Commit fb93bb9

Browse files
committed
term io initial implementation
1 parent a67a8b8 commit fb93bb9

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-10
lines changed

contrib/win32/win32compat/fileio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#define errno_from_Win32LastError() errno_from_Win32Error(GetLastError())
4545

4646
/* maps Win32 error to errno */
47-
static int
47+
int
4848
errno_from_Win32Error(int win32_error)
4949
{
5050
switch (win32_error) {

contrib/win32/win32compat/termio.c

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,58 @@
22
#include "w32fd.h"
33
#include "inc/defs.h"
44

5-
/* Win7 - Read Term Support - START*/
5+
#define TERM_IO_BUF_SIZE 2048
6+
int errno_from_Win32Error(int win32_error);
67

7-
int
8-
fileio_initiateReadTerm_Win7(struct w32_io* pio) {
8+
struct io_status {
9+
char* buf[TERM_IO_BUF_SIZE];
10+
DWORD transferred;
11+
DWORD error;
12+
};
913

10-
if (pio->read_details.pending || w32_io_is_io_available(pio, TRUE)) {
11-
debug("Win7 term read - ERROR - called in wrong state");
12-
errno = EINVAL;
14+
static struct io_status read_status, write_status;
15+
16+
static VOID CALLBACK ReadAPCProc(
17+
_In_ ULONG_PTR dwParam
18+
) {
19+
struct w32_io* pio = (struct w32_io*)dwParam;
20+
pio->read_details.error = read_status.error;
21+
pio->read_details.remaining = read_status.transferred;
22+
pio->read_details.completed = 0;
23+
pio->read_details.pending = FALSE;
24+
}
25+
26+
static DWORD WINAPI ReadThread(
27+
_In_ LPVOID lpParameter
28+
) {
29+
struct w32_io* pio = (struct w32_io*)lpParameter;
30+
memset(&read_status, 0, sizeof(read_status));
31+
if (!ReadFile(WINHANDLE(pio), read_status.buf, TERM_IO_BUF_SIZE, &read_status.transferred, NULL)) {
32+
read_status.error = GetLastError();
33+
}
34+
35+
if (0 == QueueUserAPC(ReadAPCProc, main_thread, pio))
36+
DebugBreak();
37+
}
38+
39+
static int
40+
termio_initiate_read(struct w32_io* pio) {
41+
HANDLE read_thread = CreateThread(NULL, 0, ReadThread, pio, 0, NULL);
42+
if (read_thread == NULL) {
43+
errno = errno_from_Win32Error(GetLastError());
1344
return -1;
1445
}
1546

1647
return 0;
1748
}
1849

19-
/* Win7 - Read Term Support - END*/
20-
2150
int
2251
termio_on_select(struct w32_io* pio, BOOL rd) {
23-
return fileio_on_select(pio, rd);
52+
if (!rd)
53+
return 0;
54+
55+
if ((!fileio_is_io_available(pio, rd)) && (!pio->read_details.pending))
56+
return termio_initiate_read(pio);
2457
}
2558

2659
int

contrib/win32/win32compat/w32fd.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ static struct w32fd_table fd_table;
4848
/* static table entries representing std in, out and error*/
4949
static struct w32_io w32_io_stdin, w32_io_stdout, w32_io_stderr;
5050

51+
/* main thread handle*/
52+
HANDLE main_thread;
53+
5154
void fd_table_set(struct w32_io* pio, int index);
5255

5356
/* initializes mapping table*/
@@ -120,6 +123,7 @@ w32posix_initialize() {
120123
if ((fd_table_initialize() != 0)
121124
|| (socketio_initialize() != 0))
122125
DebugBreak();
126+
main_thread = GetCurrentThread();
123127
signalio_initialize();
124128
}
125129

contrib/win32/win32compat/w32fd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ struct w32_io {
7777

7878
#define WINHANDLE(pio) (((pio)->type == STD_IO_FD)? GetStdHandle((pio)->std_handle):(pio)->handle)
7979
#define FILETYPE(pio) (GetFileType(WINHANDLE(pio)))
80+
extern HANDLE main_thread;
8081

8182
BOOL w32_io_is_blocking(struct w32_io*);
8283
BOOL w32_io_is_io_available(struct w32_io* pio, BOOL rd);

0 commit comments

Comments
 (0)