* testsuite/regress-demangle (failed test): Show result and
[binutils-gdb.git] / gdb / ser-unix.c
blob919c9fd90d9a6768032f59c6e5cb3ac3ff120b59
1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
2 Copyright 1992-1994, 1998-2000 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "defs.h"
22 #include "serial.h"
23 #include "ser-unix.h"
25 #include <fcntl.h>
26 #include <sys/types.h>
27 #include "terminal.h"
28 #include "gdb_wait.h"
29 #include <sys/socket.h>
30 #include <sys/time.h>
32 #include "gdb_string.h"
33 #include "event-loop.h"
35 #ifdef HAVE_TERMIOS
37 struct hardwire_ttystate
39 struct termios termios;
41 #endif /* termios */
43 #ifdef HAVE_TERMIO
45 /* It is believed that all systems which have added job control to SVR3
46 (e.g. sco) have also added termios. Even if not, trying to figure out
47 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
48 bewildering. So we don't attempt it. */
50 struct hardwire_ttystate
52 struct termio termio;
54 #endif /* termio */
56 #ifdef HAVE_SGTTY
57 struct hardwire_ttystate
59 struct sgttyb sgttyb;
60 struct tchars tc;
61 struct ltchars ltc;
62 /* Line discipline flags. */
63 int lmode;
65 #endif /* sgtty */
67 static int hardwire_open (serial_t scb, const char *name);
68 static void hardwire_raw (serial_t scb);
69 static int wait_for (serial_t scb, int timeout);
70 static int hardwire_readchar (serial_t scb, int timeout);
71 static int do_hardwire_readchar (serial_t scb, int timeout);
72 static int generic_readchar (serial_t scb, int timeout, int (*do_readchar) (serial_t scb, int timeout));
73 static int rate_to_code (int rate);
74 static int hardwire_setbaudrate (serial_t scb, int rate);
75 static void hardwire_close (serial_t scb);
76 static int get_tty_state (serial_t scb, struct hardwire_ttystate * state);
77 static int set_tty_state (serial_t scb, struct hardwire_ttystate * state);
78 static serial_ttystate hardwire_get_tty_state (serial_t scb);
79 static int hardwire_set_tty_state (serial_t scb, serial_ttystate state);
80 static int hardwire_noflush_set_tty_state (serial_t, serial_ttystate,
81 serial_ttystate);
82 static void hardwire_print_tty_state (serial_t, serial_ttystate, struct ui_file *);
83 static int hardwire_drain_output (serial_t);
84 static int hardwire_flush_output (serial_t);
85 static int hardwire_flush_input (serial_t);
86 static int hardwire_send_break (serial_t);
87 static int hardwire_setstopbits (serial_t, int);
89 static int do_unix_readchar (serial_t scb, int timeout);
90 static timer_handler_func push_event;
91 static handler_func fd_event;
92 static void reschedule (serial_t scb);
94 void _initialize_ser_hardwire (void);
96 extern int (*ui_loop_hook) (int);
98 /* Open up a real live device for serial I/O */
100 static int
101 hardwire_open (serial_t scb, const char *name)
103 scb->fd = open (name, O_RDWR);
104 if (scb->fd < 0)
105 return -1;
107 return 0;
110 static int
111 get_tty_state (serial_t scb, struct hardwire_ttystate *state)
113 #ifdef HAVE_TERMIOS
114 if (tcgetattr (scb->fd, &state->termios) < 0)
115 return -1;
117 return 0;
118 #endif
120 #ifdef HAVE_TERMIO
121 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
122 return -1;
123 return 0;
124 #endif
126 #ifdef HAVE_SGTTY
127 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
128 return -1;
129 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
130 return -1;
131 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
132 return -1;
133 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
134 return -1;
136 return 0;
137 #endif
140 static int
141 set_tty_state (serial_t scb, struct hardwire_ttystate *state)
143 #ifdef HAVE_TERMIOS
144 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
145 return -1;
147 return 0;
148 #endif
150 #ifdef HAVE_TERMIO
151 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
152 return -1;
153 return 0;
154 #endif
156 #ifdef HAVE_SGTTY
157 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
158 return -1;
159 if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
160 return -1;
161 if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
162 return -1;
163 if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
164 return -1;
166 return 0;
167 #endif
170 static serial_ttystate
171 hardwire_get_tty_state (serial_t scb)
173 struct hardwire_ttystate *state;
175 state = (struct hardwire_ttystate *) xmalloc (sizeof *state);
177 if (get_tty_state (scb, state))
178 return NULL;
180 return (serial_ttystate) state;
183 static int
184 hardwire_set_tty_state (serial_t scb, serial_ttystate ttystate)
186 struct hardwire_ttystate *state;
188 state = (struct hardwire_ttystate *) ttystate;
190 return set_tty_state (scb, state);
193 static int
194 hardwire_noflush_set_tty_state (serial_t scb,
195 serial_ttystate new_ttystate,
196 serial_ttystate old_ttystate)
198 struct hardwire_ttystate new_state;
199 #ifdef HAVE_SGTTY
200 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
201 #endif
203 new_state = *(struct hardwire_ttystate *) new_ttystate;
205 /* Don't change in or out of raw mode; we don't want to flush input.
206 termio and termios have no such restriction; for them flushing input
207 is separate from setting the attributes. */
209 #ifdef HAVE_SGTTY
210 if (state->sgttyb.sg_flags & RAW)
211 new_state.sgttyb.sg_flags |= RAW;
212 else
213 new_state.sgttyb.sg_flags &= ~RAW;
215 /* I'm not sure whether this is necessary; the manpage just mentions
216 RAW not CBREAK. */
217 if (state->sgttyb.sg_flags & CBREAK)
218 new_state.sgttyb.sg_flags |= CBREAK;
219 else
220 new_state.sgttyb.sg_flags &= ~CBREAK;
221 #endif
223 return set_tty_state (scb, &new_state);
226 static void
227 hardwire_print_tty_state (serial_t scb,
228 serial_ttystate ttystate,
229 struct ui_file *stream)
231 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
232 int i;
234 #ifdef HAVE_TERMIOS
235 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
236 (int) state->termios.c_iflag,
237 (int) state->termios.c_oflag);
238 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n",
239 (int) state->termios.c_cflag,
240 (int) state->termios.c_lflag);
241 #if 0
242 /* This not in POSIX, and is not really documented by those systems
243 which have it (at least not Sun). */
244 fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line);
245 #endif
246 fprintf_filtered (stream, "c_cc: ");
247 for (i = 0; i < NCCS; i += 1)
248 fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]);
249 fprintf_filtered (stream, "\n");
250 #endif
252 #ifdef HAVE_TERMIO
253 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
254 state->termio.c_iflag, state->termio.c_oflag);
255 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
256 state->termio.c_cflag, state->termio.c_lflag,
257 state->termio.c_line);
258 fprintf_filtered (stream, "c_cc: ");
259 for (i = 0; i < NCC; i += 1)
260 fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]);
261 fprintf_filtered (stream, "\n");
262 #endif
264 #ifdef HAVE_SGTTY
265 fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n",
266 state->sgttyb.sg_flags);
268 fprintf_filtered (stream, "tchars: ");
269 for (i = 0; i < (int) sizeof (struct tchars); i++)
270 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]);
271 fprintf_filtered ("\n");
273 fprintf_filtered (stream, "ltchars: ");
274 for (i = 0; i < (int) sizeof (struct ltchars); i++)
275 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]);
276 fprintf_filtered (stream, "\n");
278 fprintf_filtered (stream, "lmode: 0x%x\n", state->lmode);
279 #endif
282 /* Wait for the output to drain away, as opposed to flushing (discarding) it */
284 static int
285 hardwire_drain_output (serial_t scb)
287 #ifdef HAVE_TERMIOS
288 return tcdrain (scb->fd);
289 #endif
291 #ifdef HAVE_TERMIO
292 return ioctl (scb->fd, TCSBRK, 1);
293 #endif
295 #ifdef HAVE_SGTTY
296 /* Get the current state and then restore it using TIOCSETP,
297 which should cause the output to drain and pending input
298 to be discarded. */
300 struct hardwire_ttystate state;
301 if (get_tty_state (scb, &state))
303 return (-1);
305 else
307 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
310 #endif
313 static int
314 hardwire_flush_output (serial_t scb)
316 #ifdef HAVE_TERMIOS
317 return tcflush (scb->fd, TCOFLUSH);
318 #endif
320 #ifdef HAVE_TERMIO
321 return ioctl (scb->fd, TCFLSH, 1);
322 #endif
324 #ifdef HAVE_SGTTY
325 /* This flushes both input and output, but we can't do better. */
326 return ioctl (scb->fd, TIOCFLUSH, 0);
327 #endif
330 static int
331 hardwire_flush_input (serial_t scb)
333 ser_unix_flush_input (scb);
335 #ifdef HAVE_TERMIOS
336 return tcflush (scb->fd, TCIFLUSH);
337 #endif
339 #ifdef HAVE_TERMIO
340 return ioctl (scb->fd, TCFLSH, 0);
341 #endif
343 #ifdef HAVE_SGTTY
344 /* This flushes both input and output, but we can't do better. */
345 return ioctl (scb->fd, TIOCFLUSH, 0);
346 #endif
349 static int
350 hardwire_send_break (serial_t scb)
352 #ifdef HAVE_TERMIOS
353 return tcsendbreak (scb->fd, 0);
354 #endif
356 #ifdef HAVE_TERMIO
357 return ioctl (scb->fd, TCSBRK, 0);
358 #endif
360 #ifdef HAVE_SGTTY
362 int status;
363 struct timeval timeout;
365 status = ioctl (scb->fd, TIOCSBRK, 0);
367 /* Can't use usleep; it doesn't exist in BSD 4.2. */
368 /* Note that if this select() is interrupted by a signal it will not wait
369 the full length of time. I think that is OK. */
370 timeout.tv_sec = 0;
371 timeout.tv_usec = 250000;
372 select (0, 0, 0, 0, &timeout);
373 status = ioctl (scb->fd, TIOCCBRK, 0);
374 return status;
376 #endif
379 static void
380 hardwire_raw (serial_t scb)
382 struct hardwire_ttystate state;
384 if (get_tty_state (scb, &state))
385 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
387 #ifdef HAVE_TERMIOS
388 state.termios.c_iflag = 0;
389 state.termios.c_oflag = 0;
390 state.termios.c_lflag = 0;
391 state.termios.c_cflag &= ~(CSIZE | PARENB);
392 state.termios.c_cflag |= CLOCAL | CS8;
393 state.termios.c_cc[VMIN] = 0;
394 state.termios.c_cc[VTIME] = 0;
395 #endif
397 #ifdef HAVE_TERMIO
398 state.termio.c_iflag = 0;
399 state.termio.c_oflag = 0;
400 state.termio.c_lflag = 0;
401 state.termio.c_cflag &= ~(CSIZE | PARENB);
402 state.termio.c_cflag |= CLOCAL | CS8;
403 state.termio.c_cc[VMIN] = 0;
404 state.termio.c_cc[VTIME] = 0;
405 #endif
407 #ifdef HAVE_SGTTY
408 state.sgttyb.sg_flags |= RAW | ANYP;
409 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
410 #endif
412 scb->current_timeout = 0;
414 if (set_tty_state (scb, &state))
415 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
418 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
419 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
421 For termio{s}, we actually just setup VTIME if necessary, and let the
422 timeout occur in the read() in hardwire_read().
425 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
426 ser_unix*() until the old TERMIOS/SGTTY/... timer code has been
427 flushed. . */
429 /* NOTE: cagney/1999-09-30: Much of the code below is dead. The only
430 possible values of the TIMEOUT parameter are ONE and ZERO.
431 Consequently all the code that tries to handle the possability of
432 an overflowed timer is unnecessary. */
434 static int
435 wait_for (serial_t scb, int timeout)
437 #ifdef HAVE_SGTTY
439 struct timeval tv;
440 fd_set readfds;
442 FD_ZERO (&readfds);
444 tv.tv_sec = timeout;
445 tv.tv_usec = 0;
447 FD_SET (scb->fd, &readfds);
449 while (1)
451 int numfds;
453 if (timeout >= 0)
454 numfds = select (scb->fd + 1, &readfds, 0, 0, &tv);
455 else
456 numfds = select (scb->fd + 1, &readfds, 0, 0, 0);
458 if (numfds <= 0)
459 if (numfds == 0)
460 return SERIAL_TIMEOUT;
461 else if (errno == EINTR)
462 continue;
463 else
464 return SERIAL_ERROR; /* Got an error from select or poll */
466 return 0;
469 #endif /* HAVE_SGTTY */
471 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
472 if (timeout == scb->current_timeout)
473 return 0;
475 scb->current_timeout = timeout;
478 struct hardwire_ttystate state;
480 if (get_tty_state (scb, &state))
481 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
483 #ifdef HAVE_TERMIOS
484 if (timeout < 0)
486 /* No timeout. */
487 state.termios.c_cc[VTIME] = 0;
488 state.termios.c_cc[VMIN] = 1;
490 else
492 state.termios.c_cc[VMIN] = 0;
493 state.termios.c_cc[VTIME] = timeout * 10;
494 if (state.termios.c_cc[VTIME] != timeout * 10)
497 /* If c_cc is an 8-bit signed character, we can't go
498 bigger than this. If it is always unsigned, we could use
499 25. */
501 scb->current_timeout = 12;
502 state.termios.c_cc[VTIME] = scb->current_timeout * 10;
503 scb->timeout_remaining = timeout - scb->current_timeout;
506 #endif
508 #ifdef HAVE_TERMIO
509 if (timeout < 0)
511 /* No timeout. */
512 state.termio.c_cc[VTIME] = 0;
513 state.termio.c_cc[VMIN] = 1;
515 else
517 state.termio.c_cc[VMIN] = 0;
518 state.termio.c_cc[VTIME] = timeout * 10;
519 if (state.termio.c_cc[VTIME] != timeout * 10)
521 /* If c_cc is an 8-bit signed character, we can't go
522 bigger than this. If it is always unsigned, we could use
523 25. */
525 scb->current_timeout = 12;
526 state.termio.c_cc[VTIME] = scb->current_timeout * 10;
527 scb->timeout_remaining = timeout - scb->current_timeout;
530 #endif
532 if (set_tty_state (scb, &state))
533 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
535 return 0;
537 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
540 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
541 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
542 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
543 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
545 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
546 ser_unix*() until the old TERMIOS/SGTTY/... timer code has been
547 flushed. */
549 /* NOTE: cagney/1999-09-16: This function is not identical to
550 ser_unix_readchar() as part of replacing it with ser_unix*()
551 merging will be required - this code handles the case where read()
552 times out due to no data while ser_unix_readchar() doesn't expect
553 that. */
555 static int
556 do_hardwire_readchar (serial_t scb, int timeout)
558 int status, delta;
559 int detach = 0;
561 if (timeout > 0)
562 timeout++;
564 /* We have to be able to keep the GUI alive here, so we break the original
565 timeout into steps of 1 second, running the "keep the GUI alive" hook
566 each time through the loop.
567 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
568 will only go through the loop once. */
570 delta = (timeout == 0 ? 0 : 1);
571 while (1)
574 /* N.B. The UI may destroy our world (for instance by calling
575 remote_stop,) in which case we want to get out of here as
576 quickly as possible. It is not safe to touch scb, since
577 someone else might have freed it. The ui_loop_hook signals that
578 we should exit by returning 1. */
580 if (ui_loop_hook)
581 detach = ui_loop_hook (0);
583 if (detach)
584 return SERIAL_TIMEOUT;
586 scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
587 status = wait_for (scb, delta);
589 if (status < 0)
590 return status;
592 status = read (scb->fd, scb->buf, BUFSIZ);
594 if (status <= 0)
596 if (status == 0)
598 /* Zero characters means timeout (it could also be EOF, but
599 we don't (yet at least) distinguish). */
600 if (scb->timeout_remaining > 0)
602 timeout = scb->timeout_remaining;
603 continue;
605 else if (scb->timeout_remaining < 0)
606 continue;
607 else
608 return SERIAL_TIMEOUT;
610 else if (errno == EINTR)
611 continue;
612 else
613 return SERIAL_ERROR; /* Got an error from read */
616 scb->bufcnt = status;
617 scb->bufcnt--;
618 scb->bufp = scb->buf;
619 return *scb->bufp++;
623 static int
624 hardwire_readchar (serial_t scb, int timeout)
626 return generic_readchar (scb, timeout, do_hardwire_readchar);
630 #ifndef B19200
631 #define B19200 EXTA
632 #endif
634 #ifndef B38400
635 #define B38400 EXTB
636 #endif
638 /* Translate baud rates from integers to damn B_codes. Unix should
639 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
641 static struct
643 int rate;
644 int code;
646 baudtab[] =
649 50, B50
653 75, B75
657 110, B110
661 134, B134
665 150, B150
669 200, B200
673 300, B300
677 600, B600
681 1200, B1200
685 1800, B1800
689 2400, B2400
693 4800, B4800
697 9600, B9600
701 19200, B19200
705 38400, B38400
708 #ifdef B57600
710 57600, B57600
713 #endif
714 #ifdef B115200
716 115200, B115200
719 #endif
720 #ifdef B230400
722 230400, B230400
725 #endif
726 #ifdef B460800
728 460800, B460800
731 #endif
733 -1, -1
738 static int
739 rate_to_code (int rate)
741 int i;
743 for (i = 0; baudtab[i].rate != -1; i++)
744 if (rate == baudtab[i].rate)
745 return baudtab[i].code;
747 return -1;
750 static int
751 hardwire_setbaudrate (serial_t scb, int rate)
753 struct hardwire_ttystate state;
755 if (get_tty_state (scb, &state))
756 return -1;
758 #ifdef HAVE_TERMIOS
759 cfsetospeed (&state.termios, rate_to_code (rate));
760 cfsetispeed (&state.termios, rate_to_code (rate));
761 #endif
763 #ifdef HAVE_TERMIO
764 #ifndef CIBAUD
765 #define CIBAUD CBAUD
766 #endif
768 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
769 state.termio.c_cflag |= rate_to_code (rate);
770 #endif
772 #ifdef HAVE_SGTTY
773 state.sgttyb.sg_ispeed = rate_to_code (rate);
774 state.sgttyb.sg_ospeed = rate_to_code (rate);
775 #endif
777 return set_tty_state (scb, &state);
780 static int
781 hardwire_setstopbits (scb, num)
782 serial_t scb;
783 int num;
785 struct hardwire_ttystate state;
786 int newbit;
788 if (get_tty_state (scb, &state))
789 return -1;
791 switch (num)
793 case SERIAL_1_STOPBITS:
794 newbit = 0;
795 break;
796 case SERIAL_1_AND_A_HALF_STOPBITS:
797 case SERIAL_2_STOPBITS:
798 newbit = 1;
799 break;
800 default:
801 return 1;
804 #ifdef HAVE_TERMIOS
805 if (!newbit)
806 state.termios.c_cflag &= ~CSTOPB;
807 else
808 state.termios.c_cflag |= CSTOPB; /* two bits */
809 #endif
811 #ifdef HAVE_TERMIO
812 if (!newbit)
813 state.termio.c_cflag &= ~CSTOPB;
814 else
815 state.termio.c_cflag |= CSTOPB; /* two bits */
816 #endif
818 #ifdef HAVE_SGTTY
819 return 0; /* sgtty doesn't support this */
820 #endif
822 return set_tty_state (scb, &state);
825 static void
826 hardwire_close (serial_t scb)
828 if (scb->fd < 0)
829 return;
831 close (scb->fd);
832 scb->fd = -1;
836 /* Generic operations used by all UNIX/FD based serial interfaces. */
838 serial_ttystate
839 ser_unix_nop_get_tty_state (serial_t scb)
841 /* allocate a dummy */
842 return (serial_ttystate) XMALLOC (int);
846 ser_unix_nop_set_tty_state (serial_t scb, serial_ttystate ttystate)
848 return 0;
851 void
852 ser_unix_nop_raw (serial_t scb)
854 return; /* Always in raw mode */
857 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
858 otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
861 ser_unix_wait_for (serial_t scb, int timeout)
863 int numfds;
864 struct timeval tv;
865 fd_set readfds, exceptfds;
867 FD_ZERO (&readfds);
868 FD_ZERO (&exceptfds);
870 tv.tv_sec = timeout;
871 tv.tv_usec = 0;
873 FD_SET (scb->fd, &readfds);
874 FD_SET (scb->fd, &exceptfds);
876 while (1)
878 if (timeout >= 0)
879 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
880 else
881 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, 0);
883 if (numfds <= 0)
885 if (numfds == 0)
886 return SERIAL_TIMEOUT;
887 else if (errno == EINTR)
888 continue;
889 else
890 return SERIAL_ERROR; /* Got an error from select or poll */
893 return 0;
897 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
898 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
899 char if successful. Returns -2 if timeout expired, EOF if line dropped
900 dead, or -3 for any other error (see errno in that case). */
902 static int
903 do_unix_readchar (serial_t scb, int timeout)
905 int status;
906 int delta;
908 /* We have to be able to keep the GUI alive here, so we break the original
909 timeout into steps of 1 second, running the "keep the GUI alive" hook
910 each time through the loop.
912 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
913 will only go through the loop once. */
915 delta = (timeout == 0 ? 0 : 1);
916 while (1)
919 /* N.B. The UI may destroy our world (for instance by calling
920 remote_stop,) in which case we want to get out of here as
921 quickly as possible. It is not safe to touch scb, since
922 someone else might have freed it. The ui_loop_hook signals that
923 we should exit by returning 1. */
925 if (ui_loop_hook)
927 if (ui_loop_hook (0))
928 return SERIAL_TIMEOUT;
931 status = ser_unix_wait_for (scb, delta);
932 timeout -= delta;
934 /* If we got a character or an error back from wait_for, then we can
935 break from the loop before the timeout is completed. */
937 if (status != SERIAL_TIMEOUT)
939 break;
942 /* If we have exhausted the original timeout, then generate
943 a SERIAL_TIMEOUT, and pass it out of the loop. */
945 else if (timeout == 0)
947 status = SERIAL_TIMEOUT;
948 break;
952 if (status < 0)
953 return status;
955 while (1)
957 status = read (scb->fd, scb->buf, BUFSIZ);
958 if (status != -1 || errno != EINTR)
959 break;
962 if (status <= 0)
964 if (status == 0)
965 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
966 distinguish between EOF & timeouts
967 someday] */
968 else
969 return SERIAL_ERROR; /* Got an error from read */
972 scb->bufcnt = status;
973 scb->bufcnt--;
974 scb->bufp = scb->buf;
975 return *scb->bufp++;
978 /* Perform operations common to both old and new readchar. */
980 /* Return the next character from the input FIFO. If the FIFO is
981 empty, call the SERIAL specific routine to try and read in more
982 characters.
984 Initially data from the input FIFO is returned (fd_event()
985 pre-reads the input into that FIFO. Once that has been emptied,
986 further data is obtained by polling the input FD using the device
987 specific readchar() function. Note: reschedule() is called after
988 every read. This is because there is no guarentee that the lower
989 level fd_event() poll_event() code (which also calls reschedule())
990 will be called. */
992 static int
993 generic_readchar (serial_t scb, int timeout,
994 int (do_readchar) (serial_t scb, int timeout))
996 int ch;
997 if (scb->bufcnt > 0)
999 ch = *scb->bufp;
1000 scb->bufcnt--;
1001 scb->bufp++;
1003 else if (scb->bufcnt < 0)
1005 /* Some errors/eof are are sticky. */
1006 ch = scb->bufcnt;
1008 else
1010 ch = do_readchar (scb, timeout);
1011 if (ch < 0)
1013 switch ((enum serial_rc) ch)
1015 case SERIAL_EOF:
1016 case SERIAL_ERROR:
1017 /* Make the error/eof stick. */
1018 scb->bufcnt = ch;
1019 break;
1020 case SERIAL_TIMEOUT:
1021 scb->bufcnt = 0;
1022 break;
1026 reschedule (scb);
1027 return ch;
1031 ser_unix_readchar (serial_t scb, int timeout)
1033 return generic_readchar (scb, timeout, do_unix_readchar);
1037 ser_unix_nop_noflush_set_tty_state (serial_t scb,
1038 serial_ttystate new_ttystate,
1039 serial_ttystate old_ttystate)
1041 return 0;
1044 void
1045 ser_unix_nop_print_tty_state (serial_t scb,
1046 serial_ttystate ttystate,
1047 struct ui_file *stream)
1049 /* Nothing to print. */
1050 return;
1054 ser_unix_nop_setbaudrate (serial_t scb, int rate)
1056 return 0; /* Never fails! */
1060 ser_unix_nop_setstopbits (serial_t scb, int num)
1062 return 0; /* Never fails! */
1066 ser_unix_write (serial_t scb, const char *str, int len)
1068 int cc;
1070 while (len > 0)
1072 cc = write (scb->fd, str, len);
1074 if (cc < 0)
1075 return 1;
1076 len -= cc;
1077 str += cc;
1079 return 0;
1083 ser_unix_nop_flush_output (serial_t scb)
1085 return 0;
1089 ser_unix_flush_input (serial_t scb)
1091 if (scb->bufcnt >= 0)
1093 scb->bufcnt = 0;
1094 scb->bufp = scb->buf;
1095 return 0;
1097 else
1098 return SERIAL_ERROR;
1102 ser_unix_nop_send_break (serial_t scb)
1104 return 0;
1108 ser_unix_nop_drain_output (serial_t scb)
1110 return 0;
1115 /* Event handling for ASYNC serial code.
1117 At any time the SERIAL device either: has an empty FIFO and is
1118 waiting on a FD event; or has a non-empty FIFO/error condition and
1119 is constantly scheduling timer events.
1121 ASYNC only stops pestering its client when it is de-async'ed or it
1122 is told to go away. */
1124 /* Value of scb->async_state: */
1125 enum {
1126 /* >= 0 (TIMER_SCHEDULED) */
1127 /* The ID of the currently scheduled timer event. This state is
1128 rarely encountered. Timer events are one-off so as soon as the
1129 event is delivered the state is shanged to NOTHING_SCHEDULED. */
1130 FD_SCHEDULED = -1,
1131 /* The fd_event() handler is scheduled. It is called when ever the
1132 file descriptor becomes ready. */
1133 NOTHING_SCHEDULED = -2
1134 /* Either no task is scheduled (just going into ASYNC mode) or a
1135 timer event has just gone off and the current state has been
1136 forced into nothing scheduled. */
1139 /* Identify and schedule the next ASYNC task based on scb->async_state
1140 and scb->buf* (the input FIFO). A state machine is used to avoid
1141 the need to make redundant calls into the event-loop - the next
1142 scheduled task is only changed when needed. */
1144 static void
1145 reschedule (serial_t scb)
1147 if (SERIAL_IS_ASYNC_P (scb))
1149 int next_state;
1150 switch (scb->async_state)
1152 case FD_SCHEDULED:
1153 if (scb->bufcnt == 0)
1154 next_state = FD_SCHEDULED;
1155 else
1157 delete_file_handler (scb->fd);
1158 next_state = create_timer (0, push_event, scb);
1160 break;
1161 case NOTHING_SCHEDULED:
1162 if (scb->bufcnt == 0)
1164 add_file_handler (scb->fd, fd_event, scb);
1165 next_state = FD_SCHEDULED;
1167 else
1169 next_state = create_timer (0, push_event, scb);
1171 break;
1172 default: /* TIMER SCHEDULED */
1173 if (scb->bufcnt == 0)
1175 delete_timer (scb->async_state);
1176 add_file_handler (scb->fd, fd_event, scb);
1177 next_state = FD_SCHEDULED;
1179 else
1180 next_state = scb->async_state;
1181 break;
1183 if (SERIAL_DEBUG_P (scb))
1185 switch (next_state)
1187 case FD_SCHEDULED:
1188 if (scb->async_state != FD_SCHEDULED)
1189 fprintf_unfiltered (gdb_stdlog, "[fd%d->fd-scheduled]\n",
1190 scb->fd);
1191 break;
1192 default: /* TIMER SCHEDULED */
1193 if (scb->async_state == FD_SCHEDULED)
1194 fprintf_unfiltered (gdb_stdlog, "[fd%d->timer-scheduled]\n",
1195 scb->fd);
1196 break;
1199 scb->async_state = next_state;
1203 /* FD_EVENT: This is scheduled when the input FIFO is empty (and there
1204 is no pending error). As soon as data arrives, it is read into the
1205 input FIFO and the client notified. The client should then drain
1206 the FIFO using readchar(). If the FIFO isn't immediatly emptied,
1207 push_event() is used to nag the client until it is. */
1209 static void
1210 fd_event (int error, void *context)
1212 serial_t scb = context;
1213 if (error != 0)
1215 scb->bufcnt = SERIAL_ERROR;
1217 else if (scb->bufcnt == 0)
1219 /* Prime the input FIFO. The readchar() function is used to
1220 pull characters out of the buffer. See also
1221 generic_readchar(). */
1222 int nr;
1225 nr = read (scb->fd, scb->buf, BUFSIZ);
1227 while (nr == -1 && errno == EINTR);
1228 if (nr == 0)
1230 scb->bufcnt = SERIAL_EOF;
1232 else if (nr > 0)
1234 scb->bufcnt = nr;
1235 scb->bufp = scb->buf;
1237 else
1239 scb->bufcnt = SERIAL_ERROR;
1242 scb->async_handler (scb, scb->async_context);
1243 reschedule (scb);
1246 /* PUSH_EVENT: The input FIFO is non-empty (or there is a pending
1247 error). Nag the client until all the data has been read. In the
1248 case of errors, the client will need to close or de-async the
1249 device before naging stops. */
1251 static void
1252 push_event (void *context)
1254 serial_t scb = context;
1255 scb->async_state = NOTHING_SCHEDULED; /* Timers are one-off */
1256 scb->async_handler (scb, scb->async_context);
1257 /* re-schedule */
1258 reschedule (scb);
1261 /* Put the SERIAL device into/out-of ASYNC mode. */
1263 void
1264 ser_unix_async (serial_t scb,
1265 int async_p)
1267 if (async_p)
1269 /* Force a re-schedule. */
1270 scb->async_state = NOTHING_SCHEDULED;
1271 if (SERIAL_DEBUG_P (scb))
1272 fprintf_unfiltered (gdb_stdlog, "[fd%d->asynchronous]\n",
1273 scb->fd);
1274 reschedule (scb);
1276 else
1278 if (SERIAL_DEBUG_P (scb))
1279 fprintf_unfiltered (gdb_stdlog, "[fd%d->synchronous]\n",
1280 scb->fd);
1281 /* De-schedule what ever tasks are currently scheduled. */
1282 switch (scb->async_state)
1284 case FD_SCHEDULED:
1285 delete_file_handler (scb->fd);
1286 break;
1287 NOTHING_SCHEDULED:
1288 break;
1289 default: /* TIMER SCHEDULED */
1290 delete_timer (scb->async_state);
1291 break;
1296 void
1297 _initialize_ser_hardwire (void)
1299 struct serial_ops *ops = XMALLOC (struct serial_ops);
1300 memset (ops, sizeof (struct serial_ops), 0);
1301 ops->name = "hardwire";
1302 ops->next = 0;
1303 ops->open = hardwire_open;
1304 ops->close = hardwire_close;
1305 /* FIXME: Don't replace this with the equivalent ser_unix*() until
1306 the old TERMIOS/SGTTY/... timer code has been flushed. cagney
1307 1999-09-16. */
1308 ops->readchar = hardwire_readchar;
1309 ops->write = ser_unix_write;
1310 ops->flush_output = hardwire_flush_output;
1311 ops->flush_input = hardwire_flush_input;
1312 ops->send_break = hardwire_send_break;
1313 ops->go_raw = hardwire_raw;
1314 ops->get_tty_state = hardwire_get_tty_state;
1315 ops->set_tty_state = hardwire_set_tty_state;
1316 ops->print_tty_state = hardwire_print_tty_state;
1317 ops->noflush_set_tty_state = hardwire_noflush_set_tty_state;
1318 ops->setbaudrate = hardwire_setbaudrate;
1319 ops->setstopbits = hardwire_setstopbits;
1320 ops->drain_output = hardwire_drain_output;
1321 ops->async = ser_unix_async;
1322 serial_add_interface (ops);