*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / ser-unix.c
1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
2
3 Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
4 2003, 2004, 2005 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23 #include "defs.h"
24 #include "serial.h"
25 #include "ser-base.h"
26 #include "ser-unix.h"
27
28 #include <fcntl.h>
29 #include <sys/types.h>
30 #include "terminal.h"
31 #include <sys/socket.h>
32 #include <sys/time.h>
33
34 #include "gdb_string.h"
35
36 #ifdef HAVE_TERMIOS
37
38 struct hardwire_ttystate
39 {
40 struct termios termios;
41 };
42 #endif /* termios */
43
44 #ifdef HAVE_TERMIO
45
46 /* It is believed that all systems which have added job control to SVR3
47 (e.g. sco) have also added termios. Even if not, trying to figure out
48 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
49 bewildering. So we don't attempt it. */
50
51 struct hardwire_ttystate
52 {
53 struct termio termio;
54 };
55 #endif /* termio */
56
57 #ifdef HAVE_SGTTY
58 struct hardwire_ttystate
59 {
60 struct sgttyb sgttyb;
61 struct tchars tc;
62 struct ltchars ltc;
63 /* Line discipline flags. */
64 int lmode;
65 };
66 #endif /* sgtty */
67
68 static int hardwire_open (struct serial *scb, const char *name);
69 static void hardwire_raw (struct serial *scb);
70 static int wait_for (struct serial *scb, int timeout);
71 static int hardwire_readchar (struct serial *scb, int timeout);
72 static int do_hardwire_readchar (struct serial *scb, int timeout);
73 static int rate_to_code (int rate);
74 static int hardwire_setbaudrate (struct serial *scb, int rate);
75 static void hardwire_close (struct serial *scb);
76 static int get_tty_state (struct serial *scb,
77 struct hardwire_ttystate * state);
78 static int set_tty_state (struct serial *scb,
79 struct hardwire_ttystate * state);
80 static serial_ttystate hardwire_get_tty_state (struct serial *scb);
81 static int hardwire_set_tty_state (struct serial *scb, serial_ttystate state);
82 static int hardwire_noflush_set_tty_state (struct serial *, serial_ttystate,
83 serial_ttystate);
84 static void hardwire_print_tty_state (struct serial *, serial_ttystate,
85 struct ui_file *);
86 static int hardwire_drain_output (struct serial *);
87 static int hardwire_flush_output (struct serial *);
88 static int hardwire_flush_input (struct serial *);
89 static int hardwire_send_break (struct serial *);
90 static int hardwire_setstopbits (struct serial *, int);
91
92 void _initialize_ser_hardwire (void);
93
94 /* Open up a real live device for serial I/O */
95
96 static int
97 hardwire_open (struct serial *scb, const char *name)
98 {
99 scb->fd = open (name, O_RDWR);
100 if (scb->fd < 0)
101 return -1;
102
103 return 0;
104 }
105
106 static int
107 get_tty_state (struct serial *scb, struct hardwire_ttystate *state)
108 {
109 #ifdef HAVE_TERMIOS
110 if (tcgetattr (scb->fd, &state->termios) < 0)
111 return -1;
112
113 return 0;
114 #endif
115
116 #ifdef HAVE_TERMIO
117 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
118 return -1;
119 return 0;
120 #endif
121
122 #ifdef HAVE_SGTTY
123 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
124 return -1;
125 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
126 return -1;
127 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
128 return -1;
129 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
130 return -1;
131
132 return 0;
133 #endif
134 }
135
136 static int
137 set_tty_state (struct serial *scb, struct hardwire_ttystate *state)
138 {
139 #ifdef HAVE_TERMIOS
140 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
141 return -1;
142
143 return 0;
144 #endif
145
146 #ifdef HAVE_TERMIO
147 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
148 return -1;
149 return 0;
150 #endif
151
152 #ifdef HAVE_SGTTY
153 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
154 return -1;
155 if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
156 return -1;
157 if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
158 return -1;
159 if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
160 return -1;
161
162 return 0;
163 #endif
164 }
165
166 static serial_ttystate
167 hardwire_get_tty_state (struct serial *scb)
168 {
169 struct hardwire_ttystate *state;
170
171 state = (struct hardwire_ttystate *) xmalloc (sizeof *state);
172
173 if (get_tty_state (scb, state))
174 return NULL;
175
176 return (serial_ttystate) state;
177 }
178
179 static int
180 hardwire_set_tty_state (struct serial *scb, serial_ttystate ttystate)
181 {
182 struct hardwire_ttystate *state;
183
184 state = (struct hardwire_ttystate *) ttystate;
185
186 return set_tty_state (scb, state);
187 }
188
189 static int
190 hardwire_noflush_set_tty_state (struct serial *scb,
191 serial_ttystate new_ttystate,
192 serial_ttystate old_ttystate)
193 {
194 struct hardwire_ttystate new_state;
195 #ifdef HAVE_SGTTY
196 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
197 #endif
198
199 new_state = *(struct hardwire_ttystate *) new_ttystate;
200
201 /* Don't change in or out of raw mode; we don't want to flush input.
202 termio and termios have no such restriction; for them flushing input
203 is separate from setting the attributes. */
204
205 #ifdef HAVE_SGTTY
206 if (state->sgttyb.sg_flags & RAW)
207 new_state.sgttyb.sg_flags |= RAW;
208 else
209 new_state.sgttyb.sg_flags &= ~RAW;
210
211 /* I'm not sure whether this is necessary; the manpage just mentions
212 RAW not CBREAK. */
213 if (state->sgttyb.sg_flags & CBREAK)
214 new_state.sgttyb.sg_flags |= CBREAK;
215 else
216 new_state.sgttyb.sg_flags &= ~CBREAK;
217 #endif
218
219 return set_tty_state (scb, &new_state);
220 }
221
222 static void
223 hardwire_print_tty_state (struct serial *scb,
224 serial_ttystate ttystate,
225 struct ui_file *stream)
226 {
227 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
228 int i;
229
230 #ifdef HAVE_TERMIOS
231 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
232 (int) state->termios.c_iflag,
233 (int) state->termios.c_oflag);
234 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n",
235 (int) state->termios.c_cflag,
236 (int) state->termios.c_lflag);
237 #if 0
238 /* This not in POSIX, and is not really documented by those systems
239 which have it (at least not Sun). */
240 fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line);
241 #endif
242 fprintf_filtered (stream, "c_cc: ");
243 for (i = 0; i < NCCS; i += 1)
244 fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]);
245 fprintf_filtered (stream, "\n");
246 #endif
247
248 #ifdef HAVE_TERMIO
249 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
250 state->termio.c_iflag, state->termio.c_oflag);
251 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
252 state->termio.c_cflag, state->termio.c_lflag,
253 state->termio.c_line);
254 fprintf_filtered (stream, "c_cc: ");
255 for (i = 0; i < NCC; i += 1)
256 fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]);
257 fprintf_filtered (stream, "\n");
258 #endif
259
260 #ifdef HAVE_SGTTY
261 fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n",
262 state->sgttyb.sg_flags);
263
264 fprintf_filtered (stream, "tchars: ");
265 for (i = 0; i < (int) sizeof (struct tchars); i++)
266 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]);
267 fprintf_filtered (stream, "\n");
268
269 fprintf_filtered (stream, "ltchars: ");
270 for (i = 0; i < (int) sizeof (struct ltchars); i++)
271 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]);
272 fprintf_filtered (stream, "\n");
273
274 fprintf_filtered (stream, "lmode: 0x%x\n", state->lmode);
275 #endif
276 }
277
278 /* Wait for the output to drain away, as opposed to flushing (discarding) it */
279
280 static int
281 hardwire_drain_output (struct serial *scb)
282 {
283 #ifdef HAVE_TERMIOS
284 return tcdrain (scb->fd);
285 #endif
286
287 #ifdef HAVE_TERMIO
288 return ioctl (scb->fd, TCSBRK, 1);
289 #endif
290
291 #ifdef HAVE_SGTTY
292 /* Get the current state and then restore it using TIOCSETP,
293 which should cause the output to drain and pending input
294 to be discarded. */
295 {
296 struct hardwire_ttystate state;
297 if (get_tty_state (scb, &state))
298 {
299 return (-1);
300 }
301 else
302 {
303 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
304 }
305 }
306 #endif
307 }
308
309 static int
310 hardwire_flush_output (struct serial *scb)
311 {
312 #ifdef HAVE_TERMIOS
313 return tcflush (scb->fd, TCOFLUSH);
314 #endif
315
316 #ifdef HAVE_TERMIO
317 return ioctl (scb->fd, TCFLSH, 1);
318 #endif
319
320 #ifdef HAVE_SGTTY
321 /* This flushes both input and output, but we can't do better. */
322 return ioctl (scb->fd, TIOCFLUSH, 0);
323 #endif
324 }
325
326 static int
327 hardwire_flush_input (struct serial *scb)
328 {
329 ser_base_flush_input (scb);
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 (struct serial *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 (struct serial *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: cagney/1999-09-16: Don't replace this with the equivalent
422 ser_base*() until the old TERMIOS/SGTTY/... timer code has been
423 flushed. . */
424
425 /* NOTE: cagney/1999-09-30: Much of the code below is dead. The only
426 possible values of the TIMEOUT parameter are ONE and ZERO.
427 Consequently all the code that tries to handle the possability of
428 an overflowed timer is unnecessary. */
429
430 static int
431 wait_for (struct serial *scb, int timeout)
432 {
433 #ifdef HAVE_SGTTY
434 while (1)
435 {
436 struct timeval tv;
437 fd_set readfds;
438 int numfds;
439
440 /* NOTE: Some OS's can scramble the READFDS when the select()
441 call fails (ex the kernel with Red Hat 5.2). Initialize all
442 arguments before each call. */
443
444 tv.tv_sec = timeout;
445 tv.tv_usec = 0;
446
447 FD_ZERO (&readfds);
448 FD_SET (scb->fd, &readfds);
449
450 if (timeout >= 0)
451 numfds = select (scb->fd + 1, &readfds, 0, 0, &tv);
452 else
453 numfds = select (scb->fd + 1, &readfds, 0, 0, 0);
454
455 if (numfds <= 0)
456 if (numfds == 0)
457 return SERIAL_TIMEOUT;
458 else if (errno == EINTR)
459 continue;
460 else
461 return SERIAL_ERROR; /* Got an error from select or poll */
462
463 return 0;
464 }
465 #endif /* HAVE_SGTTY */
466
467 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
468 if (timeout == scb->current_timeout)
469 return 0;
470
471 scb->current_timeout = timeout;
472
473 {
474 struct hardwire_ttystate state;
475
476 if (get_tty_state (scb, &state))
477 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
478
479 #ifdef HAVE_TERMIOS
480 if (timeout < 0)
481 {
482 /* No timeout. */
483 state.termios.c_cc[VTIME] = 0;
484 state.termios.c_cc[VMIN] = 1;
485 }
486 else
487 {
488 state.termios.c_cc[VMIN] = 0;
489 state.termios.c_cc[VTIME] = timeout * 10;
490 if (state.termios.c_cc[VTIME] != timeout * 10)
491 {
492
493 /* If c_cc is an 8-bit signed character, we can't go
494 bigger than this. If it is always unsigned, we could use
495 25. */
496
497 scb->current_timeout = 12;
498 state.termios.c_cc[VTIME] = scb->current_timeout * 10;
499 scb->timeout_remaining = timeout - scb->current_timeout;
500 }
501 }
502 #endif
503
504 #ifdef HAVE_TERMIO
505 if (timeout < 0)
506 {
507 /* No timeout. */
508 state.termio.c_cc[VTIME] = 0;
509 state.termio.c_cc[VMIN] = 1;
510 }
511 else
512 {
513 state.termio.c_cc[VMIN] = 0;
514 state.termio.c_cc[VTIME] = timeout * 10;
515 if (state.termio.c_cc[VTIME] != timeout * 10)
516 {
517 /* If c_cc is an 8-bit signed character, we can't go
518 bigger than this. If it is always unsigned, we could use
519 25. */
520
521 scb->current_timeout = 12;
522 state.termio.c_cc[VTIME] = scb->current_timeout * 10;
523 scb->timeout_remaining = timeout - scb->current_timeout;
524 }
525 }
526 #endif
527
528 if (set_tty_state (scb, &state))
529 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
530
531 return 0;
532 }
533 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
534 }
535
536 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
537 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
538 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
539 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
540
541 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
542 ser_base*() until the old TERMIOS/SGTTY/... timer code has been
543 flushed. */
544
545 /* NOTE: cagney/1999-09-16: This function is not identical to
546 ser_base_readchar() as part of replacing it with ser_base*()
547 merging will be required - this code handles the case where read()
548 times out due to no data while ser_base_readchar() doesn't expect
549 that. */
550
551 static int
552 do_hardwire_readchar (struct serial *scb, int timeout)
553 {
554 int status, delta;
555 int detach = 0;
556
557 if (timeout > 0)
558 timeout++;
559
560 /* We have to be able to keep the GUI alive here, so we break the
561 original timeout into steps of 1 second, running the "keep the
562 GUI alive" hook each time through the loop.
563
564 Also, timeout = 0 means to poll, so we just set the delta to 0,
565 so we will only go through the loop once. */
566
567 delta = (timeout == 0 ? 0 : 1);
568 while (1)
569 {
570
571 /* N.B. The UI may destroy our world (for instance by calling
572 remote_stop,) in which case we want to get out of here as
573 quickly as possible. It is not safe to touch scb, since
574 someone else might have freed it. The
575 deprecated_ui_loop_hook signals that we should exit by
576 returning 1. */
577
578 if (deprecated_ui_loop_hook)
579 detach = deprecated_ui_loop_hook (0);
580
581 if (detach)
582 return SERIAL_TIMEOUT;
583
584 scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
585 status = wait_for (scb, delta);
586
587 if (status < 0)
588 return status;
589
590 status = read (scb->fd, scb->buf, BUFSIZ);
591
592 if (status <= 0)
593 {
594 if (status == 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 = status;
615 scb->bufcnt--;
616 scb->bufp = scb->buf;
617 return *scb->bufp++;
618 }
619 }
620
621 static int
622 hardwire_readchar (struct serial *scb, int timeout)
623 {
624 return generic_readchar (scb, timeout, do_hardwire_readchar);
625 }
626
627
628 #ifndef B19200
629 #define B19200 EXTA
630 #endif
631
632 #ifndef B38400
633 #define B38400 EXTB
634 #endif
635
636 /* Translate baud rates from integers to damn B_codes. Unix should
637 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
638
639 static struct
640 {
641 int rate;
642 int code;
643 }
644 baudtab[] =
645 {
646 {
647 50, B50
648 }
649 ,
650 {
651 75, B75
652 }
653 ,
654 {
655 110, B110
656 }
657 ,
658 {
659 134, B134
660 }
661 ,
662 {
663 150, B150
664 }
665 ,
666 {
667 200, B200
668 }
669 ,
670 {
671 300, B300
672 }
673 ,
674 {
675 600, B600
676 }
677 ,
678 {
679 1200, B1200
680 }
681 ,
682 {
683 1800, B1800
684 }
685 ,
686 {
687 2400, B2400
688 }
689 ,
690 {
691 4800, B4800
692 }
693 ,
694 {
695 9600, B9600
696 }
697 ,
698 {
699 19200, B19200
700 }
701 ,
702 {
703 38400, B38400
704 }
705 ,
706 #ifdef B57600
707 {
708 57600, B57600
709 }
710 ,
711 #endif
712 #ifdef B115200
713 {
714 115200, B115200
715 }
716 ,
717 #endif
718 #ifdef B230400
719 {
720 230400, B230400
721 }
722 ,
723 #endif
724 #ifdef B460800
725 {
726 460800, B460800
727 }
728 ,
729 #endif
730 {
731 -1, -1
732 }
733 ,
734 };
735
736 static int
737 rate_to_code (int rate)
738 {
739 int i;
740
741 for (i = 0; baudtab[i].rate != -1; i++)
742 {
743 /* test for perfect macth. */
744 if (rate == baudtab[i].rate)
745 return baudtab[i].code;
746 else
747 {
748 /* check if it is in between valid values. */
749 if (rate < baudtab[i].rate)
750 {
751 if (i)
752 {
753 warning (_("Invalid baud rate %d. Closest values are %d and %d."),
754 rate, baudtab[i - 1].rate, baudtab[i].rate);
755 }
756 else
757 {
758 warning (_("Invalid baud rate %d. Minimum value is %d."),
759 rate, baudtab[0].rate);
760 }
761 return -1;
762 }
763 }
764 }
765
766 /* The requested speed was too large. */
767 warning (_("Invalid baud rate %d. Maximum value is %d."),
768 rate, baudtab[i - 1].rate);
769 return -1;
770 }
771
772 static int
773 hardwire_setbaudrate (struct serial *scb, int rate)
774 {
775 struct hardwire_ttystate state;
776 int baud_code = rate_to_code (rate);
777
778 if (baud_code < 0)
779 {
780 /* The baud rate was not valid.
781 A warning has already been issued. */
782 errno = EINVAL;
783 return -1;
784 }
785
786 if (get_tty_state (scb, &state))
787 return -1;
788
789 #ifdef HAVE_TERMIOS
790 cfsetospeed (&state.termios, baud_code);
791 cfsetispeed (&state.termios, baud_code);
792 #endif
793
794 #ifdef HAVE_TERMIO
795 #ifndef CIBAUD
796 #define CIBAUD CBAUD
797 #endif
798
799 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
800 state.termio.c_cflag |= baud_code;
801 #endif
802
803 #ifdef HAVE_SGTTY
804 state.sgttyb.sg_ispeed = baud_code;
805 state.sgttyb.sg_ospeed = baud_code;
806 #endif
807
808 return set_tty_state (scb, &state);
809 }
810
811 static int
812 hardwire_setstopbits (struct serial *scb, int num)
813 {
814 struct hardwire_ttystate state;
815 int newbit;
816
817 if (get_tty_state (scb, &state))
818 return -1;
819
820 switch (num)
821 {
822 case SERIAL_1_STOPBITS:
823 newbit = 0;
824 break;
825 case SERIAL_1_AND_A_HALF_STOPBITS:
826 case SERIAL_2_STOPBITS:
827 newbit = 1;
828 break;
829 default:
830 return 1;
831 }
832
833 #ifdef HAVE_TERMIOS
834 if (!newbit)
835 state.termios.c_cflag &= ~CSTOPB;
836 else
837 state.termios.c_cflag |= CSTOPB; /* two bits */
838 #endif
839
840 #ifdef HAVE_TERMIO
841 if (!newbit)
842 state.termio.c_cflag &= ~CSTOPB;
843 else
844 state.termio.c_cflag |= CSTOPB; /* two bits */
845 #endif
846
847 #ifdef HAVE_SGTTY
848 return 0; /* sgtty doesn't support this */
849 #endif
850
851 return set_tty_state (scb, &state);
852 }
853
854 static void
855 hardwire_close (struct serial *scb)
856 {
857 if (scb->fd < 0)
858 return;
859
860 close (scb->fd);
861 scb->fd = -1;
862 }
863 \f
864 \f
865 void
866 _initialize_ser_hardwire (void)
867 {
868 struct serial_ops *ops = XMALLOC (struct serial_ops);
869 memset (ops, 0, sizeof (struct serial_ops));
870 ops->name = "hardwire";
871 ops->next = 0;
872 ops->open = hardwire_open;
873 ops->close = hardwire_close;
874 /* FIXME: Don't replace this with the equivalent ser_base*() until
875 the old TERMIOS/SGTTY/... timer code has been flushed. cagney
876 1999-09-16. */
877 ops->readchar = hardwire_readchar;
878 ops->write = ser_base_write;
879 ops->flush_output = hardwire_flush_output;
880 ops->flush_input = hardwire_flush_input;
881 ops->send_break = hardwire_send_break;
882 ops->go_raw = hardwire_raw;
883 ops->get_tty_state = hardwire_get_tty_state;
884 ops->set_tty_state = hardwire_set_tty_state;
885 ops->print_tty_state = hardwire_print_tty_state;
886 ops->noflush_set_tty_state = hardwire_noflush_set_tty_state;
887 ops->setbaudrate = hardwire_setbaudrate;
888 ops->setstopbits = hardwire_setstopbits;
889 ops->drain_output = hardwire_drain_output;
890 ops->async = ser_base_async;
891 ops->read_prim = ser_unix_read_prim;
892 ops->write_prim = ser_unix_write_prim;
893 serial_add_interface (ops);
894 }
895
896 int
897 ser_unix_read_prim (struct serial *scb, size_t count)
898 {
899 int status;
900
901 while (1)
902 {
903 status = read (scb->fd, scb->buf, count);
904 if (status != -1 || errno != EINTR)
905 break;
906 }
907 return status;
908 }
909
910 int
911 ser_unix_write_prim (struct serial *scb, const void *buf, size_t len)
912 {
913 /* ??? Historically, GDB has not retried calls to "write" that
914 result in EINTR. */
915 return write (scb->fd, buf, len);
916 }
This page took 0.062104 seconds and 4 git commands to generate.