* Fix for PR 18665, from sky branch.
[binutils-gdb.git] / gdb / ser-unix.c
blob6b34d8b630cc8bb925bf38a4510435f5b7bf43cb
1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
2 Copyright 1992, 1993, 1994, 1998 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, Boston, MA 02111-1307, USA. */
20 #include "defs.h"
21 #include "serial.h"
22 #include <fcntl.h>
23 #include <sys/types.h>
24 #include "terminal.h"
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
29 #ifdef HAVE_TERMIOS
31 struct hardwire_ttystate
33 struct termios termios;
35 #endif /* termios */
37 #ifdef HAVE_TERMIO
39 /* It is believed that all systems which have added job control to SVR3
40 (e.g. sco) have also added termios. Even if not, trying to figure out
41 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
42 bewildering. So we don't attempt it. */
44 struct hardwire_ttystate
46 struct termio termio;
48 #endif /* termio */
50 #ifdef HAVE_SGTTY
51 /* Needed for the code which uses select(). We would include <sys/select.h>
52 too if it existed on all systems. */
53 #include <sys/time.h>
55 struct hardwire_ttystate
57 struct sgttyb sgttyb;
58 struct tchars tc;
59 struct ltchars ltc;
60 /* Line discipline flags. */
61 int lmode;
63 #endif /* sgtty */
65 static int hardwire_open PARAMS ((serial_t scb, const char *name));
66 static void hardwire_raw PARAMS ((serial_t scb));
67 static int wait_for PARAMS ((serial_t scb, int timeout));
68 static int hardwire_readchar PARAMS ((serial_t scb, int timeout));
69 static int rate_to_code PARAMS ((int rate));
70 static int hardwire_setbaudrate PARAMS ((serial_t scb, int rate));
71 static int hardwire_write PARAMS ((serial_t scb, const char *str, int len));
72 static void hardwire_close PARAMS ((serial_t scb));
73 static int get_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
74 static int set_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
75 static serial_ttystate hardwire_get_tty_state PARAMS ((serial_t scb));
76 static int hardwire_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
77 static int hardwire_noflush_set_tty_state PARAMS ((serial_t, serial_ttystate,
78 serial_ttystate));
79 static void hardwire_print_tty_state PARAMS ((serial_t, serial_ttystate));
80 static int hardwire_drain_output PARAMS ((serial_t));
81 static int hardwire_flush_output PARAMS ((serial_t));
82 static int hardwire_flush_input PARAMS ((serial_t));
83 static int hardwire_send_break PARAMS ((serial_t));
84 static int hardwire_setstopbits PARAMS ((serial_t, int));
86 void _initialize_ser_hardwire PARAMS ((void));
88 #ifdef __CYGWIN__
89 extern void (*ui_loop_hook) PARAMS ((int));
90 #endif
92 /* Open up a real live device for serial I/O */
94 static int
95 hardwire_open(scb, name)
96 serial_t scb;
97 const char *name;
99 scb->fd = open (name, O_RDWR);
100 if (scb->fd < 0)
101 return -1;
103 return 0;
106 static int
107 get_tty_state (scb, state)
108 serial_t scb;
109 struct hardwire_ttystate *state;
111 #ifdef HAVE_TERMIOS
112 if (tcgetattr(scb->fd, &state->termios) < 0)
113 return -1;
115 return 0;
116 #endif
118 #ifdef HAVE_TERMIO
119 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
120 return -1;
121 return 0;
122 #endif
124 #ifdef HAVE_SGTTY
125 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
126 return -1;
127 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
128 return -1;
129 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
130 return -1;
131 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
132 return -1;
134 return 0;
135 #endif
138 static int
139 set_tty_state(scb, state)
140 serial_t scb;
141 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(scb)
172 serial_t scb;
174 struct hardwire_ttystate *state;
176 state = (struct hardwire_ttystate *)xmalloc(sizeof *state);
178 if (get_tty_state(scb, state))
179 return NULL;
181 return (serial_ttystate)state;
184 static int
185 hardwire_set_tty_state(scb, ttystate)
186 serial_t scb;
187 serial_ttystate ttystate;
189 struct hardwire_ttystate *state;
191 state = (struct hardwire_ttystate *)ttystate;
193 return set_tty_state(scb, state);
196 static int
197 hardwire_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
198 serial_t scb;
199 serial_ttystate new_ttystate;
200 serial_ttystate old_ttystate;
202 struct hardwire_ttystate new_state;
203 #ifdef HAVE_SGTTY
204 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
205 #endif
207 new_state = *(struct hardwire_ttystate *)new_ttystate;
209 /* Don't change in or out of raw mode; we don't want to flush input.
210 termio and termios have no such restriction; for them flushing input
211 is separate from setting the attributes. */
213 #ifdef HAVE_SGTTY
214 if (state->sgttyb.sg_flags & RAW)
215 new_state.sgttyb.sg_flags |= RAW;
216 else
217 new_state.sgttyb.sg_flags &= ~RAW;
219 /* I'm not sure whether this is necessary; the manpage just mentions
220 RAW not CBREAK. */
221 if (state->sgttyb.sg_flags & CBREAK)
222 new_state.sgttyb.sg_flags |= CBREAK;
223 else
224 new_state.sgttyb.sg_flags &= ~CBREAK;
225 #endif
227 return set_tty_state (scb, &new_state);
230 static void
231 hardwire_print_tty_state (scb, ttystate)
232 serial_t scb;
233 serial_ttystate ttystate;
235 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
236 int i;
238 #ifdef HAVE_TERMIOS
239 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
240 state->termios.c_iflag, state->termios.c_oflag);
241 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x\n",
242 state->termios.c_cflag, state->termios.c_lflag);
243 #if 0
244 /* This not in POSIX, and is not really documented by those systems
245 which have it (at least not Sun). */
246 printf_filtered ("c_line = 0x%x.\n", state->termios.c_line);
247 #endif
248 printf_filtered ("c_cc: ");
249 for (i = 0; i < NCCS; i += 1)
250 printf_filtered ("0x%x ", state->termios.c_cc[i]);
251 printf_filtered ("\n");
252 #endif
254 #ifdef HAVE_TERMIO
255 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
256 state->termio.c_iflag, state->termio.c_oflag);
257 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
258 state->termio.c_cflag, state->termio.c_lflag,
259 state->termio.c_line);
260 printf_filtered ("c_cc: ");
261 for (i = 0; i < NCC; i += 1)
262 printf_filtered ("0x%x ", state->termio.c_cc[i]);
263 printf_filtered ("\n");
264 #endif
266 #ifdef HAVE_SGTTY
267 printf_filtered ("sgttyb.sg_flags = 0x%x.\n", state->sgttyb.sg_flags);
269 printf_filtered ("tchars: ");
270 for (i = 0; i < (int)sizeof (struct tchars); i++)
271 printf_filtered ("0x%x ", ((unsigned char *)&state->tc)[i]);
272 printf_filtered ("\n");
274 printf_filtered ("ltchars: ");
275 for (i = 0; i < (int)sizeof (struct ltchars); i++)
276 printf_filtered ("0x%x ", ((unsigned char *)&state->ltc)[i]);
277 printf_filtered ("\n");
279 printf_filtered ("lmode: 0x%x\n", state->lmode);
280 #endif
283 /* Wait for the output to drain away, as opposed to flushing (discarding) it */
285 static int
286 hardwire_drain_output (scb)
287 serial_t scb;
289 #ifdef HAVE_TERMIOS
290 return tcdrain (scb->fd);
291 #endif
293 #ifdef HAVE_TERMIO
294 return ioctl (scb->fd, TCSBRK, 1);
295 #endif
297 #ifdef HAVE_SGTTY
298 /* Get the current state and then restore it using TIOCSETP,
299 which should cause the output to drain and pending input
300 to be discarded. */
302 struct hardwire_ttystate state;
303 if (get_tty_state (scb, &state))
305 return (-1);
307 else
309 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
312 #endif
315 static int
316 hardwire_flush_output (scb)
317 serial_t scb;
319 #ifdef HAVE_TERMIOS
320 return tcflush (scb->fd, TCOFLUSH);
321 #endif
323 #ifdef HAVE_TERMIO
324 return ioctl (scb->fd, TCFLSH, 1);
325 #endif
327 #ifdef HAVE_SGTTY
328 /* This flushes both input and output, but we can't do better. */
329 return ioctl (scb->fd, TIOCFLUSH, 0);
330 #endif
333 static int
334 hardwire_flush_input (scb)
335 serial_t scb;
337 scb->bufcnt = 0;
338 scb->bufp = scb->buf;
340 #ifdef HAVE_TERMIOS
341 return tcflush (scb->fd, TCIFLUSH);
342 #endif
344 #ifdef HAVE_TERMIO
345 return ioctl (scb->fd, TCFLSH, 0);
346 #endif
348 #ifdef HAVE_SGTTY
349 /* This flushes both input and output, but we can't do better. */
350 return ioctl (scb->fd, TIOCFLUSH, 0);
351 #endif
354 static int
355 hardwire_send_break (scb)
356 serial_t scb;
358 #ifdef HAVE_TERMIOS
359 return tcsendbreak (scb->fd, 0);
360 #endif
362 #ifdef HAVE_TERMIO
363 return ioctl (scb->fd, TCSBRK, 0);
364 #endif
366 #ifdef HAVE_SGTTY
368 int status;
369 struct timeval timeout;
371 status = ioctl (scb->fd, TIOCSBRK, 0);
373 /* Can't use usleep; it doesn't exist in BSD 4.2. */
374 /* Note that if this select() is interrupted by a signal it will not wait
375 the full length of time. I think that is OK. */
376 timeout.tv_sec = 0;
377 timeout.tv_usec = 250000;
378 select (0, 0, 0, 0, &timeout);
379 status = ioctl (scb->fd, TIOCCBRK, 0);
380 return status;
382 #endif
385 static void
386 hardwire_raw(scb)
387 serial_t scb;
389 struct hardwire_ttystate state;
391 if (get_tty_state(scb, &state))
392 fprintf_unfiltered(gdb_stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
394 #ifdef HAVE_TERMIOS
395 state.termios.c_iflag = 0;
396 state.termios.c_oflag = 0;
397 state.termios.c_lflag = 0;
398 state.termios.c_cflag &= ~(CSIZE|PARENB);
399 state.termios.c_cflag |= CLOCAL | CS8;
400 state.termios.c_cc[VMIN] = 0;
401 state.termios.c_cc[VTIME] = 0;
402 #endif
404 #ifdef HAVE_TERMIO
405 state.termio.c_iflag = 0;
406 state.termio.c_oflag = 0;
407 state.termio.c_lflag = 0;
408 state.termio.c_cflag &= ~(CSIZE|PARENB);
409 state.termio.c_cflag |= CLOCAL | CS8;
410 state.termio.c_cc[VMIN] = 0;
411 state.termio.c_cc[VTIME] = 0;
412 #endif
414 #ifdef HAVE_SGTTY
415 state.sgttyb.sg_flags |= RAW | ANYP;
416 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
417 #endif
419 scb->current_timeout = 0;
421 if (set_tty_state (scb, &state))
422 fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
425 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
426 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
428 For termio{s}, we actually just setup VTIME if necessary, and let the
429 timeout occur in the read() in hardwire_read().
432 static int
433 wait_for(scb, timeout)
434 serial_t scb;
435 int timeout;
437 #ifndef __CYGWIN__
438 scb->timeout_remaining = 0;
439 #endif
441 #ifdef HAVE_SGTTY
443 struct timeval tv;
444 fd_set readfds;
446 FD_ZERO (&readfds);
448 tv.tv_sec = timeout;
449 tv.tv_usec = 0;
451 FD_SET(scb->fd, &readfds);
453 while (1)
455 int numfds;
457 if (timeout >= 0)
458 numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
459 else
460 numfds = select(scb->fd+1, &readfds, 0, 0, 0);
462 if (numfds <= 0)
463 if (numfds == 0)
464 return SERIAL_TIMEOUT;
465 else if (errno == EINTR)
466 continue;
467 else
468 return SERIAL_ERROR; /* Got an error from select or poll */
470 return 0;
473 #endif /* HAVE_SGTTY */
475 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
476 if (timeout == scb->current_timeout)
477 return 0;
479 scb->current_timeout = timeout;
482 struct hardwire_ttystate state;
484 if (get_tty_state(scb, &state))
485 fprintf_unfiltered(gdb_stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
487 #ifdef HAVE_TERMIOS
488 if (timeout < 0)
490 /* No timeout. */
491 state.termios.c_cc[VTIME] = 0;
492 state.termios.c_cc[VMIN] = 1;
494 else
496 state.termios.c_cc[VMIN] = 0;
497 state.termios.c_cc[VTIME] = timeout * 10;
498 if (state.termios.c_cc[VTIME] != timeout * 10)
501 /* If c_cc is an 8-bit signed character, we can't go
502 bigger than this. If it is always unsigned, we could use
503 25. */
505 scb->current_timeout = 12;
506 state.termios.c_cc[VTIME] = scb->current_timeout * 10;
507 scb->timeout_remaining = timeout - scb->current_timeout;
510 #endif
512 #ifdef HAVE_TERMIO
513 if (timeout < 0)
515 /* No timeout. */
516 state.termio.c_cc[VTIME] = 0;
517 state.termio.c_cc[VMIN] = 1;
519 else
521 state.termio.c_cc[VMIN] = 0;
522 state.termio.c_cc[VTIME] = timeout * 10;
523 if (state.termio.c_cc[VTIME] != timeout * 10)
525 /* If c_cc is an 8-bit signed character, we can't go
526 bigger than this. If it is always unsigned, we could use
527 25. */
529 scb->current_timeout = 12;
530 state.termio.c_cc[VTIME] = scb->current_timeout * 10;
531 scb->timeout_remaining = timeout - scb->current_timeout;
534 #endif
536 if (set_tty_state (scb, &state))
537 fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
539 return 0;
541 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
544 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
545 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
546 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
547 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
548 static int
549 hardwire_readchar (scb, timeout)
550 serial_t scb;
551 int timeout;
553 int status;
554 #ifdef __CYGWIN__
555 int t;
556 #endif
558 if (scb->bufcnt-- > 0)
559 return *scb->bufp++;
561 #ifdef __CYGWIN__
562 if (timeout > 0)
563 timeout++;
564 #endif
566 while (1)
568 #ifdef __CYGWIN__
569 t = timeout == 0 ? 0 : 1;
570 scb->timeout_remaining = timeout < 0 ? timeout : timeout - t;
571 status = wait_for (scb, t);
573 /* -2 means disable timer */
574 if (ui_loop_hook)
575 ui_loop_hook (-2);
576 #else
577 status = wait_for (scb, timeout);
578 #endif
579 if (status < 0)
580 return status;
582 scb->bufcnt = read (scb->fd, scb->buf, BUFSIZ);
584 if (scb->bufcnt <= 0)
586 if (scb->bufcnt == 0)
588 /* Zero characters means timeout (it could also be EOF, but
589 we don't (yet at least) distinguish). */
590 if (scb->timeout_remaining > 0)
592 timeout = scb->timeout_remaining;
593 continue;
595 #ifdef __CYGWIN__
596 else if (scb->timeout_remaining < 0)
597 continue;
598 #endif
599 else
600 return SERIAL_TIMEOUT;
602 else if (errno == EINTR)
603 continue;
604 else
605 return SERIAL_ERROR; /* Got an error from read */
608 scb->bufcnt--;
609 scb->bufp = scb->buf;
610 return *scb->bufp++;
614 #ifndef B19200
615 #define B19200 EXTA
616 #endif
618 #ifndef B38400
619 #define B38400 EXTB
620 #endif
622 /* Translate baud rates from integers to damn B_codes. Unix should
623 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
625 static struct
627 int rate;
628 int code;
630 baudtab[] =
632 {50, B50},
633 {75, B75},
634 {110, B110},
635 {134, B134},
636 {150, B150},
637 {200, B200},
638 {300, B300},
639 {600, B600},
640 {1200, B1200},
641 {1800, B1800},
642 {2400, B2400},
643 {4800, B4800},
644 {9600, B9600},
645 {19200, B19200},
646 {38400, B38400},
647 #ifdef B57600
648 {57600, B57600},
649 #endif
650 #ifdef B115200
651 {115200, B115200},
652 #endif
653 #ifdef B230400
654 {230400, B230400},
655 #endif
656 #ifdef B460800
657 {460800, B460800},
658 #endif
659 {-1, -1},
662 static int
663 rate_to_code(rate)
664 int rate;
666 int i;
668 for (i = 0; baudtab[i].rate != -1; i++)
669 if (rate == baudtab[i].rate)
670 return baudtab[i].code;
672 return -1;
675 static int
676 hardwire_setbaudrate(scb, rate)
677 serial_t scb;
678 int rate;
680 struct hardwire_ttystate state;
682 if (get_tty_state(scb, &state))
683 return -1;
685 #ifdef HAVE_TERMIOS
686 cfsetospeed (&state.termios, rate_to_code (rate));
687 cfsetispeed (&state.termios, rate_to_code (rate));
688 #endif
690 #ifdef HAVE_TERMIO
691 #ifndef CIBAUD
692 #define CIBAUD CBAUD
693 #endif
695 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
696 state.termio.c_cflag |= rate_to_code (rate);
697 #endif
699 #ifdef HAVE_SGTTY
700 state.sgttyb.sg_ispeed = rate_to_code (rate);
701 state.sgttyb.sg_ospeed = rate_to_code (rate);
702 #endif
704 return set_tty_state (scb, &state);
707 static int
708 hardwire_setstopbits(scb, num)
709 serial_t scb;
710 int num;
712 struct hardwire_ttystate state;
713 int newbit;
715 if (get_tty_state(scb, &state))
716 return -1;
718 switch (num)
720 case SERIAL_1_STOPBITS:
721 newbit = 0;
722 break;
723 case SERIAL_1_AND_A_HALF_STOPBITS:
724 case SERIAL_2_STOPBITS:
725 newbit = 1;
726 break;
727 default:
728 return 1;
731 #ifdef HAVE_TERMIOS
732 if (!newbit)
733 state.termios.c_cflag &= ~CSTOPB;
734 else
735 state.termios.c_cflag |= CSTOPB; /* two bits */
736 #endif
738 #ifdef HAVE_TERMIO
739 if (!newbit)
740 state.termio.c_cflag &= ~CSTOPB;
741 else
742 state.termio.c_cflag |= CSTOPB; /* two bits */
743 #endif
745 #ifdef HAVE_SGTTY
746 return 0; /* sgtty doesn't support this */
747 #endif
749 return set_tty_state (scb, &state);
752 static int
753 hardwire_write(scb, str, len)
754 serial_t scb;
755 const char *str;
756 int len;
758 int cc;
760 while (len > 0)
762 cc = write(scb->fd, str, len);
764 if (cc < 0)
765 return 1;
766 len -= cc;
767 str += cc;
769 return 0;
772 static void
773 hardwire_close(scb)
774 serial_t scb;
776 if (scb->fd < 0)
777 return;
779 close(scb->fd);
780 scb->fd = -1;
783 static struct serial_ops hardwire_ops =
785 "hardwire",
787 hardwire_open,
788 hardwire_close,
789 hardwire_readchar,
790 hardwire_write,
791 hardwire_flush_output,
792 hardwire_flush_input,
793 hardwire_send_break,
794 hardwire_raw,
795 hardwire_get_tty_state,
796 hardwire_set_tty_state,
797 hardwire_print_tty_state,
798 hardwire_noflush_set_tty_state,
799 hardwire_setbaudrate,
800 hardwire_setstopbits,
801 hardwire_drain_output, /* wait for output to drain */
804 void
805 _initialize_ser_hardwire ()
807 serial_add_interface (&hardwire_ops);