]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/ser-unix.c
import gdb-1999-09-21
[thirdparty/binutils-gdb.git] / gdb / ser-unix.c
1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
2 Copyright 1992, 1993, 1994, 1998-1999 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
20
21 #include "defs.h"
22 #include "serial.h"
23 #include "ser-unix.h"
24
25 #include <fcntl.h>
26 #include <sys/types.h>
27 #include "terminal.h"
28 #ifdef HAVE_SYS_WAIT_H
29 #include <sys/wait.h>
30 #endif
31 #include <sys/socket.h>
32 #include <sys/time.h>
33
34 #include "gdb_string.h"
35 #include "event-loop.h"
36
37
38 #ifdef HAVE_TERMIOS
39
40 struct hardwire_ttystate
41 {
42 struct termios termios;
43 };
44 #endif /* termios */
45
46 #ifdef HAVE_TERMIO
47
48 /* It is believed that all systems which have added job control to SVR3
49 (e.g. sco) have also added termios. Even if not, trying to figure out
50 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
51 bewildering. So we don't attempt it. */
52
53 struct hardwire_ttystate
54 {
55 struct termio termio;
56 };
57 #endif /* termio */
58
59 #ifdef HAVE_SGTTY
60 struct hardwire_ttystate
61 {
62 struct sgttyb sgttyb;
63 struct tchars tc;
64 struct ltchars ltc;
65 /* Line discipline flags. */
66 int lmode;
67 };
68 #endif /* sgtty */
69
70 static int hardwire_open (serial_t scb, const char *name);
71 static void hardwire_raw (serial_t scb);
72 static int wait_for (serial_t scb, int timeout);
73 static int hardwire_readchar (serial_t scb, int timeout);
74 static int rate_to_code (int rate);
75 static int hardwire_setbaudrate (serial_t scb, int rate);
76 static int hardwire_write (serial_t scb, const char *str, int len);
77 static void hardwire_close (serial_t scb);
78 static int get_tty_state (serial_t scb, struct hardwire_ttystate * state);
79 static int set_tty_state (serial_t scb, struct hardwire_ttystate * state);
80 static serial_ttystate hardwire_get_tty_state (serial_t scb);
81 static int hardwire_set_tty_state (serial_t scb, serial_ttystate state);
82 static int hardwire_noflush_set_tty_state (serial_t, serial_ttystate,
83 serial_ttystate);
84 static void hardwire_print_tty_state (serial_t, serial_ttystate, struct gdb_file *);
85 static int hardwire_drain_output (serial_t);
86 static int hardwire_flush_output (serial_t);
87 static int hardwire_flush_input (serial_t);
88 static int hardwire_send_break (serial_t);
89 static int hardwire_setstopbits (serial_t, int);
90
91 void _initialize_ser_hardwire (void);
92
93 extern int (*ui_loop_hook) (int);
94
95 /* Open up a real live device for serial I/O */
96
97 static int
98 hardwire_open (serial_t scb, const char *name)
99 {
100 scb->fd = open (name, O_RDWR);
101 if (scb->fd < 0)
102 return -1;
103
104 return 0;
105 }
106
107 static int
108 get_tty_state (serial_t scb, struct hardwire_ttystate *state)
109 {
110 #ifdef HAVE_TERMIOS
111 if (tcgetattr (scb->fd, &state->termios) < 0)
112 return -1;
113
114 return 0;
115 #endif
116
117 #ifdef HAVE_TERMIO
118 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
119 return -1;
120 return 0;
121 #endif
122
123 #ifdef HAVE_SGTTY
124 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
125 return -1;
126 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
127 return -1;
128 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
129 return -1;
130 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
131 return -1;
132
133 return 0;
134 #endif
135 }
136
137 static int
138 set_tty_state (serial_t scb, struct hardwire_ttystate *state)
139 {
140 #ifdef HAVE_TERMIOS
141 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
142 return -1;
143
144 return 0;
145 #endif
146
147 #ifdef HAVE_TERMIO
148 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
149 return -1;
150 return 0;
151 #endif
152
153 #ifdef HAVE_SGTTY
154 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
155 return -1;
156 if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
157 return -1;
158 if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
159 return -1;
160 if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
161 return -1;
162
163 return 0;
164 #endif
165 }
166
167 static serial_ttystate
168 hardwire_get_tty_state (serial_t scb)
169 {
170 struct hardwire_ttystate *state;
171
172 state = (struct hardwire_ttystate *) xmalloc (sizeof *state);
173
174 if (get_tty_state (scb, state))
175 return NULL;
176
177 return (serial_ttystate) state;
178 }
179
180 static int
181 hardwire_set_tty_state (serial_t scb, serial_ttystate ttystate)
182 {
183 struct hardwire_ttystate *state;
184
185 state = (struct hardwire_ttystate *) ttystate;
186
187 return set_tty_state (scb, state);
188 }
189
190 static int
191 hardwire_noflush_set_tty_state (serial_t scb,
192 serial_ttystate new_ttystate,
193 serial_ttystate old_ttystate)
194 {
195 struct hardwire_ttystate new_state;
196 #ifdef HAVE_SGTTY
197 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
198 #endif
199
200 new_state = *(struct hardwire_ttystate *) new_ttystate;
201
202 /* Don't change in or out of raw mode; we don't want to flush input.
203 termio and termios have no such restriction; for them flushing input
204 is separate from setting the attributes. */
205
206 #ifdef HAVE_SGTTY
207 if (state->sgttyb.sg_flags & RAW)
208 new_state.sgttyb.sg_flags |= RAW;
209 else
210 new_state.sgttyb.sg_flags &= ~RAW;
211
212 /* I'm not sure whether this is necessary; the manpage just mentions
213 RAW not CBREAK. */
214 if (state->sgttyb.sg_flags & CBREAK)
215 new_state.sgttyb.sg_flags |= CBREAK;
216 else
217 new_state.sgttyb.sg_flags &= ~CBREAK;
218 #endif
219
220 return set_tty_state (scb, &new_state);
221 }
222
223 static void
224 hardwire_print_tty_state (serial_t scb,
225 serial_ttystate ttystate,
226 struct gdb_file *stream)
227 {
228 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
229 int i;
230
231 #ifdef HAVE_TERMIOS
232 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
233 state->termios.c_iflag, state->termios.c_oflag);
234 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n",
235 state->termios.c_cflag, state->termios.c_lflag);
236 #if 0
237 /* This not in POSIX, and is not really documented by those systems
238 which have it (at least not Sun). */
239 fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line);
240 #endif
241 fprintf_filtered (stream, "c_cc: ");
242 for (i = 0; i < NCCS; i += 1)
243 fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]);
244 fprintf_filtered (stream, "\n");
245 #endif
246
247 #ifdef HAVE_TERMIO
248 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
249 state->termio.c_iflag, state->termio.c_oflag);
250 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
251 state->termio.c_cflag, state->termio.c_lflag,
252 state->termio.c_line);
253 fprintf_filtered (stream, "c_cc: ");
254 for (i = 0; i < NCC; i += 1)
255 fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]);
256 fprintf_filtered (stream, "\n");
257 #endif
258
259 #ifdef HAVE_SGTTY
260 fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n",
261 state->sgttyb.sg_flags);
262
263 fprintf_filtered (stream, "tchars: ");
264 for (i = 0; i < (int) sizeof (struct tchars); i++)
265 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]);
266 fprintf_filtered ("\n");
267
268 fprintf_filtered (stream, "ltchars: ");
269 for (i = 0; i < (int) sizeof (struct ltchars); i++)
270 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]);
271 fprintf_filtered (stream, "\n");
272
273 fprintf_filtered (stream, "lmode: 0x%x\n", state->lmode);
274 #endif
275 }
276
277 /* Wait for the output to drain away, as opposed to flushing (discarding) it */
278
279 static int
280 hardwire_drain_output (serial_t scb)
281 {
282 #ifdef HAVE_TERMIOS
283 return tcdrain (scb->fd);
284 #endif
285
286 #ifdef HAVE_TERMIO
287 return ioctl (scb->fd, TCSBRK, 1);
288 #endif
289
290 #ifdef HAVE_SGTTY
291 /* Get the current state and then restore it using TIOCSETP,
292 which should cause the output to drain and pending input
293 to be discarded. */
294 {
295 struct hardwire_ttystate state;
296 if (get_tty_state (scb, &state))
297 {
298 return (-1);
299 }
300 else
301 {
302 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
303 }
304 }
305 #endif
306 }
307
308 static int
309 hardwire_flush_output (serial_t scb)
310 {
311 #ifdef HAVE_TERMIOS
312 return tcflush (scb->fd, TCOFLUSH);
313 #endif
314
315 #ifdef HAVE_TERMIO
316 return ioctl (scb->fd, TCFLSH, 1);
317 #endif
318
319 #ifdef HAVE_SGTTY
320 /* This flushes both input and output, but we can't do better. */
321 return ioctl (scb->fd, TIOCFLUSH, 0);
322 #endif
323 }
324
325 static int
326 hardwire_flush_input (serial_t scb)
327 {
328 scb->bufcnt = 0;
329 scb->bufp = scb->buf;
330
331 #ifdef HAVE_TERMIOS
332 return tcflush (scb->fd, TCIFLUSH);
333 #endif
334
335 #ifdef HAVE_TERMIO
336 return ioctl (scb->fd, TCFLSH, 0);
337 #endif
338
339 #ifdef HAVE_SGTTY
340 /* This flushes both input and output, but we can't do better. */
341 return ioctl (scb->fd, TIOCFLUSH, 0);
342 #endif
343 }
344
345 static int
346 hardwire_send_break (serial_t scb)
347 {
348 #ifdef HAVE_TERMIOS
349 return tcsendbreak (scb->fd, 0);
350 #endif
351
352 #ifdef HAVE_TERMIO
353 return ioctl (scb->fd, TCSBRK, 0);
354 #endif
355
356 #ifdef HAVE_SGTTY
357 {
358 int status;
359 struct timeval timeout;
360
361 status = ioctl (scb->fd, TIOCSBRK, 0);
362
363 /* Can't use usleep; it doesn't exist in BSD 4.2. */
364 /* Note that if this select() is interrupted by a signal it will not wait
365 the full length of time. I think that is OK. */
366 timeout.tv_sec = 0;
367 timeout.tv_usec = 250000;
368 select (0, 0, 0, 0, &timeout);
369 status = ioctl (scb->fd, TIOCCBRK, 0);
370 return status;
371 }
372 #endif
373 }
374
375 static void
376 hardwire_raw (serial_t scb)
377 {
378 struct hardwire_ttystate state;
379
380 if (get_tty_state (scb, &state))
381 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
382
383 #ifdef HAVE_TERMIOS
384 state.termios.c_iflag = 0;
385 state.termios.c_oflag = 0;
386 state.termios.c_lflag = 0;
387 state.termios.c_cflag &= ~(CSIZE | PARENB);
388 state.termios.c_cflag |= CLOCAL | CS8;
389 state.termios.c_cc[VMIN] = 0;
390 state.termios.c_cc[VTIME] = 0;
391 #endif
392
393 #ifdef HAVE_TERMIO
394 state.termio.c_iflag = 0;
395 state.termio.c_oflag = 0;
396 state.termio.c_lflag = 0;
397 state.termio.c_cflag &= ~(CSIZE | PARENB);
398 state.termio.c_cflag |= CLOCAL | CS8;
399 state.termio.c_cc[VMIN] = 0;
400 state.termio.c_cc[VTIME] = 0;
401 #endif
402
403 #ifdef HAVE_SGTTY
404 state.sgttyb.sg_flags |= RAW | ANYP;
405 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
406 #endif
407
408 scb->current_timeout = 0;
409
410 if (set_tty_state (scb, &state))
411 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
412 }
413
414 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
415 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
416
417 For termio{s}, we actually just setup VTIME if necessary, and let the
418 timeout occur in the read() in hardwire_read().
419 */
420
421 /* FIXME: Don't replace this with the equivalent ser_unix*() until the
422 old TERMIOS/SGTTY/... timer code has been flushed. cagney
423 1999-09-16. */
424
425 static int
426 wait_for (serial_t scb, int timeout)
427 {
428 #ifdef HAVE_SGTTY
429 {
430 struct timeval tv;
431 fd_set readfds;
432
433 FD_ZERO (&readfds);
434
435 tv.tv_sec = timeout;
436 tv.tv_usec = 0;
437
438 FD_SET (scb->fd, &readfds);
439
440 while (1)
441 {
442 int numfds;
443
444 if (timeout >= 0)
445 numfds = select (scb->fd + 1, &readfds, 0, 0, &tv);
446 else
447 numfds = select (scb->fd + 1, &readfds, 0, 0, 0);
448
449 if (numfds <= 0)
450 if (numfds == 0)
451 return SERIAL_TIMEOUT;
452 else if (errno == EINTR)
453 continue;
454 else
455 return SERIAL_ERROR; /* Got an error from select or poll */
456
457 return 0;
458 }
459 }
460 #endif /* HAVE_SGTTY */
461
462 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
463 if (timeout == scb->current_timeout)
464 return 0;
465
466 scb->current_timeout = timeout;
467
468 {
469 struct hardwire_ttystate state;
470
471 if (get_tty_state (scb, &state))
472 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
473
474 #ifdef HAVE_TERMIOS
475 if (timeout < 0)
476 {
477 /* No timeout. */
478 state.termios.c_cc[VTIME] = 0;
479 state.termios.c_cc[VMIN] = 1;
480 }
481 else
482 {
483 state.termios.c_cc[VMIN] = 0;
484 state.termios.c_cc[VTIME] = timeout * 10;
485 if (state.termios.c_cc[VTIME] != timeout * 10)
486 {
487
488 /* If c_cc is an 8-bit signed character, we can't go
489 bigger than this. If it is always unsigned, we could use
490 25. */
491
492 scb->current_timeout = 12;
493 state.termios.c_cc[VTIME] = scb->current_timeout * 10;
494 scb->timeout_remaining = timeout - scb->current_timeout;
495 }
496 }
497 #endif
498
499 #ifdef HAVE_TERMIO
500 if (timeout < 0)
501 {
502 /* No timeout. */
503 state.termio.c_cc[VTIME] = 0;
504 state.termio.c_cc[VMIN] = 1;
505 }
506 else
507 {
508 state.termio.c_cc[VMIN] = 0;
509 state.termio.c_cc[VTIME] = timeout * 10;
510 if (state.termio.c_cc[VTIME] != timeout * 10)
511 {
512 /* If c_cc is an 8-bit signed character, we can't go
513 bigger than this. If it is always unsigned, we could use
514 25. */
515
516 scb->current_timeout = 12;
517 state.termio.c_cc[VTIME] = scb->current_timeout * 10;
518 scb->timeout_remaining = timeout - scb->current_timeout;
519 }
520 }
521 #endif
522
523 if (set_tty_state (scb, &state))
524 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
525
526 return 0;
527 }
528 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
529 }
530
531 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
532 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
533 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
534 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
535
536 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
537 ser_unix*() until the old TERMIOS/SGTTY/... timer code has been
538 flushed. */
539
540 /* NOTE: cagney/1999-09-16: This function is not identical to
541 ser_unix_readchar() as part of replacing it with ser_unix*()
542 merging will be required - this code handles the case where read()
543 times out due to no data while ser_unix_readchar() doesn't expect
544 that. */
545
546 static int
547 hardwire_readchar (serial_t scb, int timeout)
548 {
549 int status, delta;
550 int detach = 0;
551
552 if (scb->bufcnt-- > 0)
553 return *scb->bufp++;
554
555 if (timeout > 0)
556 timeout++;
557
558 /* We have to be able to keep the GUI alive here, so we break the original
559 timeout into steps of 1 second, running the "keep the GUI alive" hook
560 each time through the loop.
561 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
562 will only go through the loop once. */
563
564 delta = (timeout == 0 ? 0 : 1);
565 while (1)
566 {
567
568 /* N.B. The UI may destroy our world (for instance by calling
569 remote_stop,) in which case we want to get out of here as
570 quickly as possible. It is not safe to touch scb, since
571 someone else might have freed it. The ui_loop_hook signals that
572 we should exit by returning 1. */
573
574 if (ui_loop_hook)
575 detach = ui_loop_hook (0);
576
577 if (detach)
578 return SERIAL_TIMEOUT;
579
580 scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
581 status = wait_for (scb, delta);
582
583 if (status < 0)
584 return status;
585
586 /* NOTE: cagney/1999-09-17: See ser_unix_readchar() for reason
587 why ASYNC reads are character by character. */
588
589 scb->bufcnt = read (scb->fd, scb->buf,
590 (SERIAL_IS_ASYNC_P (scb) ? 1 : BUFSIZ));
591
592 if (scb->bufcnt <= 0)
593 {
594 if (scb->bufcnt == 0)
595 {
596 /* Zero characters means timeout (it could also be EOF, but
597 we don't (yet at least) distinguish). */
598 if (scb->timeout_remaining > 0)
599 {
600 timeout = scb->timeout_remaining;
601 continue;
602 }
603 else if (scb->timeout_remaining < 0)
604 continue;
605 else
606 return SERIAL_TIMEOUT;
607 }
608 else if (errno == EINTR)
609 continue;
610 else
611 return SERIAL_ERROR; /* Got an error from read */
612 }
613
614 scb->bufcnt--;
615 scb->bufp = scb->buf;
616 return *scb->bufp++;
617 }
618 }
619
620 #ifndef B19200
621 #define B19200 EXTA
622 #endif
623
624 #ifndef B38400
625 #define B38400 EXTB
626 #endif
627
628 /* Translate baud rates from integers to damn B_codes. Unix should
629 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
630
631 static struct
632 {
633 int rate;
634 int code;
635 }
636 baudtab[] =
637 {
638 {
639 50, B50
640 }
641 ,
642 {
643 75, B75
644 }
645 ,
646 {
647 110, B110
648 }
649 ,
650 {
651 134, B134
652 }
653 ,
654 {
655 150, B150
656 }
657 ,
658 {
659 200, B200
660 }
661 ,
662 {
663 300, B300
664 }
665 ,
666 {
667 600, B600
668 }
669 ,
670 {
671 1200, B1200
672 }
673 ,
674 {
675 1800, B1800
676 }
677 ,
678 {
679 2400, B2400
680 }
681 ,
682 {
683 4800, B4800
684 }
685 ,
686 {
687 9600, B9600
688 }
689 ,
690 {
691 19200, B19200
692 }
693 ,
694 {
695 38400, B38400
696 }
697 ,
698 #ifdef B57600
699 {
700 57600, B57600
701 }
702 ,
703 #endif
704 #ifdef B115200
705 {
706 115200, B115200
707 }
708 ,
709 #endif
710 #ifdef B230400
711 {
712 230400, B230400
713 }
714 ,
715 #endif
716 #ifdef B460800
717 {
718 460800, B460800
719 }
720 ,
721 #endif
722 {
723 -1, -1
724 }
725 ,
726 };
727
728 static int
729 rate_to_code (int rate)
730 {
731 int i;
732
733 for (i = 0; baudtab[i].rate != -1; i++)
734 if (rate == baudtab[i].rate)
735 return baudtab[i].code;
736
737 return -1;
738 }
739
740 static int
741 hardwire_setbaudrate (serial_t scb, int rate)
742 {
743 struct hardwire_ttystate state;
744
745 if (get_tty_state (scb, &state))
746 return -1;
747
748 #ifdef HAVE_TERMIOS
749 cfsetospeed (&state.termios, rate_to_code (rate));
750 cfsetispeed (&state.termios, rate_to_code (rate));
751 #endif
752
753 #ifdef HAVE_TERMIO
754 #ifndef CIBAUD
755 #define CIBAUD CBAUD
756 #endif
757
758 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
759 state.termio.c_cflag |= rate_to_code (rate);
760 #endif
761
762 #ifdef HAVE_SGTTY
763 state.sgttyb.sg_ispeed = rate_to_code (rate);
764 state.sgttyb.sg_ospeed = rate_to_code (rate);
765 #endif
766
767 return set_tty_state (scb, &state);
768 }
769
770 static int
771 hardwire_setstopbits (scb, num)
772 serial_t scb;
773 int num;
774 {
775 struct hardwire_ttystate state;
776 int newbit;
777
778 if (get_tty_state (scb, &state))
779 return -1;
780
781 switch (num)
782 {
783 case SERIAL_1_STOPBITS:
784 newbit = 0;
785 break;
786 case SERIAL_1_AND_A_HALF_STOPBITS:
787 case SERIAL_2_STOPBITS:
788 newbit = 1;
789 break;
790 default:
791 return 1;
792 }
793
794 #ifdef HAVE_TERMIOS
795 if (!newbit)
796 state.termios.c_cflag &= ~CSTOPB;
797 else
798 state.termios.c_cflag |= CSTOPB; /* two bits */
799 #endif
800
801 #ifdef HAVE_TERMIO
802 if (!newbit)
803 state.termio.c_cflag &= ~CSTOPB;
804 else
805 state.termio.c_cflag |= CSTOPB; /* two bits */
806 #endif
807
808 #ifdef HAVE_SGTTY
809 return 0; /* sgtty doesn't support this */
810 #endif
811
812 return set_tty_state (scb, &state);
813 }
814
815 /* FIXME: Don't replace this with the equivalent ser_unix*() until the
816 old TERMIOS/SGTTY/... timer code has been flushed. cagney
817 1999-09-16. */
818
819 static int
820 hardwire_write (serial_t scb, const char *str, int len)
821 {
822 int cc;
823
824 while (len > 0)
825 {
826 cc = write (scb->fd, str, len);
827
828 if (cc < 0)
829 return 1;
830 len -= cc;
831 str += cc;
832 }
833 return 0;
834 }
835
836
837 static void
838 hardwire_close (serial_t scb)
839 {
840 if (scb->fd < 0)
841 return;
842
843 close (scb->fd);
844 scb->fd = -1;
845 }
846
847 \f
848 /* Generic operations used by all UNIX/FD based serial interfaces. */
849
850 serial_ttystate
851 ser_unix_nop_get_tty_state (serial_t scb)
852 {
853 /* allocate a dummy */
854 return (serial_ttystate) XMALLOC (int);
855 }
856
857 int
858 ser_unix_nop_set_tty_state (serial_t scb, serial_ttystate ttystate)
859 {
860 return 0;
861 }
862
863 void
864 ser_unix_nop_raw (serial_t scb)
865 {
866 return; /* Always in raw mode */
867 }
868
869 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
870 otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
871
872 int
873 ser_unix_wait_for (serial_t scb, int timeout)
874 {
875 int numfds;
876 struct timeval tv;
877 fd_set readfds, exceptfds;
878
879 FD_ZERO (&readfds);
880 FD_ZERO (&exceptfds);
881
882 tv.tv_sec = timeout;
883 tv.tv_usec = 0;
884
885 FD_SET (scb->fd, &readfds);
886 FD_SET (scb->fd, &exceptfds);
887
888 while (1)
889 {
890 if (timeout >= 0)
891 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
892 else
893 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, 0);
894
895 if (numfds <= 0)
896 {
897 if (numfds == 0)
898 return SERIAL_TIMEOUT;
899 else if (errno == EINTR)
900 continue;
901 else
902 return SERIAL_ERROR; /* Got an error from select or poll */
903 }
904
905 return 0;
906 }
907 }
908
909 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
910 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
911 char if successful. Returns -2 if timeout expired, EOF if line dropped
912 dead, or -3 for any other error (see errno in that case). */
913
914 int
915 ser_unix_readchar (serial_t scb, int timeout)
916 {
917 int status;
918 int delta;
919
920 if (scb->bufcnt-- > 0)
921 return *scb->bufp++;
922
923 /* We have to be able to keep the GUI alive here, so we break the original
924 timeout into steps of 1 second, running the "keep the GUI alive" hook
925 each time through the loop.
926
927 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
928 will only go through the loop once. */
929
930 delta = (timeout == 0 ? 0 : 1);
931 while (1)
932 {
933
934 /* N.B. The UI may destroy our world (for instance by calling
935 remote_stop,) in which case we want to get out of here as
936 quickly as possible. It is not safe to touch scb, since
937 someone else might have freed it. The ui_loop_hook signals that
938 we should exit by returning 1. */
939
940 if (ui_loop_hook)
941 {
942 if (ui_loop_hook (0))
943 return SERIAL_TIMEOUT;
944 }
945
946 status = ser_unix_wait_for (scb, delta);
947 timeout -= delta;
948
949 /* If we got a character or an error back from wait_for, then we can
950 break from the loop before the timeout is completed. */
951
952 if (status != SERIAL_TIMEOUT)
953 {
954 break;
955 }
956
957 /* If we have exhausted the original timeout, then generate
958 a SERIAL_TIMEOUT, and pass it out of the loop. */
959
960 else if (timeout == 0)
961 {
962 status = SERIAL_TIMEOUT;
963 break;
964 }
965 }
966
967 if (status < 0)
968 return status;
969
970 while (1)
971 {
972 /* FIXME: cagney/1999-09-17: ASYNC: The ASYNC serial code needs
973 to be modified so that it agressivly tries to drain its local
974 input buffer. Until this is done, the read() below can only
975 take in single characters. This is to ensure that
976 unprocessed data doesn't end up sitting in the input fifo. */
977 scb->bufcnt = read (scb->fd, scb->buf,
978 (SERIAL_IS_ASYNC_P (scb) ? 1 : BUFSIZ));
979 if (scb->bufcnt != -1 || errno != EINTR)
980 break;
981 }
982
983 if (scb->bufcnt <= 0)
984 {
985 if (scb->bufcnt == 0)
986 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
987 distinguish between EOF & timeouts
988 someday] */
989 else
990 return SERIAL_ERROR; /* Got an error from read */
991 }
992
993 scb->bufcnt--;
994 scb->bufp = scb->buf;
995 return *scb->bufp++;
996 }
997
998 int
999 ser_unix_nop_noflush_set_tty_state (serial_t scb,
1000 serial_ttystate new_ttystate,
1001 serial_ttystate old_ttystate)
1002 {
1003 return 0;
1004 }
1005
1006 void
1007 ser_unix_nop_print_tty_state (serial_t scb,
1008 serial_ttystate ttystate,
1009 struct gdb_file *stream)
1010 {
1011 /* Nothing to print. */
1012 return;
1013 }
1014
1015 int
1016 ser_unix_nop_setbaudrate (serial_t scb, int rate)
1017 {
1018 return 0; /* Never fails! */
1019 }
1020
1021 int
1022 ser_unix_nop_setstopbits (serial_t scb, int num)
1023 {
1024 return 0; /* Never fails! */
1025 }
1026
1027 int
1028 ser_unix_write (serial_t scb, const char *str, int len)
1029 {
1030 int cc;
1031
1032 while (len > 0)
1033 {
1034 cc = write (scb->fd, str, len);
1035
1036 if (cc < 0)
1037 return 1;
1038 len -= cc;
1039 str += cc;
1040 }
1041 return 0;
1042 }
1043
1044 int
1045 ser_unix_nop_flush_output (serial_t scb)
1046 {
1047 return 0;
1048 }
1049
1050 int
1051 ser_unix_nop_flush_input (serial_t scb)
1052 {
1053 return 0;
1054 }
1055
1056 int
1057 ser_unix_nop_send_break (serial_t scb)
1058 {
1059 return 0;
1060 }
1061
1062 int
1063 ser_unix_nop_drain_output (serial_t scb)
1064 {
1065 return 0;
1066 }
1067
1068 static void
1069 ser_unix_event (int error, int fd, gdb_client_data context)
1070 {
1071 serial_t scb = context;
1072 scb->async_handler (error, scb->async_context, fd);
1073 }
1074
1075 void
1076 ser_unix_async (serial_t scb,
1077 int async_p)
1078 {
1079 if (async_p)
1080 {
1081 add_file_handler (scb->fd, ser_unix_event, scb);
1082 }
1083 else
1084 {
1085 delete_file_handler (scb->fd);
1086 }
1087 }
1088
1089 void
1090 _initialize_ser_hardwire (void)
1091 {
1092 struct serial_ops *ops = XMALLOC (struct serial_ops);
1093 memset (ops, sizeof (struct serial_ops), 0);
1094 ops->name = "hardwire";
1095 ops->next = 0;
1096 ops->open = hardwire_open;
1097 ops->close = hardwire_close;
1098 /* FIXME: Don't replace this with the equivalent ser_unix*() until
1099 the old TERMIOS/SGTTY/... timer code has been flushed. cagney
1100 1999-09-16. */
1101 ops->readchar = hardwire_readchar;
1102 /* FIXME: Don't replace this with the equivalent ser_unix*() until
1103 the old TERMIOS/SGTTY/... timer code has been flushed. cagney
1104 1999-09-16. */
1105 ops->write = hardwire_write;
1106 ops->flush_output = hardwire_flush_output;
1107 ops->flush_input = hardwire_flush_input;
1108 ops->send_break = hardwire_send_break;
1109 ops->go_raw = hardwire_raw;
1110 ops->get_tty_state = hardwire_get_tty_state;
1111 ops->set_tty_state = hardwire_set_tty_state;
1112 ops->print_tty_state = hardwire_print_tty_state;
1113 ops->noflush_set_tty_state = hardwire_noflush_set_tty_state;
1114 ops->setbaudrate = hardwire_setbaudrate;
1115 ops->setstopbits = hardwire_setstopbits;
1116 ops->drain_output = hardwire_drain_output;
1117 ops->async = ser_unix_async;
1118 serial_add_interface (ops);
1119 }