-/* Wait for input on scb, with timeout seconds. Returns 0 on success,
- otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
-
- For termio{s}, we actually just setup VTIME if necessary, and let the
- timeout occur in the read() in hardwire_read().
- */
-
-/* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
- ser_unix*() until the old TERMIOS/SGTTY/... timer code has been
- flushed. . */
-
-/* NOTE: cagney/1999-09-30: Much of the code below is dead. The only
- possible values of the TIMEOUT parameter are ONE and ZERO.
- Consequently all the code that tries to handle the possability of
- an overflowed timer is unnecessary. */
-
-static int
-wait_for (serial_t scb, int timeout)
-{
-#ifdef HAVE_SGTTY
- {
- struct timeval tv;
- fd_set readfds;
-
- FD_ZERO (&readfds);
-
- tv.tv_sec = timeout;
- tv.tv_usec = 0;
-
- FD_SET (scb->fd, &readfds);
-
- while (1)
- {
- int numfds;
-
- if (timeout >= 0)
- numfds = select (scb->fd + 1, &readfds, 0, 0, &tv);
- else
- numfds = select (scb->fd + 1, &readfds, 0, 0, 0);
-
- if (numfds <= 0)
- if (numfds == 0)
- return SERIAL_TIMEOUT;
- else if (errno == EINTR)
- continue;
- else
- return SERIAL_ERROR; /* Got an error from select or poll */
-
- return 0;
- }
- }
-#endif /* HAVE_SGTTY */
-
-#if defined HAVE_TERMIO || defined HAVE_TERMIOS
- if (timeout == scb->current_timeout)
- return 0;
-
- scb->current_timeout = timeout;
-
- {
- struct hardwire_ttystate state;
-
- if (get_tty_state (scb, &state))
- fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
-
-#ifdef HAVE_TERMIOS
- if (timeout < 0)
- {
- /* No timeout. */
- state.termios.c_cc[VTIME] = 0;
- state.termios.c_cc[VMIN] = 1;
- }
- else
- {
- state.termios.c_cc[VMIN] = 0;
- state.termios.c_cc[VTIME] = timeout * 10;
- if (state.termios.c_cc[VTIME] != timeout * 10)
- {
-
- /* If c_cc is an 8-bit signed character, we can't go
- bigger than this. If it is always unsigned, we could use
- 25. */
-
- scb->current_timeout = 12;
- state.termios.c_cc[VTIME] = scb->current_timeout * 10;
- scb->timeout_remaining = timeout - scb->current_timeout;
- }
- }
-#endif
-
-#ifdef HAVE_TERMIO
- if (timeout < 0)
- {
- /* No timeout. */
- state.termio.c_cc[VTIME] = 0;
- state.termio.c_cc[VMIN] = 1;
- }
- else
- {
- state.termio.c_cc[VMIN] = 0;
- state.termio.c_cc[VTIME] = timeout * 10;
- if (state.termio.c_cc[VTIME] != timeout * 10)
- {
- /* If c_cc is an 8-bit signed character, we can't go
- bigger than this. If it is always unsigned, we could use
- 25. */
-
- scb->current_timeout = 12;
- state.termio.c_cc[VTIME] = scb->current_timeout * 10;
- scb->timeout_remaining = timeout - scb->current_timeout;
- }
- }
-#endif
-
- if (set_tty_state (scb, &state))
- fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
-
- return 0;
- }
-#endif /* HAVE_TERMIO || HAVE_TERMIOS */
-}
-
-/* Read a character with user-specified timeout. TIMEOUT is number of seconds
- to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
- char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
- dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
-
-/* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
- ser_unix*() until the old TERMIOS/SGTTY/... timer code has been
- flushed. */
-
-/* NOTE: cagney/1999-09-16: This function is not identical to
- ser_unix_readchar() as part of replacing it with ser_unix*()
- merging will be required - this code handles the case where read()
- times out due to no data while ser_unix_readchar() doesn't expect
- that. */
-
-static int
-do_hardwire_readchar (serial_t scb, int timeout)
-{
- int status, delta;
- int detach = 0;
-
- if (timeout > 0)
- timeout++;
-
- /* We have to be able to keep the GUI alive here, so we break the original
- timeout into steps of 1 second, running the "keep the GUI alive" hook
- each time through the loop.
- Also, timeout = 0 means to poll, so we just set the delta to 0, so we
- will only go through the loop once. */
-
- delta = (timeout == 0 ? 0 : 1);
- while (1)
- {
-
- /* N.B. The UI may destroy our world (for instance by calling
- remote_stop,) in which case we want to get out of here as
- quickly as possible. It is not safe to touch scb, since
- someone else might have freed it. The ui_loop_hook signals that
- we should exit by returning 1. */
-
- if (ui_loop_hook)
- detach = ui_loop_hook (0);
-
- if (detach)
- return SERIAL_TIMEOUT;
-
- scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
- status = wait_for (scb, delta);
-
- if (status < 0)
- return status;
-
- status = read (scb->fd, scb->buf, BUFSIZ);
-
- if (status <= 0)
- {
- if (status == 0)
- {
- /* Zero characters means timeout (it could also be EOF, but
- we don't (yet at least) distinguish). */
- if (scb->timeout_remaining > 0)
- {
- timeout = scb->timeout_remaining;
- continue;
- }
- else if (scb->timeout_remaining < 0)
- continue;
- else
- return SERIAL_TIMEOUT;
- }
- else if (errno == EINTR)
- continue;
- else
- return SERIAL_ERROR; /* Got an error from read */
- }
-
- scb->bufcnt = status;
- scb->bufcnt--;
- scb->bufp = scb->buf;
- return *scb->bufp++;
- }
-}
-
-static int
-hardwire_readchar (serial_t scb, int timeout)
-{
- return generic_readchar (scb, timeout, do_hardwire_readchar);
-}
-
-