OSDN Git Service

Copyright updates for 2007.
[pf3gnuchains/pf3gnuchains3x.git] / gdb / ser-unix.c
1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
2
3    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2003,
4    2004, 2005, 2007 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., 51 Franklin Street, Fifth Floor,
21    Boston, MA 02110-1301, 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_select.h"
35 #include "gdb_string.h"
36
37 #ifdef HAVE_TERMIOS
38
39 struct hardwire_ttystate
40   {
41     struct termios termios;
42   };
43 #endif /* termios */
44
45 #ifdef HAVE_TERMIO
46
47 /* It is believed that all systems which have added job control to SVR3
48    (e.g. sco) have also added termios.  Even if not, trying to figure out
49    all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
50    bewildering.  So we don't attempt it.  */
51
52 struct hardwire_ttystate
53   {
54     struct termio termio;
55   };
56 #endif /* termio */
57
58 #ifdef HAVE_SGTTY
59 struct hardwire_ttystate
60   {
61     struct sgttyb sgttyb;
62     struct tchars tc;
63     struct ltchars ltc;
64     /* Line discipline flags.  */
65     int lmode;
66   };
67 #endif /* sgtty */
68
69 static int hardwire_open (struct serial *scb, const char *name);
70 static void hardwire_raw (struct serial *scb);
71 static int wait_for (struct serial *scb, int timeout);
72 static int hardwire_readchar (struct serial *scb, int timeout);
73 static int do_hardwire_readchar (struct serial *scb, int timeout);
74 static int rate_to_code (int rate);
75 static int hardwire_setbaudrate (struct serial *scb, int rate);
76 static void hardwire_close (struct serial *scb);
77 static int get_tty_state (struct serial *scb,
78                           struct hardwire_ttystate * state);
79 static int set_tty_state (struct serial *scb,
80                           struct hardwire_ttystate * state);
81 static serial_ttystate hardwire_get_tty_state (struct serial *scb);
82 static int hardwire_set_tty_state (struct serial *scb, serial_ttystate state);
83 static int hardwire_noflush_set_tty_state (struct serial *, serial_ttystate,
84                                            serial_ttystate);
85 static void hardwire_print_tty_state (struct serial *, serial_ttystate,
86                                       struct ui_file *);
87 static int hardwire_drain_output (struct serial *);
88 static int hardwire_flush_output (struct serial *);
89 static int hardwire_flush_input (struct serial *);
90 static int hardwire_send_break (struct serial *);
91 static int hardwire_setstopbits (struct serial *, int);
92
93 void _initialize_ser_hardwire (void);
94
95 /* Open up a real live device for serial I/O */
96
97 static int
98 hardwire_open (struct serial *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 (struct serial *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 (struct serial *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 (struct serial *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 (struct serial *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 (struct serial *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 (struct serial *scb,
225                           serial_ttystate ttystate,
226                           struct ui_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                     (int) state->termios.c_iflag,
234                     (int) state->termios.c_oflag);
235   fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n",
236                     (int) state->termios.c_cflag,
237                     (int) state->termios.c_lflag);
238 #if 0
239   /* This not in POSIX, and is not really documented by those systems
240      which have it (at least not Sun).  */
241   fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line);
242 #endif
243   fprintf_filtered (stream, "c_cc: ");
244   for (i = 0; i < NCCS; i += 1)
245     fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]);
246   fprintf_filtered (stream, "\n");
247 #endif
248
249 #ifdef HAVE_TERMIO
250   fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
251                     state->termio.c_iflag, state->termio.c_oflag);
252   fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
253                     state->termio.c_cflag, state->termio.c_lflag,
254                     state->termio.c_line);
255   fprintf_filtered (stream, "c_cc: ");
256   for (i = 0; i < NCC; i += 1)
257     fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]);
258   fprintf_filtered (stream, "\n");
259 #endif
260
261 #ifdef HAVE_SGTTY
262   fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n",
263                     state->sgttyb.sg_flags);
264
265   fprintf_filtered (stream, "tchars: ");
266   for (i = 0; i < (int) sizeof (struct tchars); i++)
267     fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]);
268   fprintf_filtered (stream, "\n");
269
270   fprintf_filtered (stream, "ltchars: ");
271   for (i = 0; i < (int) sizeof (struct ltchars); i++)
272     fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]);
273   fprintf_filtered (stream, "\n");
274
275   fprintf_filtered (stream, "lmode:  0x%x\n", state->lmode);
276 #endif
277 }
278
279 /* Wait for the output to drain away, as opposed to flushing (discarding) it */
280
281 static int
282 hardwire_drain_output (struct serial *scb)
283 {
284 #ifdef HAVE_TERMIOS
285   return tcdrain (scb->fd);
286 #endif
287
288 #ifdef HAVE_TERMIO
289   return ioctl (scb->fd, TCSBRK, 1);
290 #endif
291
292 #ifdef HAVE_SGTTY
293   /* Get the current state and then restore it using TIOCSETP,
294      which should cause the output to drain and pending input
295      to be discarded. */
296   {
297     struct hardwire_ttystate state;
298     if (get_tty_state (scb, &state))
299       {
300         return (-1);
301       }
302     else
303       {
304         return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
305       }
306   }
307 #endif
308 }
309
310 static int
311 hardwire_flush_output (struct serial *scb)
312 {
313 #ifdef HAVE_TERMIOS
314   return tcflush (scb->fd, TCOFLUSH);
315 #endif
316
317 #ifdef HAVE_TERMIO
318   return ioctl (scb->fd, TCFLSH, 1);
319 #endif
320
321 #ifdef HAVE_SGTTY
322   /* This flushes both input and output, but we can't do better.  */
323   return ioctl (scb->fd, TIOCFLUSH, 0);
324 #endif
325 }
326
327 static int
328 hardwire_flush_input (struct serial *scb)
329 {
330   ser_base_flush_input (scb);
331
332 #ifdef HAVE_TERMIOS
333   return tcflush (scb->fd, TCIFLUSH);
334 #endif
335
336 #ifdef HAVE_TERMIO
337   return ioctl (scb->fd, TCFLSH, 0);
338 #endif
339
340 #ifdef HAVE_SGTTY
341   /* This flushes both input and output, but we can't do better.  */
342   return ioctl (scb->fd, TIOCFLUSH, 0);
343 #endif
344 }
345
346 static int
347 hardwire_send_break (struct serial *scb)
348 {
349 #ifdef HAVE_TERMIOS
350   return tcsendbreak (scb->fd, 0);
351 #endif
352
353 #ifdef HAVE_TERMIO
354   return ioctl (scb->fd, TCSBRK, 0);
355 #endif
356
357 #ifdef HAVE_SGTTY
358   {
359     int status;
360     struct timeval timeout;
361
362     status = ioctl (scb->fd, TIOCSBRK, 0);
363
364     /* Can't use usleep; it doesn't exist in BSD 4.2.  */
365     /* Note that if this select() is interrupted by a signal it will not wait
366        the full length of time.  I think that is OK.  */
367     timeout.tv_sec = 0;
368     timeout.tv_usec = 250000;
369     gdb_select (0, 0, 0, 0, &timeout);
370     status = ioctl (scb->fd, TIOCCBRK, 0);
371     return status;
372   }
373 #endif
374 }
375
376 static void
377 hardwire_raw (struct serial *scb)
378 {
379   struct hardwire_ttystate state;
380
381   if (get_tty_state (scb, &state))
382     fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
383
384 #ifdef HAVE_TERMIOS
385   state.termios.c_iflag = 0;
386   state.termios.c_oflag = 0;
387   state.termios.c_lflag = 0;
388   state.termios.c_cflag &= ~(CSIZE | PARENB);
389   state.termios.c_cflag |= CLOCAL | CS8;
390   state.termios.c_cc[VMIN] = 0;
391   state.termios.c_cc[VTIME] = 0;
392 #endif
393
394 #ifdef HAVE_TERMIO
395   state.termio.c_iflag = 0;
396   state.termio.c_oflag = 0;
397   state.termio.c_lflag = 0;
398   state.termio.c_cflag &= ~(CSIZE | PARENB);
399   state.termio.c_cflag |= CLOCAL | CS8;
400   state.termio.c_cc[VMIN] = 0;
401   state.termio.c_cc[VTIME] = 0;
402 #endif
403
404 #ifdef HAVE_SGTTY
405   state.sgttyb.sg_flags |= RAW | ANYP;
406   state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
407 #endif
408
409   scb->current_timeout = 0;
410
411   if (set_tty_state (scb, &state))
412     fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
413 }
414
415 /* Wait for input on scb, with timeout seconds.  Returns 0 on success,
416    otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
417
418    For termio{s}, we actually just setup VTIME if necessary, and let the
419    timeout occur in the read() in hardwire_read().
420  */
421
422 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
423    ser_base*() until the old TERMIOS/SGTTY/... timer code has been
424    flushed. . */
425
426 /* NOTE: cagney/1999-09-30: Much of the code below is dead.  The only
427    possible values of the TIMEOUT parameter are ONE and ZERO.
428    Consequently all the code that tries to handle the possability of
429    an overflowed timer is unnecessary. */
430
431 static int
432 wait_for (struct serial *scb, int timeout)
433 {
434 #ifdef HAVE_SGTTY
435   while (1)
436     {
437       struct timeval tv;
438       fd_set readfds;
439       int numfds;
440
441       /* NOTE: Some OS's can scramble the READFDS when the select()
442          call fails (ex the kernel with Red Hat 5.2).  Initialize all
443          arguments before each call. */
444
445       tv.tv_sec = timeout;
446       tv.tv_usec = 0;
447
448       FD_ZERO (&readfds);
449       FD_SET (scb->fd, &readfds);
450
451       if (timeout >= 0)
452         numfds = gdb_select (scb->fd + 1, &readfds, 0, 0, &tv);
453       else
454         numfds = gdb_select (scb->fd + 1, &readfds, 0, 0, 0);
455
456       if (numfds <= 0)
457         if (numfds == 0)
458           return SERIAL_TIMEOUT;
459         else if (errno == EINTR)
460           continue;
461         else
462           return SERIAL_ERROR;  /* Got an error from select or poll */
463
464       return 0;
465     }
466 #endif /* HAVE_SGTTY */
467
468 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
469   if (timeout == scb->current_timeout)
470     return 0;
471
472   scb->current_timeout = timeout;
473
474   {
475     struct hardwire_ttystate state;
476
477     if (get_tty_state (scb, &state))
478       fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
479
480 #ifdef HAVE_TERMIOS
481     if (timeout < 0)
482       {
483         /* No timeout.  */
484         state.termios.c_cc[VTIME] = 0;
485         state.termios.c_cc[VMIN] = 1;
486       }
487     else
488       {
489         state.termios.c_cc[VMIN] = 0;
490         state.termios.c_cc[VTIME] = timeout * 10;
491         if (state.termios.c_cc[VTIME] != timeout * 10)
492           {
493
494             /* If c_cc is an 8-bit signed character, we can't go 
495                bigger than this.  If it is always unsigned, we could use
496                25.  */
497
498             scb->current_timeout = 12;
499             state.termios.c_cc[VTIME] = scb->current_timeout * 10;
500             scb->timeout_remaining = timeout - scb->current_timeout;
501           }
502       }
503 #endif
504
505 #ifdef HAVE_TERMIO
506     if (timeout < 0)
507       {
508         /* No timeout.  */
509         state.termio.c_cc[VTIME] = 0;
510         state.termio.c_cc[VMIN] = 1;
511       }
512     else
513       {
514         state.termio.c_cc[VMIN] = 0;
515         state.termio.c_cc[VTIME] = timeout * 10;
516         if (state.termio.c_cc[VTIME] != timeout * 10)
517           {
518             /* If c_cc is an 8-bit signed character, we can't go 
519                bigger than this.  If it is always unsigned, we could use
520                25.  */
521
522             scb->current_timeout = 12;
523             state.termio.c_cc[VTIME] = scb->current_timeout * 10;
524             scb->timeout_remaining = timeout - scb->current_timeout;
525           }
526       }
527 #endif
528
529     if (set_tty_state (scb, &state))
530       fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
531
532     return 0;
533   }
534 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
535 }
536
537 /* Read a character with user-specified timeout.  TIMEOUT is number of seconds
538    to wait, or -1 to wait forever.  Use timeout of 0 to effect a poll.  Returns
539    char if successful.  Returns SERIAL_TIMEOUT if timeout expired, EOF if line
540    dropped dead, or SERIAL_ERROR for any other error (see errno in that case).  */
541
542 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
543    ser_base*() until the old TERMIOS/SGTTY/... timer code has been
544    flushed. */
545
546 /* NOTE: cagney/1999-09-16: This function is not identical to
547    ser_base_readchar() as part of replacing it with ser_base*()
548    merging will be required - this code handles the case where read()
549    times out due to no data while ser_base_readchar() doesn't expect
550    that. */
551
552 static int
553 do_hardwire_readchar (struct serial *scb, int timeout)
554 {
555   int status, delta;
556   int detach = 0;
557
558   if (timeout > 0)
559     timeout++;
560
561   /* We have to be able to keep the GUI alive here, so we break the
562      original timeout into steps of 1 second, running the "keep the
563      GUI alive" hook each time through the loop.
564
565      Also, timeout = 0 means to poll, so we just set the delta to 0,
566      so we will only go through the loop once.  */
567
568   delta = (timeout == 0 ? 0 : 1);
569   while (1)
570     {
571
572       /* N.B. The UI may destroy our world (for instance by calling
573          remote_stop,) in which case we want to get out of here as
574          quickly as possible.  It is not safe to touch scb, since
575          someone else might have freed it.  The
576          deprecated_ui_loop_hook signals that we should exit by
577          returning 1.  */
578
579       if (deprecated_ui_loop_hook)
580         detach = deprecated_ui_loop_hook (0);
581
582       if (detach)
583         return SERIAL_TIMEOUT;
584
585       scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
586       status = wait_for (scb, delta);
587
588       if (status < 0)
589         return status;
590
591       status = read (scb->fd, scb->buf, BUFSIZ);
592
593       if (status <= 0)
594         {
595           if (status == 0)
596             {
597               /* Zero characters means timeout (it could also be EOF, but
598                  we don't (yet at least) distinguish).  */
599               if (scb->timeout_remaining > 0)
600                 {
601                   timeout = scb->timeout_remaining;
602                   continue;
603                 }
604               else if (scb->timeout_remaining < 0)
605                 continue;
606               else
607                 return SERIAL_TIMEOUT;
608             }
609           else if (errno == EINTR)
610             continue;
611           else
612             return SERIAL_ERROR;        /* Got an error from read */
613         }
614
615       scb->bufcnt = status;
616       scb->bufcnt--;
617       scb->bufp = scb->buf;
618       return *scb->bufp++;
619     }
620 }
621
622 static int
623 hardwire_readchar (struct serial *scb, int timeout)
624 {
625   return generic_readchar (scb, timeout, do_hardwire_readchar);
626 }
627
628
629 #ifndef B19200
630 #define B19200 EXTA
631 #endif
632
633 #ifndef B38400
634 #define B38400 EXTB
635 #endif
636
637 /* Translate baud rates from integers to damn B_codes.  Unix should
638    have outgrown this crap years ago, but even POSIX wouldn't buck it.  */
639
640 static struct
641 {
642   int rate;
643   int code;
644 }
645 baudtab[] =
646 {
647   {
648     50, B50
649   }
650   ,
651   {
652     75, B75
653   }
654   ,
655   {
656     110, B110
657   }
658   ,
659   {
660     134, B134
661   }
662   ,
663   {
664     150, B150
665   }
666   ,
667   {
668     200, B200
669   }
670   ,
671   {
672     300, B300
673   }
674   ,
675   {
676     600, B600
677   }
678   ,
679   {
680     1200, B1200
681   }
682   ,
683   {
684     1800, B1800
685   }
686   ,
687   {
688     2400, B2400
689   }
690   ,
691   {
692     4800, B4800
693   }
694   ,
695   {
696     9600, B9600
697   }
698   ,
699   {
700     19200, B19200
701   }
702   ,
703   {
704     38400, B38400
705   }
706   ,
707 #ifdef B57600
708   {
709     57600, B57600
710   }
711   ,
712 #endif
713 #ifdef B115200
714   {
715     115200, B115200
716   }
717   ,
718 #endif
719 #ifdef B230400
720   {
721     230400, B230400
722   }
723   ,
724 #endif
725 #ifdef B460800
726   {
727     460800, B460800
728   }
729   ,
730 #endif
731   {
732     -1, -1
733   }
734   ,
735 };
736
737 static int
738 rate_to_code (int rate)
739 {
740   int i;
741
742   for (i = 0; baudtab[i].rate != -1; i++)
743     {
744       /* test for perfect macth. */
745       if (rate == baudtab[i].rate)
746         return baudtab[i].code;
747       else
748         {
749           /* check if it is in between valid values. */
750           if (rate < baudtab[i].rate)
751             {
752               if (i)
753                 {
754                   warning (_("Invalid baud rate %d.  Closest values are %d and %d."),
755                             rate, baudtab[i - 1].rate, baudtab[i].rate);
756                 }
757               else
758                 {
759                   warning (_("Invalid baud rate %d.  Minimum value is %d."),
760                             rate, baudtab[0].rate);
761                 }
762               return -1;
763             }
764         }
765     }
766  
767   /* The requested speed was too large. */
768   warning (_("Invalid baud rate %d.  Maximum value is %d."),
769             rate, baudtab[i - 1].rate);
770   return -1;
771 }
772
773 static int
774 hardwire_setbaudrate (struct serial *scb, int rate)
775 {
776   struct hardwire_ttystate state;
777   int baud_code = rate_to_code (rate);
778   
779   if (baud_code < 0)
780     {
781       /* The baud rate was not valid.
782          A warning has already been issued. */
783       errno = EINVAL;
784       return -1;
785     }
786
787   if (get_tty_state (scb, &state))
788     return -1;
789
790 #ifdef HAVE_TERMIOS
791   cfsetospeed (&state.termios, baud_code);
792   cfsetispeed (&state.termios, baud_code);
793 #endif
794
795 #ifdef HAVE_TERMIO
796 #ifndef CIBAUD
797 #define CIBAUD CBAUD
798 #endif
799
800   state.termio.c_cflag &= ~(CBAUD | CIBAUD);
801   state.termio.c_cflag |= baud_code;
802 #endif
803
804 #ifdef HAVE_SGTTY
805   state.sgttyb.sg_ispeed = baud_code;
806   state.sgttyb.sg_ospeed = baud_code;
807 #endif
808
809   return set_tty_state (scb, &state);
810 }
811
812 static int
813 hardwire_setstopbits (struct serial *scb, int num)
814 {
815   struct hardwire_ttystate state;
816   int newbit;
817
818   if (get_tty_state (scb, &state))
819     return -1;
820
821   switch (num)
822     {
823     case SERIAL_1_STOPBITS:
824       newbit = 0;
825       break;
826     case SERIAL_1_AND_A_HALF_STOPBITS:
827     case SERIAL_2_STOPBITS:
828       newbit = 1;
829       break;
830     default:
831       return 1;
832     }
833
834 #ifdef HAVE_TERMIOS
835   if (!newbit)
836     state.termios.c_cflag &= ~CSTOPB;
837   else
838     state.termios.c_cflag |= CSTOPB;    /* two bits */
839 #endif
840
841 #ifdef HAVE_TERMIO
842   if (!newbit)
843     state.termio.c_cflag &= ~CSTOPB;
844   else
845     state.termio.c_cflag |= CSTOPB;     /* two bits */
846 #endif
847
848 #ifdef HAVE_SGTTY
849   return 0;                     /* sgtty doesn't support this */
850 #endif
851
852   return set_tty_state (scb, &state);
853 }
854
855 static void
856 hardwire_close (struct serial *scb)
857 {
858   if (scb->fd < 0)
859     return;
860
861   close (scb->fd);
862   scb->fd = -1;
863 }
864 \f
865 \f
866 void
867 _initialize_ser_hardwire (void)
868 {
869   struct serial_ops *ops = XMALLOC (struct serial_ops);
870   memset (ops, 0, sizeof (struct serial_ops));
871   ops->name = "hardwire";
872   ops->next = 0;
873   ops->open = hardwire_open;
874   ops->close = hardwire_close;
875   /* FIXME: Don't replace this with the equivalent ser_base*() until
876      the old TERMIOS/SGTTY/... timer code has been flushed. cagney
877      1999-09-16. */
878   ops->readchar = hardwire_readchar;
879   ops->write = ser_base_write;
880   ops->flush_output = hardwire_flush_output;
881   ops->flush_input = hardwire_flush_input;
882   ops->send_break = hardwire_send_break;
883   ops->go_raw = hardwire_raw;
884   ops->get_tty_state = hardwire_get_tty_state;
885   ops->set_tty_state = hardwire_set_tty_state;
886   ops->print_tty_state = hardwire_print_tty_state;
887   ops->noflush_set_tty_state = hardwire_noflush_set_tty_state;
888   ops->setbaudrate = hardwire_setbaudrate;
889   ops->setstopbits = hardwire_setstopbits;
890   ops->drain_output = hardwire_drain_output;
891   ops->async = ser_base_async;
892   ops->read_prim = ser_unix_read_prim;
893   ops->write_prim = ser_unix_write_prim;
894   serial_add_interface (ops);
895 }
896
897 int
898 ser_unix_read_prim (struct serial *scb, size_t count)
899 {
900   int status;
901
902   while (1)
903     {
904       status = read (scb->fd, scb->buf, count);
905       if (status != -1 || errno != EINTR)
906         break;
907     }
908   return status;
909 }
910
911 int
912 ser_unix_write_prim (struct serial *scb, const void *buf, size_t len)
913 {
914   /* ??? Historically, GDB has not retried calls to "write" that
915      result in EINTR.  */
916   return write (scb->fd, buf, len);
917 }