Commit | Line | Data |
---|---|---|
0ea3f30e DJ |
1 | /* Serial interface for local (hardwired) serial ports on Windows systems |
2 | ||
61baf725 | 3 | Copyright (C) 2006-2017 Free Software Foundation, Inc. |
0ea3f30e DJ |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
0ea3f30e DJ |
10 | (at your option) any later version. |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
0ea3f30e DJ |
19 | |
20 | #include "defs.h" | |
21 | #include "serial.h" | |
22 | #include "ser-base.h" | |
23 | #include "ser-tcp.h" | |
24 | ||
25 | #include <windows.h> | |
c3e2b812 | 26 | #include <conio.h> |
0ea3f30e DJ |
27 | |
28 | #include <fcntl.h> | |
29 | #include <unistd.h> | |
30 | #include <sys/types.h> | |
31 | ||
efdb2a86 PA |
32 | #include "command.h" |
33 | ||
0ea3f30e DJ |
34 | struct ser_windows_state |
35 | { | |
36 | int in_progress; | |
37 | OVERLAPPED ov; | |
38 | DWORD lastCommMask; | |
39 | HANDLE except_event; | |
40 | }; | |
41 | ||
4af53f97 PM |
42 | /* CancelIo is not available for Windows 95 OS, so we need to use |
43 | LoadLibrary/GetProcAddress to avoid a startup failure. */ | |
44 | #define CancelIo dyn_CancelIo | |
cd78b7a1 PA |
45 | typedef BOOL WINAPI (CancelIo_ftype) (HANDLE); |
46 | static CancelIo_ftype *CancelIo; | |
4af53f97 | 47 | |
0ea3f30e DJ |
48 | /* Open up a real live device for serial I/O. */ |
49 | ||
50 | static int | |
51 | ser_windows_open (struct serial *scb, const char *name) | |
52 | { | |
53 | HANDLE h; | |
54 | struct ser_windows_state *state; | |
55 | COMMTIMEOUTS timeouts; | |
56 | ||
0ea3f30e DJ |
57 | h = CreateFile (name, GENERIC_READ | GENERIC_WRITE, 0, NULL, |
58 | OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); | |
59 | if (h == INVALID_HANDLE_VALUE) | |
60 | { | |
61 | errno = ENOENT; | |
62 | return -1; | |
63 | } | |
64 | ||
1be7fe8d | 65 | scb->fd = _open_osfhandle ((intptr_t) h, O_RDWR); |
0ea3f30e DJ |
66 | if (scb->fd < 0) |
67 | { | |
68 | errno = ENOENT; | |
69 | return -1; | |
70 | } | |
71 | ||
72 | if (!SetCommMask (h, EV_RXCHAR)) | |
73 | { | |
74 | errno = EINVAL; | |
75 | return -1; | |
76 | } | |
77 | ||
78 | timeouts.ReadIntervalTimeout = MAXDWORD; | |
79 | timeouts.ReadTotalTimeoutConstant = 0; | |
80 | timeouts.ReadTotalTimeoutMultiplier = 0; | |
81 | timeouts.WriteTotalTimeoutConstant = 0; | |
82 | timeouts.WriteTotalTimeoutMultiplier = 0; | |
83 | if (!SetCommTimeouts (h, &timeouts)) | |
84 | { | |
85 | errno = EINVAL; | |
86 | return -1; | |
87 | } | |
88 | ||
8d749320 | 89 | state = XCNEW (struct ser_windows_state); |
0ea3f30e DJ |
90 | scb->state = state; |
91 | ||
92 | /* Create a manual reset event to watch the input buffer. */ | |
93 | state->ov.hEvent = CreateEvent (0, TRUE, FALSE, 0); | |
94 | ||
95 | /* Create a (currently unused) handle to record exceptions. */ | |
96 | state->except_event = CreateEvent (0, TRUE, FALSE, 0); | |
97 | ||
98 | return 0; | |
99 | } | |
100 | ||
101 | /* Wait for the output to drain away, as opposed to flushing (discarding) | |
102 | it. */ | |
103 | ||
104 | static int | |
105 | ser_windows_drain_output (struct serial *scb) | |
106 | { | |
107 | HANDLE h = (HANDLE) _get_osfhandle (scb->fd); | |
108 | ||
109 | return (FlushFileBuffers (h) != 0) ? 0 : -1; | |
110 | } | |
111 | ||
112 | static int | |
113 | ser_windows_flush_output (struct serial *scb) | |
114 | { | |
115 | HANDLE h = (HANDLE) _get_osfhandle (scb->fd); | |
116 | ||
117 | return (PurgeComm (h, PURGE_TXCLEAR) != 0) ? 0 : -1; | |
118 | } | |
119 | ||
120 | static int | |
121 | ser_windows_flush_input (struct serial *scb) | |
122 | { | |
123 | HANDLE h = (HANDLE) _get_osfhandle (scb->fd); | |
124 | ||
125 | return (PurgeComm (h, PURGE_RXCLEAR) != 0) ? 0 : -1; | |
126 | } | |
127 | ||
128 | static int | |
129 | ser_windows_send_break (struct serial *scb) | |
130 | { | |
131 | HANDLE h = (HANDLE) _get_osfhandle (scb->fd); | |
132 | ||
133 | if (SetCommBreak (h) == 0) | |
134 | return -1; | |
135 | ||
136 | /* Delay for 250 milliseconds. */ | |
137 | Sleep (250); | |
138 | ||
139 | if (ClearCommBreak (h)) | |
140 | return -1; | |
141 | ||
142 | return 0; | |
143 | } | |
144 | ||
145 | static void | |
146 | ser_windows_raw (struct serial *scb) | |
147 | { | |
148 | HANDLE h = (HANDLE) _get_osfhandle (scb->fd); | |
149 | DCB state; | |
150 | ||
151 | if (GetCommState (h, &state) == 0) | |
152 | return; | |
153 | ||
0ea3f30e DJ |
154 | state.fOutxCtsFlow = FALSE; |
155 | state.fOutxDsrFlow = FALSE; | |
156 | state.fDtrControl = DTR_CONTROL_ENABLE; | |
157 | state.fDsrSensitivity = FALSE; | |
158 | state.fOutX = FALSE; | |
159 | state.fInX = FALSE; | |
160 | state.fNull = FALSE; | |
161 | state.fAbortOnError = FALSE; | |
162 | state.ByteSize = 8; | |
0ea3f30e | 163 | |
0ea3f30e | 164 | if (SetCommState (h, &state) == 0) |
b37520b6 | 165 | warning (_("SetCommState failed")); |
0ea3f30e DJ |
166 | } |
167 | ||
168 | static int | |
169 | ser_windows_setstopbits (struct serial *scb, int num) | |
170 | { | |
171 | HANDLE h = (HANDLE) _get_osfhandle (scb->fd); | |
172 | DCB state; | |
173 | ||
174 | if (GetCommState (h, &state) == 0) | |
175 | return -1; | |
176 | ||
177 | switch (num) | |
178 | { | |
179 | case SERIAL_1_STOPBITS: | |
180 | state.StopBits = ONESTOPBIT; | |
181 | break; | |
182 | case SERIAL_1_AND_A_HALF_STOPBITS: | |
183 | state.StopBits = ONE5STOPBITS; | |
184 | break; | |
185 | case SERIAL_2_STOPBITS: | |
186 | state.StopBits = TWOSTOPBITS; | |
187 | break; | |
188 | default: | |
189 | return 1; | |
190 | } | |
191 | ||
192 | return (SetCommState (h, &state) != 0) ? 0 : -1; | |
193 | } | |
194 | ||
236af5e3 YG |
195 | /* Implement the "setparity" serial_ops callback. */ |
196 | ||
197 | static int | |
198 | ser_windows_setparity (struct serial *scb, int parity) | |
199 | { | |
200 | HANDLE h = (HANDLE) _get_osfhandle (scb->fd); | |
201 | DCB state; | |
202 | ||
203 | if (GetCommState (h, &state) == 0) | |
204 | return -1; | |
205 | ||
206 | switch (parity) | |
207 | { | |
208 | case GDBPARITY_NONE: | |
209 | state.Parity = NOPARITY; | |
210 | state.fParity = FALSE; | |
211 | break; | |
212 | case GDBPARITY_ODD: | |
213 | state.Parity = ODDPARITY; | |
214 | state.fParity = TRUE; | |
215 | break; | |
216 | case GDBPARITY_EVEN: | |
217 | state.Parity = EVENPARITY; | |
218 | state.fParity = TRUE; | |
219 | break; | |
220 | default: | |
221 | internal_warning (__FILE__, __LINE__, | |
8a4506c0 | 222 | "Incorrect parity value: %d", parity); |
236af5e3 YG |
223 | return -1; |
224 | } | |
225 | ||
226 | return (SetCommState (h, &state) != 0) ? 0 : -1; | |
227 | } | |
228 | ||
0ea3f30e DJ |
229 | static int |
230 | ser_windows_setbaudrate (struct serial *scb, int rate) | |
231 | { | |
232 | HANDLE h = (HANDLE) _get_osfhandle (scb->fd); | |
233 | DCB state; | |
234 | ||
235 | if (GetCommState (h, &state) == 0) | |
236 | return -1; | |
237 | ||
238 | state.BaudRate = rate; | |
239 | ||
240 | return (SetCommState (h, &state) != 0) ? 0 : -1; | |
241 | } | |
242 | ||
243 | static void | |
244 | ser_windows_close (struct serial *scb) | |
245 | { | |
246 | struct ser_windows_state *state; | |
247 | ||
e25b2cfa PM |
248 | /* Stop any pending selects. On Windows 95 OS, CancelIo function does |
249 | not exist. In that case, it can be replaced by a call to CloseHandle, | |
250 | but this is not necessary here as we do close the Windows handle | |
251 | by calling close (scb->fd) below. */ | |
4af53f97 PM |
252 | if (CancelIo) |
253 | CancelIo ((HANDLE) _get_osfhandle (scb->fd)); | |
cd78b7a1 | 254 | state = (struct ser_windows_state *) scb->state; |
0ea3f30e DJ |
255 | CloseHandle (state->ov.hEvent); |
256 | CloseHandle (state->except_event); | |
257 | ||
258 | if (scb->fd < 0) | |
259 | return; | |
260 | ||
261 | close (scb->fd); | |
262 | scb->fd = -1; | |
263 | ||
264 | xfree (scb->state); | |
265 | } | |
266 | ||
267 | static void | |
268 | ser_windows_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except) | |
269 | { | |
270 | struct ser_windows_state *state; | |
271 | COMSTAT status; | |
272 | DWORD errors; | |
273 | HANDLE h = (HANDLE) _get_osfhandle (scb->fd); | |
274 | ||
cd78b7a1 | 275 | state = (struct ser_windows_state *) scb->state; |
0ea3f30e DJ |
276 | |
277 | *except = state->except_event; | |
278 | *read = state->ov.hEvent; | |
279 | ||
280 | if (state->in_progress) | |
281 | return; | |
282 | ||
283 | /* Reset the mask - we are only interested in any characters which | |
284 | arrive after this point, not characters which might have arrived | |
285 | and already been read. */ | |
286 | ||
287 | /* This really, really shouldn't be necessary - just the second one. | |
288 | But otherwise an internal flag for EV_RXCHAR does not get | |
289 | cleared, and we get a duplicated event, if the last batch | |
290 | of characters included at least two arriving close together. */ | |
291 | if (!SetCommMask (h, 0)) | |
292 | warning (_("ser_windows_wait_handle: reseting mask failed")); | |
293 | ||
294 | if (!SetCommMask (h, EV_RXCHAR)) | |
295 | warning (_("ser_windows_wait_handle: reseting mask failed (2)")); | |
296 | ||
297 | /* There's a potential race condition here; we must check cbInQue | |
298 | and not wait if that's nonzero. */ | |
299 | ||
300 | ClearCommError (h, &errors, &status); | |
301 | if (status.cbInQue > 0) | |
302 | { | |
303 | SetEvent (state->ov.hEvent); | |
304 | return; | |
305 | } | |
306 | ||
307 | state->in_progress = 1; | |
308 | ResetEvent (state->ov.hEvent); | |
309 | state->lastCommMask = -2; | |
310 | if (WaitCommEvent (h, &state->lastCommMask, &state->ov)) | |
311 | { | |
312 | gdb_assert (state->lastCommMask & EV_RXCHAR); | |
313 | SetEvent (state->ov.hEvent); | |
314 | } | |
315 | else | |
316 | gdb_assert (GetLastError () == ERROR_IO_PENDING); | |
317 | } | |
318 | ||
319 | static int | |
320 | ser_windows_read_prim (struct serial *scb, size_t count) | |
321 | { | |
322 | struct ser_windows_state *state; | |
323 | OVERLAPPED ov; | |
324 | DWORD bytes_read, bytes_read_tmp; | |
325 | HANDLE h; | |
326 | gdb_byte *p; | |
327 | ||
cd78b7a1 | 328 | state = (struct ser_windows_state *) scb->state; |
0ea3f30e DJ |
329 | if (state->in_progress) |
330 | { | |
331 | WaitForSingleObject (state->ov.hEvent, INFINITE); | |
332 | state->in_progress = 0; | |
333 | ResetEvent (state->ov.hEvent); | |
334 | } | |
335 | ||
336 | memset (&ov, 0, sizeof (OVERLAPPED)); | |
337 | ov.hEvent = CreateEvent (0, FALSE, FALSE, 0); | |
338 | h = (HANDLE) _get_osfhandle (scb->fd); | |
339 | ||
340 | if (!ReadFile (h, scb->buf, /* count */ 1, &bytes_read, &ov)) | |
341 | { | |
342 | if (GetLastError () != ERROR_IO_PENDING | |
343 | || !GetOverlappedResult (h, &ov, &bytes_read, TRUE)) | |
344 | bytes_read = -1; | |
345 | } | |
346 | ||
347 | CloseHandle (ov.hEvent); | |
348 | return bytes_read; | |
349 | } | |
350 | ||
351 | static int | |
352 | ser_windows_write_prim (struct serial *scb, const void *buf, size_t len) | |
353 | { | |
354 | struct ser_windows_state *state; | |
355 | OVERLAPPED ov; | |
356 | DWORD bytes_written; | |
357 | HANDLE h; | |
358 | ||
359 | memset (&ov, 0, sizeof (OVERLAPPED)); | |
360 | ov.hEvent = CreateEvent (0, FALSE, FALSE, 0); | |
361 | h = (HANDLE) _get_osfhandle (scb->fd); | |
362 | if (!WriteFile (h, buf, len, &bytes_written, &ov)) | |
363 | { | |
364 | if (GetLastError () != ERROR_IO_PENDING | |
365 | || !GetOverlappedResult (h, &ov, &bytes_written, TRUE)) | |
366 | bytes_written = -1; | |
367 | } | |
368 | ||
369 | CloseHandle (ov.hEvent); | |
370 | return bytes_written; | |
371 | } | |
372 | ||
4577549b DJ |
373 | /* On Windows, gdb_select is implemented using WaitForMulpleObjects. |
374 | A "select thread" is created for each file descriptor. These | |
375 | threads looks for activity on the corresponding descriptor, using | |
376 | whatever techniques are appropriate for the descriptor type. When | |
377 | that activity occurs, the thread signals an appropriate event, | |
378 | which wakes up WaitForMultipleObjects. | |
379 | ||
380 | Each select thread is in one of two states: stopped or started. | |
381 | Select threads begin in the stopped state. When gdb_select is | |
382 | called, threads corresponding to the descriptors of interest are | |
383 | started by calling a wait_handle function. Each thread that | |
384 | notices activity signals the appropriate event and then reenters | |
385 | the stopped state. Before gdb_select returns it calls the | |
386 | wait_handle_done functions, which return the threads to the stopped | |
387 | state. */ | |
388 | ||
389 | enum select_thread_state { | |
390 | STS_STARTED, | |
391 | STS_STOPPED | |
392 | }; | |
393 | ||
0ea3f30e DJ |
394 | struct ser_console_state |
395 | { | |
4577549b DJ |
396 | /* Signaled by the select thread to indicate that data is available |
397 | on the file descriptor. */ | |
0ea3f30e | 398 | HANDLE read_event; |
4577549b DJ |
399 | /* Signaled by the select thread to indicate that an exception has |
400 | occurred on the file descriptor. */ | |
0ea3f30e | 401 | HANDLE except_event; |
4577549b DJ |
402 | /* Signaled by the select thread to indicate that it has entered the |
403 | started state. HAVE_STARTED and HAVE_STOPPED are never signaled | |
404 | simultaneously. */ | |
405 | HANDLE have_started; | |
406 | /* Signaled by the select thread to indicate that it has stopped, | |
407 | either because data is available (and READ_EVENT is signaled), | |
408 | because an exception has occurred (and EXCEPT_EVENT is signaled), | |
409 | or because STOP_SELECT was signaled. */ | |
410 | HANDLE have_stopped; | |
0ea3f30e | 411 | |
4577549b DJ |
412 | /* Signaled by the main program to tell the select thread to enter |
413 | the started state. */ | |
0ea3f30e | 414 | HANDLE start_select; |
4577549b | 415 | /* Signaled by the main program to tell the select thread to enter |
c378eb4e | 416 | the stopped state. */ |
0ea3f30e | 417 | HANDLE stop_select; |
4577549b DJ |
418 | /* Signaled by the main program to tell the select thread to |
419 | exit. */ | |
c3e2b812 | 420 | HANDLE exit_select; |
c3e2b812 | 421 | |
4577549b | 422 | /* The handle for the select thread. */ |
c3e2b812 | 423 | HANDLE thread; |
4577549b DJ |
424 | /* The state of the select thread. This field is only accessed in |
425 | the main program, never by the select thread itself. */ | |
426 | enum select_thread_state thread_state; | |
0ea3f30e DJ |
427 | }; |
428 | ||
4577549b DJ |
429 | /* Called by a select thread to enter the stopped state. This |
430 | function does not return until the thread has re-entered the | |
431 | started state. */ | |
432 | static void | |
433 | select_thread_wait (struct ser_console_state *state) | |
434 | { | |
435 | HANDLE wait_events[2]; | |
436 | ||
437 | /* There are two things that can wake us up: a request that we enter | |
438 | the started state, or that we exit this thread. */ | |
439 | wait_events[0] = state->start_select; | |
440 | wait_events[1] = state->exit_select; | |
441 | if (WaitForMultipleObjects (2, wait_events, FALSE, INFINITE) | |
442 | != WAIT_OBJECT_0) | |
443 | /* Either the EXIT_SELECT event was signaled (requesting that the | |
444 | thread exit) or an error has occurred. In either case, we exit | |
445 | the thread. */ | |
446 | ExitThread (0); | |
447 | ||
448 | /* We are now in the started state. */ | |
449 | SetEvent (state->have_started); | |
450 | } | |
451 | ||
452 | typedef DWORD WINAPI (*thread_fn_type)(void *); | |
453 | ||
454 | /* Create a new select thread for SCB executing THREAD_FN. The STATE | |
455 | will be filled in by this function before return. */ | |
a95babbf | 456 | static void |
4577549b DJ |
457 | create_select_thread (thread_fn_type thread_fn, |
458 | struct serial *scb, | |
459 | struct ser_console_state *state) | |
460 | { | |
461 | DWORD threadId; | |
462 | ||
463 | /* Create all of the events. These are all auto-reset events. */ | |
464 | state->read_event = CreateEvent (NULL, FALSE, FALSE, NULL); | |
465 | state->except_event = CreateEvent (NULL, FALSE, FALSE, NULL); | |
466 | state->have_started = CreateEvent (NULL, FALSE, FALSE, NULL); | |
467 | state->have_stopped = CreateEvent (NULL, FALSE, FALSE, NULL); | |
468 | state->start_select = CreateEvent (NULL, FALSE, FALSE, NULL); | |
469 | state->stop_select = CreateEvent (NULL, FALSE, FALSE, NULL); | |
470 | state->exit_select = CreateEvent (NULL, FALSE, FALSE, NULL); | |
471 | ||
472 | state->thread = CreateThread (NULL, 0, thread_fn, scb, 0, &threadId); | |
473 | /* The thread begins in the stopped state. */ | |
474 | state->thread_state = STS_STOPPED; | |
475 | } | |
476 | ||
477 | /* Destroy the select thread indicated by STATE. */ | |
478 | static void | |
479 | destroy_select_thread (struct ser_console_state *state) | |
480 | { | |
481 | /* Ask the thread to exit. */ | |
482 | SetEvent (state->exit_select); | |
483 | /* Wait until it does. */ | |
484 | WaitForSingleObject (state->thread, INFINITE); | |
485 | ||
486 | /* Destroy the events. */ | |
487 | CloseHandle (state->read_event); | |
488 | CloseHandle (state->except_event); | |
489 | CloseHandle (state->have_started); | |
490 | CloseHandle (state->have_stopped); | |
491 | CloseHandle (state->start_select); | |
492 | CloseHandle (state->stop_select); | |
493 | CloseHandle (state->exit_select); | |
494 | } | |
495 | ||
496 | /* Called by gdb_select to start the select thread indicated by STATE. | |
497 | This function does not return until the thread has started. */ | |
498 | static void | |
499 | start_select_thread (struct ser_console_state *state) | |
500 | { | |
501 | /* Ask the thread to start. */ | |
502 | SetEvent (state->start_select); | |
503 | /* Wait until it does. */ | |
504 | WaitForSingleObject (state->have_started, INFINITE); | |
505 | /* The thread is now started. */ | |
506 | state->thread_state = STS_STARTED; | |
507 | } | |
508 | ||
509 | /* Called by gdb_select to stop the select thread indicated by STATE. | |
510 | This function does not return until the thread has stopped. */ | |
511 | static void | |
512 | stop_select_thread (struct ser_console_state *state) | |
513 | { | |
514 | /* If the thread is already in the stopped state, we have nothing to | |
515 | do. Some of the wait_handle functions avoid calling | |
516 | start_select_thread if they notice activity on the relevant file | |
517 | descriptors. The wait_handle_done functions still call | |
518 | stop_select_thread -- but it is already stopped. */ | |
519 | if (state->thread_state != STS_STARTED) | |
520 | return; | |
521 | /* Ask the thread to stop. */ | |
522 | SetEvent (state->stop_select); | |
523 | /* Wait until it does. */ | |
524 | WaitForSingleObject (state->have_stopped, INFINITE); | |
525 | /* The thread is now stopped. */ | |
526 | state->thread_state = STS_STOPPED; | |
527 | } | |
528 | ||
0ea3f30e DJ |
529 | static DWORD WINAPI |
530 | console_select_thread (void *arg) | |
531 | { | |
cd78b7a1 | 532 | struct serial *scb = (struct serial *) arg; |
c3e2b812 DJ |
533 | struct ser_console_state *state; |
534 | int event_index; | |
0ea3f30e DJ |
535 | HANDLE h; |
536 | ||
cd78b7a1 | 537 | state = (struct ser_console_state *) scb->state; |
c3e2b812 | 538 | h = (HANDLE) _get_osfhandle (scb->fd); |
0ea3f30e DJ |
539 | |
540 | while (1) | |
541 | { | |
542 | HANDLE wait_events[2]; | |
543 | INPUT_RECORD record; | |
544 | DWORD n_records; | |
545 | ||
4577549b | 546 | select_thread_wait (state); |
c3e2b812 | 547 | |
4577549b DJ |
548 | while (1) |
549 | { | |
550 | wait_events[0] = state->stop_select; | |
551 | wait_events[1] = h; | |
0ea3f30e | 552 | |
3e43a32a MS |
553 | event_index = WaitForMultipleObjects (2, wait_events, |
554 | FALSE, INFINITE); | |
0ea3f30e | 555 | |
4577549b DJ |
556 | if (event_index == WAIT_OBJECT_0 |
557 | || WaitForSingleObject (state->stop_select, 0) == WAIT_OBJECT_0) | |
558 | break; | |
0ea3f30e | 559 | |
4577549b DJ |
560 | if (event_index != WAIT_OBJECT_0 + 1) |
561 | { | |
562 | /* Wait must have failed; assume an error has occured, e.g. | |
563 | the handle has been closed. */ | |
564 | SetEvent (state->except_event); | |
565 | break; | |
566 | } | |
0ea3f30e | 567 | |
4577549b DJ |
568 | /* We've got a pending event on the console. See if it's |
569 | of interest. */ | |
570 | if (!PeekConsoleInput (h, &record, 1, &n_records) || n_records != 1) | |
571 | { | |
572 | /* Something went wrong. Maybe the console is gone. */ | |
573 | SetEvent (state->except_event); | |
574 | break; | |
575 | } | |
0ea3f30e | 576 | |
4577549b | 577 | if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown) |
c3e2b812 | 578 | { |
4577549b DJ |
579 | WORD keycode = record.Event.KeyEvent.wVirtualKeyCode; |
580 | ||
581 | /* Ignore events containing only control keys. We must | |
582 | recognize "enhanced" keys which we are interested in | |
583 | reading via getch, if they do not map to ASCII. But we | |
584 | do not want to report input available for e.g. the | |
585 | control key alone. */ | |
586 | ||
587 | if (record.Event.KeyEvent.uChar.AsciiChar != 0 | |
588 | || keycode == VK_PRIOR | |
589 | || keycode == VK_NEXT | |
590 | || keycode == VK_END | |
591 | || keycode == VK_HOME | |
592 | || keycode == VK_LEFT | |
593 | || keycode == VK_UP | |
594 | || keycode == VK_RIGHT | |
595 | || keycode == VK_DOWN | |
596 | || keycode == VK_INSERT | |
597 | || keycode == VK_DELETE) | |
598 | { | |
599 | /* This is really a keypress. */ | |
600 | SetEvent (state->read_event); | |
601 | break; | |
602 | } | |
c3e2b812 | 603 | } |
4577549b DJ |
604 | |
605 | /* Otherwise discard it and wait again. */ | |
606 | ReadConsoleInput (h, &record, 1, &n_records); | |
0ea3f30e DJ |
607 | } |
608 | ||
4577549b | 609 | SetEvent(state->have_stopped); |
0ea3f30e | 610 | } |
a32d7317 | 611 | return 0; |
0ea3f30e DJ |
612 | } |
613 | ||
614 | static int | |
615 | fd_is_pipe (int fd) | |
616 | { | |
617 | if (PeekNamedPipe ((HANDLE) _get_osfhandle (fd), NULL, 0, NULL, NULL, NULL)) | |
618 | return 1; | |
619 | else | |
620 | return 0; | |
621 | } | |
622 | ||
7e71daaa JG |
623 | static int |
624 | fd_is_file (int fd) | |
625 | { | |
626 | if (GetFileType ((HANDLE) _get_osfhandle (fd)) == FILE_TYPE_DISK) | |
627 | return 1; | |
628 | else | |
629 | return 0; | |
630 | } | |
631 | ||
0ea3f30e DJ |
632 | static DWORD WINAPI |
633 | pipe_select_thread (void *arg) | |
634 | { | |
cd78b7a1 | 635 | struct serial *scb = (struct serial *) arg; |
c3e2b812 DJ |
636 | struct ser_console_state *state; |
637 | int event_index; | |
0ea3f30e DJ |
638 | HANDLE h; |
639 | ||
cd78b7a1 | 640 | state = (struct ser_console_state *) scb->state; |
c3e2b812 | 641 | h = (HANDLE) _get_osfhandle (scb->fd); |
0ea3f30e DJ |
642 | |
643 | while (1) | |
644 | { | |
0ea3f30e DJ |
645 | DWORD n_avail; |
646 | ||
4577549b | 647 | select_thread_wait (state); |
c3e2b812 | 648 | |
4577549b DJ |
649 | /* Wait for something to happen on the pipe. */ |
650 | while (1) | |
0ea3f30e | 651 | { |
4577549b DJ |
652 | if (!PeekNamedPipe (h, NULL, 0, NULL, &n_avail, NULL)) |
653 | { | |
654 | SetEvent (state->except_event); | |
655 | break; | |
656 | } | |
0ea3f30e | 657 | |
4577549b DJ |
658 | if (n_avail > 0) |
659 | { | |
660 | SetEvent (state->read_event); | |
661 | break; | |
662 | } | |
0ea3f30e | 663 | |
4577549b DJ |
664 | /* Delay 10ms before checking again, but allow the stop |
665 | event to wake us. */ | |
666 | if (WaitForSingleObject (state->stop_select, 10) == WAIT_OBJECT_0) | |
667 | break; | |
668 | } | |
0ea3f30e | 669 | |
4577549b | 670 | SetEvent (state->have_stopped); |
0ea3f30e | 671 | } |
a32d7317 | 672 | return 0; |
0ea3f30e DJ |
673 | } |
674 | ||
7e71daaa JG |
675 | static DWORD WINAPI |
676 | file_select_thread (void *arg) | |
677 | { | |
cd78b7a1 | 678 | struct serial *scb = (struct serial *) arg; |
7e71daaa JG |
679 | struct ser_console_state *state; |
680 | int event_index; | |
681 | HANDLE h; | |
682 | ||
cd78b7a1 | 683 | state = (struct ser_console_state *) scb->state; |
7e71daaa JG |
684 | h = (HANDLE) _get_osfhandle (scb->fd); |
685 | ||
686 | while (1) | |
687 | { | |
4577549b | 688 | select_thread_wait (state); |
7e71daaa | 689 | |
3e43a32a MS |
690 | if (SetFilePointer (h, 0, NULL, FILE_CURRENT) |
691 | == INVALID_SET_FILE_POINTER) | |
4577549b DJ |
692 | SetEvent (state->except_event); |
693 | else | |
694 | SetEvent (state->read_event); | |
7e71daaa | 695 | |
4577549b | 696 | SetEvent (state->have_stopped); |
7e71daaa | 697 | } |
a32d7317 | 698 | return 0; |
7e71daaa JG |
699 | } |
700 | ||
0ea3f30e DJ |
701 | static void |
702 | ser_console_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except) | |
703 | { | |
cd78b7a1 | 704 | struct ser_console_state *state = (struct ser_console_state *) scb->state; |
0ea3f30e DJ |
705 | |
706 | if (state == NULL) | |
707 | { | |
4577549b | 708 | thread_fn_type thread_fn; |
0ea3f30e DJ |
709 | int is_tty; |
710 | ||
711 | is_tty = isatty (scb->fd); | |
7e71daaa | 712 | if (!is_tty && !fd_is_file (scb->fd) && !fd_is_pipe (scb->fd)) |
0ea3f30e DJ |
713 | { |
714 | *read = NULL; | |
715 | *except = NULL; | |
716 | return; | |
717 | } | |
718 | ||
8d749320 | 719 | state = XCNEW (struct ser_console_state); |
0ea3f30e DJ |
720 | scb->state = state; |
721 | ||
0ea3f30e | 722 | if (is_tty) |
4577549b | 723 | thread_fn = console_select_thread; |
7e71daaa | 724 | else if (fd_is_pipe (scb->fd)) |
4577549b | 725 | thread_fn = pipe_select_thread; |
7e71daaa | 726 | else |
4577549b DJ |
727 | thread_fn = file_select_thread; |
728 | ||
729 | create_select_thread (thread_fn, scb, state); | |
0ea3f30e DJ |
730 | } |
731 | ||
c3e2b812 DJ |
732 | *read = state->read_event; |
733 | *except = state->except_event; | |
734 | ||
735 | /* Start from a blank state. */ | |
0ea3f30e DJ |
736 | ResetEvent (state->read_event); |
737 | ResetEvent (state->except_event); | |
c3e2b812 DJ |
738 | ResetEvent (state->stop_select); |
739 | ||
740 | /* First check for a key already in the buffer. If there is one, | |
741 | we don't need a thread. This also catches the second key of | |
742 | multi-character returns from getch, for instance for arrow | |
743 | keys. The second half is in a C library internal buffer, | |
744 | and PeekConsoleInput will not find it. */ | |
745 | if (_kbhit ()) | |
746 | { | |
747 | SetEvent (state->read_event); | |
748 | return; | |
749 | } | |
0ea3f30e | 750 | |
c3e2b812 | 751 | /* Otherwise, start the select thread. */ |
4577549b | 752 | start_select_thread (state); |
c3e2b812 | 753 | } |
0ea3f30e | 754 | |
c3e2b812 DJ |
755 | static void |
756 | ser_console_done_wait_handle (struct serial *scb) | |
757 | { | |
cd78b7a1 | 758 | struct ser_console_state *state = (struct ser_console_state *) scb->state; |
c3e2b812 DJ |
759 | |
760 | if (state == NULL) | |
761 | return; | |
762 | ||
4577549b | 763 | stop_select_thread (state); |
0ea3f30e DJ |
764 | } |
765 | ||
766 | static void | |
767 | ser_console_close (struct serial *scb) | |
768 | { | |
cd78b7a1 | 769 | struct ser_console_state *state = (struct ser_console_state *) scb->state; |
0ea3f30e DJ |
770 | |
771 | if (scb->state) | |
772 | { | |
4577549b | 773 | destroy_select_thread (state); |
0ea3f30e DJ |
774 | xfree (scb->state); |
775 | } | |
776 | } | |
777 | ||
778 | struct ser_console_ttystate | |
779 | { | |
780 | int is_a_tty; | |
781 | }; | |
782 | ||
783 | static serial_ttystate | |
784 | ser_console_get_tty_state (struct serial *scb) | |
785 | { | |
786 | if (isatty (scb->fd)) | |
787 | { | |
788 | struct ser_console_ttystate *state; | |
433759f7 | 789 | |
8d749320 | 790 | state = XNEW (struct ser_console_ttystate); |
0ea3f30e DJ |
791 | state->is_a_tty = 1; |
792 | return state; | |
793 | } | |
794 | else | |
795 | return NULL; | |
796 | } | |
797 | ||
5f1fb6dc JB |
798 | struct pipe_state |
799 | { | |
800 | /* Since we use the pipe_select_thread for our select emulation, | |
801 | we need to place the state structure it requires at the front | |
802 | of our state. */ | |
803 | struct ser_console_state wait; | |
804 | ||
805 | /* The pex obj for our (one-stage) pipeline. */ | |
806 | struct pex_obj *pex; | |
807 | ||
808 | /* Streams for the pipeline's input and output. */ | |
809 | FILE *input, *output; | |
810 | }; | |
811 | ||
812 | static struct pipe_state * | |
813 | make_pipe_state (void) | |
814 | { | |
8d749320 | 815 | struct pipe_state *ps = XCNEW (struct pipe_state); |
5f1fb6dc | 816 | |
5f1fb6dc JB |
817 | ps->wait.read_event = INVALID_HANDLE_VALUE; |
818 | ps->wait.except_event = INVALID_HANDLE_VALUE; | |
819 | ps->wait.start_select = INVALID_HANDLE_VALUE; | |
820 | ps->wait.stop_select = INVALID_HANDLE_VALUE; | |
821 | ||
822 | return ps; | |
823 | } | |
824 | ||
825 | static void | |
826 | free_pipe_state (struct pipe_state *ps) | |
827 | { | |
828 | int saved_errno = errno; | |
829 | ||
830 | if (ps->wait.read_event != INVALID_HANDLE_VALUE) | |
4577549b | 831 | destroy_select_thread (&ps->wait); |
5f1fb6dc JB |
832 | |
833 | /* Close the pipe to the child. We must close the pipe before | |
834 | calling pex_free because pex_free will wait for the child to exit | |
835 | and the child will not exit until the pipe is closed. */ | |
836 | if (ps->input) | |
837 | fclose (ps->input); | |
838 | if (ps->pex) | |
58f07bae PA |
839 | { |
840 | pex_free (ps->pex); | |
841 | /* pex_free closes ps->output. */ | |
842 | } | |
843 | else if (ps->output) | |
844 | fclose (ps->output); | |
5f1fb6dc JB |
845 | |
846 | xfree (ps); | |
847 | ||
848 | errno = saved_errno; | |
849 | } | |
850 | ||
851 | static void | |
852 | cleanup_pipe_state (void *untyped) | |
853 | { | |
cd78b7a1 | 854 | struct pipe_state *ps = (struct pipe_state *) untyped; |
5f1fb6dc JB |
855 | |
856 | free_pipe_state (ps); | |
857 | } | |
858 | ||
859 | static int | |
860 | pipe_windows_open (struct serial *scb, const char *name) | |
861 | { | |
ef7723eb | 862 | struct pipe_state *ps; |
65cc4390 | 863 | FILE *pex_stderr; |
efdb2a86 | 864 | struct cleanup *back_to; |
ef7723eb | 865 | |
d1a41061 PP |
866 | if (name == NULL) |
867 | error_no_arg (_("child command")); | |
868 | ||
773a1edc | 869 | gdb_argv argv (name); |
d1a41061 | 870 | |
5f1fb6dc | 871 | if (! argv[0] || argv[0][0] == '\0') |
a73c6dcd | 872 | error (_("missing child command")); |
5f1fb6dc | 873 | |
ef7723eb | 874 | ps = make_pipe_state (); |
773a1edc | 875 | back_to = make_cleanup (cleanup_pipe_state, ps); |
5f1fb6dc JB |
876 | |
877 | ps->pex = pex_init (PEX_USE_PIPES, "target remote pipe", NULL); | |
878 | if (! ps->pex) | |
879 | goto fail; | |
880 | ps->input = pex_input_pipe (ps->pex, 1); | |
881 | if (! ps->input) | |
882 | goto fail; | |
883 | ||
884 | { | |
885 | int err; | |
886 | const char *err_msg | |
65cc4390 VP |
887 | = pex_run (ps->pex, PEX_SEARCH | PEX_BINARY_INPUT | PEX_BINARY_OUTPUT |
888 | | PEX_STDERR_TO_PIPE, | |
773a1edc | 889 | argv[0], argv.get (), NULL, NULL, |
5f1fb6dc JB |
890 | &err); |
891 | ||
892 | if (err_msg) | |
893 | { | |
894 | /* Our caller expects us to return -1, but all they'll do with | |
895 | it generally is print the message based on errno. We have | |
896 | all the same information here, plus err_msg provided by | |
897 | pex_run, so we just raise the error here. */ | |
898 | if (err) | |
a73c6dcd | 899 | error (_("error starting child process '%s': %s: %s"), |
5f1fb6dc JB |
900 | name, err_msg, safe_strerror (err)); |
901 | else | |
a73c6dcd | 902 | error (_("error starting child process '%s': %s"), |
5f1fb6dc JB |
903 | name, err_msg); |
904 | } | |
905 | } | |
906 | ||
907 | ps->output = pex_read_output (ps->pex, 1); | |
908 | if (! ps->output) | |
909 | goto fail; | |
5f1fb6dc | 910 | scb->fd = fileno (ps->output); |
65cc4390 VP |
911 | |
912 | pex_stderr = pex_read_err (ps->pex, 1); | |
913 | if (! pex_stderr) | |
914 | goto fail; | |
915 | scb->error_fd = fileno (pex_stderr); | |
916 | ||
5f1fb6dc JB |
917 | scb->state = (void *) ps; |
918 | ||
773a1edc | 919 | argv.release (); |
5f1fb6dc JB |
920 | discard_cleanups (back_to); |
921 | return 0; | |
922 | ||
923 | fail: | |
924 | do_cleanups (back_to); | |
925 | return -1; | |
926 | } | |
927 | ||
58f07bae PA |
928 | static int |
929 | pipe_windows_fdopen (struct serial *scb, int fd) | |
930 | { | |
931 | struct pipe_state *ps; | |
932 | ||
933 | ps = make_pipe_state (); | |
934 | ||
935 | ps->input = fdopen (fd, "r+"); | |
936 | if (! ps->input) | |
937 | goto fail; | |
938 | ||
939 | ps->output = fdopen (fd, "r+"); | |
940 | if (! ps->output) | |
941 | goto fail; | |
942 | ||
943 | scb->fd = fd; | |
944 | scb->state = (void *) ps; | |
945 | ||
946 | return 0; | |
947 | ||
948 | fail: | |
949 | free_pipe_state (ps); | |
950 | return -1; | |
951 | } | |
5f1fb6dc JB |
952 | |
953 | static void | |
954 | pipe_windows_close (struct serial *scb) | |
955 | { | |
cd78b7a1 | 956 | struct pipe_state *ps = (struct pipe_state *) scb->state; |
5f1fb6dc JB |
957 | |
958 | /* In theory, we should try to kill the subprocess here, but the pex | |
959 | interface doesn't give us enough information to do that. Usually | |
960 | closing the input pipe will get the message across. */ | |
961 | ||
962 | free_pipe_state (ps); | |
963 | } | |
964 | ||
965 | ||
966 | static int | |
967 | pipe_windows_read (struct serial *scb, size_t count) | |
968 | { | |
4998c1df | 969 | HANDLE pipeline_out = (HANDLE) _get_osfhandle (scb->fd); |
ef7723eb VP |
970 | DWORD available; |
971 | DWORD bytes_read; | |
972 | ||
5f1fb6dc JB |
973 | if (pipeline_out == INVALID_HANDLE_VALUE) |
974 | return -1; | |
975 | ||
5f1fb6dc JB |
976 | if (! PeekNamedPipe (pipeline_out, NULL, 0, NULL, &available, NULL)) |
977 | return -1; | |
978 | ||
979 | if (count > available) | |
980 | count = available; | |
981 | ||
5f1fb6dc JB |
982 | if (! ReadFile (pipeline_out, scb->buf, count, &bytes_read, NULL)) |
983 | return -1; | |
984 | ||
985 | return bytes_read; | |
986 | } | |
987 | ||
988 | ||
989 | static int | |
990 | pipe_windows_write (struct serial *scb, const void *buf, size_t count) | |
991 | { | |
cd78b7a1 | 992 | struct pipe_state *ps = (struct pipe_state *) scb->state; |
ef7723eb VP |
993 | HANDLE pipeline_in; |
994 | DWORD written; | |
995 | ||
5f1fb6dc JB |
996 | int pipeline_in_fd = fileno (ps->input); |
997 | if (pipeline_in_fd < 0) | |
998 | return -1; | |
999 | ||
ef7723eb | 1000 | pipeline_in = (HANDLE) _get_osfhandle (pipeline_in_fd); |
5f1fb6dc JB |
1001 | if (pipeline_in == INVALID_HANDLE_VALUE) |
1002 | return -1; | |
1003 | ||
5f1fb6dc JB |
1004 | if (! WriteFile (pipeline_in, buf, count, &written, NULL)) |
1005 | return -1; | |
1006 | ||
1007 | return written; | |
1008 | } | |
1009 | ||
1010 | ||
1011 | static void | |
1012 | pipe_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except) | |
1013 | { | |
cd78b7a1 | 1014 | struct pipe_state *ps = (struct pipe_state *) scb->state; |
5f1fb6dc JB |
1015 | |
1016 | /* Have we allocated our events yet? */ | |
1017 | if (ps->wait.read_event == INVALID_HANDLE_VALUE) | |
4577549b DJ |
1018 | /* Start the thread. */ |
1019 | create_select_thread (pipe_select_thread, scb, &ps->wait); | |
5f1fb6dc | 1020 | |
774a49c0 DJ |
1021 | *read = ps->wait.read_event; |
1022 | *except = ps->wait.except_event; | |
1023 | ||
1024 | /* Start from a blank state. */ | |
5f1fb6dc JB |
1025 | ResetEvent (ps->wait.read_event); |
1026 | ResetEvent (ps->wait.except_event); | |
774a49c0 | 1027 | ResetEvent (ps->wait.stop_select); |
5f1fb6dc | 1028 | |
4577549b | 1029 | start_select_thread (&ps->wait); |
5f1fb6dc JB |
1030 | } |
1031 | ||
774a49c0 DJ |
1032 | static void |
1033 | pipe_done_wait_handle (struct serial *scb) | |
1034 | { | |
cd78b7a1 | 1035 | struct pipe_state *ps = (struct pipe_state *) scb->state; |
774a49c0 DJ |
1036 | |
1037 | /* Have we allocated our events yet? */ | |
1038 | if (ps->wait.read_event == INVALID_HANDLE_VALUE) | |
1039 | return; | |
1040 | ||
4577549b | 1041 | stop_select_thread (&ps->wait); |
774a49c0 | 1042 | } |
5f1fb6dc | 1043 | |
65cc4390 VP |
1044 | static int |
1045 | pipe_avail (struct serial *scb, int fd) | |
1046 | { | |
1047 | HANDLE h = (HANDLE) _get_osfhandle (fd); | |
1048 | DWORD numBytes; | |
1049 | BOOL r = PeekNamedPipe (h, NULL, 0, NULL, &numBytes, NULL); | |
433759f7 | 1050 | |
65cc4390 VP |
1051 | if (r == FALSE) |
1052 | numBytes = 0; | |
1053 | return numBytes; | |
1054 | } | |
1055 | ||
58f07bae PA |
1056 | int |
1057 | gdb_pipe (int pdes[2]) | |
1058 | { | |
1059 | if (_pipe (pdes, 512, _O_BINARY | _O_NOINHERIT) == -1) | |
1060 | return -1; | |
1061 | return 0; | |
1062 | } | |
1063 | ||
0ea3f30e DJ |
1064 | struct net_windows_state |
1065 | { | |
4577549b DJ |
1066 | struct ser_console_state base; |
1067 | ||
0ea3f30e DJ |
1068 | HANDLE sock_event; |
1069 | }; | |
1070 | ||
364fe1f7 PA |
1071 | /* Check whether the socket has any pending data to be read. If so, |
1072 | set the select thread's read event. On error, set the select | |
1073 | thread's except event. If any event was set, return true, | |
1074 | otherwise return false. */ | |
1075 | ||
1076 | static int | |
1077 | net_windows_socket_check_pending (struct serial *scb) | |
1078 | { | |
cd78b7a1 | 1079 | struct net_windows_state *state = (struct net_windows_state *) scb->state; |
364fe1f7 PA |
1080 | unsigned long available; |
1081 | ||
1082 | if (ioctlsocket (scb->fd, FIONREAD, &available) != 0) | |
1083 | { | |
1084 | /* The socket closed, or some other error. */ | |
1085 | SetEvent (state->base.except_event); | |
1086 | return 1; | |
1087 | } | |
1088 | else if (available > 0) | |
1089 | { | |
1090 | SetEvent (state->base.read_event); | |
1091 | return 1; | |
1092 | } | |
1093 | ||
1094 | return 0; | |
1095 | } | |
1096 | ||
0ea3f30e DJ |
1097 | static DWORD WINAPI |
1098 | net_windows_select_thread (void *arg) | |
1099 | { | |
cd78b7a1 | 1100 | struct serial *scb = (struct serial *) arg; |
4577549b | 1101 | struct net_windows_state *state; |
c3e2b812 | 1102 | int event_index; |
0ea3f30e | 1103 | |
cd78b7a1 | 1104 | state = (struct net_windows_state *) scb->state; |
0ea3f30e DJ |
1105 | |
1106 | while (1) | |
1107 | { | |
1108 | HANDLE wait_events[2]; | |
1109 | WSANETWORKEVENTS events; | |
1110 | ||
4577549b | 1111 | select_thread_wait (&state->base); |
c3e2b812 | 1112 | |
4577549b | 1113 | wait_events[0] = state->base.stop_select; |
0ea3f30e DJ |
1114 | wait_events[1] = state->sock_event; |
1115 | ||
364fe1f7 PA |
1116 | /* Wait for something to happen on the socket. */ |
1117 | while (1) | |
0ea3f30e | 1118 | { |
364fe1f7 PA |
1119 | event_index = WaitForMultipleObjects (2, wait_events, FALSE, INFINITE); |
1120 | ||
1121 | if (event_index == WAIT_OBJECT_0 | |
1122 | || WaitForSingleObject (state->base.stop_select, 0) == WAIT_OBJECT_0) | |
1123 | { | |
1124 | /* We have been requested to stop. */ | |
1125 | break; | |
1126 | } | |
1127 | ||
1128 | if (event_index != WAIT_OBJECT_0 + 1) | |
1129 | { | |
1130 | /* Some error has occured. Assume that this is an error | |
1131 | condition. */ | |
1132 | SetEvent (state->base.except_event); | |
1133 | break; | |
1134 | } | |
1135 | ||
4577549b DJ |
1136 | /* Enumerate the internal network events, and reset the |
1137 | object that signalled us to catch the next event. */ | |
364fe1f7 PA |
1138 | if (WSAEnumNetworkEvents (scb->fd, state->sock_event, &events) != 0) |
1139 | { | |
1140 | /* Something went wrong. Maybe the socket is gone. */ | |
1141 | SetEvent (state->base.except_event); | |
1142 | break; | |
1143 | } | |
1144 | ||
4577549b | 1145 | if (events.lNetworkEvents & FD_READ) |
364fe1f7 PA |
1146 | { |
1147 | if (net_windows_socket_check_pending (scb)) | |
1148 | break; | |
1149 | ||
1150 | /* Spurious wakeup. That is, the socket's event was | |
1151 | signalled before we last called recv. */ | |
1152 | } | |
1153 | ||
4577549b | 1154 | if (events.lNetworkEvents & FD_CLOSE) |
364fe1f7 PA |
1155 | { |
1156 | SetEvent (state->base.except_event); | |
1157 | break; | |
1158 | } | |
0ea3f30e DJ |
1159 | } |
1160 | ||
4577549b | 1161 | SetEvent (state->base.have_stopped); |
0ea3f30e | 1162 | } |
364fe1f7 | 1163 | return 0; |
0ea3f30e DJ |
1164 | } |
1165 | ||
1166 | static void | |
1167 | net_windows_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except) | |
1168 | { | |
cd78b7a1 | 1169 | struct net_windows_state *state = (struct net_windows_state *) scb->state; |
0ea3f30e | 1170 | |
c3e2b812 | 1171 | /* Start from a clean slate. */ |
4577549b DJ |
1172 | ResetEvent (state->base.read_event); |
1173 | ResetEvent (state->base.except_event); | |
1174 | ResetEvent (state->base.stop_select); | |
0ea3f30e | 1175 | |
4577549b DJ |
1176 | *read = state->base.read_event; |
1177 | *except = state->base.except_event; | |
c3e2b812 | 1178 | |
364fe1f7 PA |
1179 | /* Check any pending events. Otherwise, start the select |
1180 | thread. */ | |
1181 | if (!net_windows_socket_check_pending (scb)) | |
1182 | start_select_thread (&state->base); | |
c3e2b812 DJ |
1183 | } |
1184 | ||
1185 | static void | |
1186 | net_windows_done_wait_handle (struct serial *scb) | |
1187 | { | |
cd78b7a1 | 1188 | struct net_windows_state *state = (struct net_windows_state *) scb->state; |
c3e2b812 | 1189 | |
4577549b | 1190 | stop_select_thread (&state->base); |
0ea3f30e DJ |
1191 | } |
1192 | ||
1193 | static int | |
1194 | net_windows_open (struct serial *scb, const char *name) | |
1195 | { | |
1196 | struct net_windows_state *state; | |
1197 | int ret; | |
1198 | DWORD threadId; | |
1199 | ||
1200 | ret = net_open (scb, name); | |
1201 | if (ret != 0) | |
1202 | return ret; | |
1203 | ||
8d749320 | 1204 | state = XCNEW (struct net_windows_state); |
0ea3f30e DJ |
1205 | scb->state = state; |
1206 | ||
0ea3f30e DJ |
1207 | /* Associate an event with the socket. */ |
1208 | state->sock_event = CreateEvent (0, TRUE, FALSE, 0); | |
1209 | WSAEventSelect (scb->fd, state->sock_event, FD_READ | FD_CLOSE); | |
1210 | ||
4577549b DJ |
1211 | /* Start the thread. */ |
1212 | create_select_thread (net_windows_select_thread, scb, &state->base); | |
0ea3f30e DJ |
1213 | |
1214 | return 0; | |
1215 | } | |
1216 | ||
1217 | ||
1218 | static void | |
1219 | net_windows_close (struct serial *scb) | |
1220 | { | |
cd78b7a1 | 1221 | struct net_windows_state *state = (struct net_windows_state *) scb->state; |
0ea3f30e | 1222 | |
4577549b | 1223 | destroy_select_thread (&state->base); |
0ea3f30e DJ |
1224 | CloseHandle (state->sock_event); |
1225 | ||
1226 | xfree (scb->state); | |
1227 | ||
1228 | net_close (scb); | |
1229 | } | |
1230 | ||
12e8c7d7 TT |
1231 | /* The serial port driver. */ |
1232 | ||
1233 | static const struct serial_ops hardwire_ops = | |
1234 | { | |
1235 | "hardwire", | |
1236 | ser_windows_open, | |
1237 | ser_windows_close, | |
1238 | NULL, | |
1239 | ser_base_readchar, | |
1240 | ser_base_write, | |
1241 | ser_windows_flush_output, | |
1242 | ser_windows_flush_input, | |
1243 | ser_windows_send_break, | |
1244 | ser_windows_raw, | |
1245 | /* These are only used for stdin; we do not need them for serial | |
1246 | ports, so supply the standard dummies. */ | |
1247 | ser_base_get_tty_state, | |
1248 | ser_base_copy_tty_state, | |
1249 | ser_base_set_tty_state, | |
1250 | ser_base_print_tty_state, | |
1251 | ser_base_noflush_set_tty_state, | |
1252 | ser_windows_setbaudrate, | |
1253 | ser_windows_setstopbits, | |
236af5e3 | 1254 | ser_windows_setparity, |
12e8c7d7 TT |
1255 | ser_windows_drain_output, |
1256 | ser_base_async, | |
1257 | ser_windows_read_prim, | |
1258 | ser_windows_write_prim, | |
1259 | NULL, | |
1260 | ser_windows_wait_handle | |
1261 | }; | |
1262 | ||
1263 | /* The dummy serial driver used for terminals. We only provide the | |
1264 | TTY-related methods. */ | |
1265 | ||
1266 | static const struct serial_ops tty_ops = | |
1267 | { | |
1268 | "terminal", | |
1269 | NULL, | |
1270 | ser_console_close, | |
1271 | NULL, | |
1272 | NULL, | |
1273 | NULL, | |
1274 | NULL, | |
1275 | NULL, | |
1276 | NULL, | |
1277 | NULL, | |
1278 | ser_console_get_tty_state, | |
1279 | ser_base_copy_tty_state, | |
1280 | ser_base_set_tty_state, | |
1281 | ser_base_print_tty_state, | |
1282 | ser_base_noflush_set_tty_state, | |
1283 | NULL, | |
1284 | NULL, | |
236af5e3 | 1285 | NULL, |
12e8c7d7 TT |
1286 | ser_base_drain_output, |
1287 | NULL, | |
1288 | NULL, | |
1289 | NULL, | |
1290 | NULL, | |
1291 | ser_console_wait_handle, | |
1292 | ser_console_done_wait_handle | |
1293 | }; | |
1294 | ||
1295 | /* The pipe interface. */ | |
1296 | ||
1297 | static const struct serial_ops pipe_ops = | |
1298 | { | |
1299 | "pipe", | |
1300 | pipe_windows_open, | |
1301 | pipe_windows_close, | |
1302 | pipe_windows_fdopen, | |
1303 | ser_base_readchar, | |
1304 | ser_base_write, | |
1305 | ser_base_flush_output, | |
1306 | ser_base_flush_input, | |
1307 | ser_base_send_break, | |
1308 | ser_base_raw, | |
1309 | ser_base_get_tty_state, | |
1310 | ser_base_copy_tty_state, | |
1311 | ser_base_set_tty_state, | |
1312 | ser_base_print_tty_state, | |
1313 | ser_base_noflush_set_tty_state, | |
1314 | ser_base_setbaudrate, | |
1315 | ser_base_setstopbits, | |
236af5e3 | 1316 | ser_base_setparity, |
12e8c7d7 TT |
1317 | ser_base_drain_output, |
1318 | ser_base_async, | |
1319 | pipe_windows_read, | |
1320 | pipe_windows_write, | |
1321 | pipe_avail, | |
1322 | pipe_wait_handle, | |
1323 | pipe_done_wait_handle | |
1324 | }; | |
1325 | ||
1326 | /* The TCP/UDP socket driver. */ | |
1327 | ||
1328 | static const struct serial_ops tcp_ops = | |
1329 | { | |
1330 | "tcp", | |
1331 | net_windows_open, | |
1332 | net_windows_close, | |
1333 | NULL, | |
1334 | ser_base_readchar, | |
1335 | ser_base_write, | |
1336 | ser_base_flush_output, | |
1337 | ser_base_flush_input, | |
1338 | ser_tcp_send_break, | |
1339 | ser_base_raw, | |
1340 | ser_base_get_tty_state, | |
1341 | ser_base_copy_tty_state, | |
1342 | ser_base_set_tty_state, | |
1343 | ser_base_print_tty_state, | |
1344 | ser_base_noflush_set_tty_state, | |
1345 | ser_base_setbaudrate, | |
1346 | ser_base_setstopbits, | |
236af5e3 | 1347 | ser_base_setparity, |
12e8c7d7 TT |
1348 | ser_base_drain_output, |
1349 | ser_base_async, | |
1350 | net_read_prim, | |
1351 | net_write_prim, | |
1352 | NULL, | |
1353 | net_windows_wait_handle, | |
1354 | net_windows_done_wait_handle | |
1355 | }; | |
1356 | ||
0ea3f30e DJ |
1357 | void |
1358 | _initialize_ser_windows (void) | |
1359 | { | |
1360 | WSADATA wsa_data; | |
1361 | struct serial_ops *ops; | |
1362 | ||
4af53f97 PM |
1363 | HMODULE hm = NULL; |
1364 | ||
1365 | /* First find out if kernel32 exports CancelIo function. */ | |
1366 | hm = LoadLibrary ("kernel32.dll"); | |
1367 | if (hm) | |
1368 | { | |
cd78b7a1 | 1369 | CancelIo = (CancelIo_ftype *) GetProcAddress (hm, "CancelIo"); |
4af53f97 PM |
1370 | FreeLibrary (hm); |
1371 | } | |
1372 | else | |
1373 | CancelIo = NULL; | |
0ea3f30e | 1374 | |
12e8c7d7 TT |
1375 | serial_add_interface (&hardwire_ops); |
1376 | serial_add_interface (&tty_ops); | |
1377 | serial_add_interface (&pipe_ops); | |
5f1fb6dc | 1378 | |
0ea3f30e DJ |
1379 | /* If WinSock works, register the TCP/UDP socket driver. */ |
1380 | ||
1381 | if (WSAStartup (MAKEWORD (1, 0), &wsa_data) != 0) | |
1382 | /* WinSock is unavailable. */ | |
1383 | return; | |
1384 | ||
12e8c7d7 | 1385 | serial_add_interface (&tcp_ops); |
0ea3f30e | 1386 | } |