From 44b44a751d07b42b4546e3d856a6e761f46dbf63 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Sun, 14 Aug 2011 23:26:09 +0200 Subject: telnet: allow programatic use on Windows Summary of the issue can be found at: http://curl.haxx.se/mail/lib-2010-04/0367.html That patch only updated the unix sockets code - the winsock code was not updated and no longer works the same was as the unix code. This change updates the windows code accordingly. Bug: http://curl.haxx.se/bug/view.cgi?id=3163118 --- lib/telnet.c | 60 +++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 19 deletions(-) (limited to 'lib') diff --git a/lib/telnet.c b/lib/telnet.c index 91725a062..ebd47ebae 100644 --- a/lib/telnet.c +++ b/lib/telnet.c @@ -1227,13 +1227,6 @@ static CURLcode telnet_do(struct connectdata *conn, bool *done) return CURLE_FAILED_INIT; } - /* The get the Windows file handle for stdin */ - stdin_handle = GetStdHandle(STD_INPUT_HANDLE); - - /* Create the list of objects to wait for */ - objs[0] = event_handle; - objs[1] = stdin_handle; - /* Tell winsock what events we want to listen to */ if(event_select_func(sockfd, event_handle, FD_READ|FD_CLOSE) == SOCKET_ERROR) { @@ -1242,9 +1235,17 @@ static CURLcode telnet_do(struct connectdata *conn, bool *done) return CURLE_OK; } + /* The get the Windows file handle for stdin */ + stdin_handle = GetStdHandle(STD_INPUT_HANDLE); + + /* Create the list of objects to wait for */ + objs[0] = event_handle; + objs[1] = stdin_handle; + /* If stdin_handle is a pipe, use PeekNamedPipe() method to check it, else use the old WaitForMultipleObjects() way */ - if(GetFileType(stdin_handle) == FILE_TYPE_PIPE) { + if(GetFileType(stdin_handle) == FILE_TYPE_PIPE || + data->set.is_fread_set) { /* Don't wait for stdin_handle, just wait for event_handle */ obj_count = 1; /* Check stdin_handle per 100 milliseconds */ @@ -1262,20 +1263,41 @@ static CURLcode telnet_do(struct connectdata *conn, bool *done) case WAIT_TIMEOUT: { for(;;) { - if(!PeekNamedPipe(stdin_handle, NULL, 0, NULL, &readfile_read, NULL)) { - keepon = FALSE; - code = CURLE_READ_ERROR; - break; + if(obj_count == 1) { + /* read from user-supplied method */ + code = (int)conn->fread_func(buf, 1, BUFSIZE - 1, conn->fread_in); + if(code == CURL_READFUNC_ABORT) { + keepon = FALSE; + code = CURLE_READ_ERROR; + break; + } + + if(code == CURL_READFUNC_PAUSE) + break; + + if(code == 0) /* no bytes */ + break; + + readfile_read = code; /* fall thru with number of bytes read */ } + else { + /* read from stdin */ + if(!PeekNamedPipe(stdin_handle, NULL, 0, NULL, + &readfile_read, NULL)) { + keepon = FALSE; + code = CURLE_READ_ERROR; + break; + } - if(!readfile_read) - break; + if(!readfile_read) + break; - if(!ReadFile(stdin_handle, buf, sizeof(data->state.buffer), - &readfile_read, NULL)) { - keepon = FALSE; - code = CURLE_READ_ERROR; - break; + if(!ReadFile(stdin_handle, buf, sizeof(data->state.buffer), + &readfile_read, NULL)) { + keepon = FALSE; + code = CURLE_READ_ERROR; + break; + } } code = send_telnet_data(conn, buf, readfile_read); -- cgit v1.2.3