- fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
-}
-
-/* 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().
- */
-
-static int
-wait_for(scb, timeout)
- serial_t scb;
- int timeout;
-{
- scb->timeout_remaining = 0;
-
-#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). */
-
-static int
-hardwire_readchar(scb, timeout)
- serial_t scb;
- int timeout;
-{
- int status;
-
- if (scb->bufcnt-- > 0)
- return *scb->bufp++;
-
- while (1)
- {
- status = wait_for (scb, timeout);
-
- if (status < 0)
- return status;
-
- scb->bufcnt = read (scb->fd, scb->buf, BUFSIZ);
-
- if (scb->bufcnt <= 0)
- {
- if (scb->bufcnt == 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
- return SERIAL_TIMEOUT;
- }
- else if (errno == EINTR)
- continue;
- else
- return SERIAL_ERROR; /* Got an error from read */
- }
-
- scb->bufcnt--;
- scb->bufp = scb->buf;
- return *scb->bufp++;
- }