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