]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ser-unix.c
import gdb-1999-07-07 post reformat
[thirdparty/binutils-gdb.git] / gdb / ser-unix.c
CommitLineData
c906108c 1/* Serial interface for local (hardwired) serial ports on Un*x like systems
9846de1b 2 Copyright 1992, 1993, 1994, 1998 Free Software Foundation, Inc.
c906108c 3
c5aa993b 4 This file is part of GDB.
c906108c 5
c5aa993b
JM
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.
c906108c 10
c5aa993b
JM
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.
c906108c 15
c5aa993b
JM
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. */
c906108c
SS
20
21#include "defs.h"
22#include "serial.h"
23#include <fcntl.h>
24#include <sys/types.h>
25#include "terminal.h"
26#ifdef HAVE_UNISTD_H
27#include <unistd.h>
28#endif
29
30#ifdef HAVE_TERMIOS
31
32struct hardwire_ttystate
c5aa993b
JM
33 {
34 struct termios termios;
35 };
c906108c
SS
36#endif /* termios */
37
38#ifdef HAVE_TERMIO
39
40/* It is believed that all systems which have added job control to SVR3
41 (e.g. sco) have also added termios. Even if not, trying to figure out
42 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
43 bewildering. So we don't attempt it. */
44
45struct hardwire_ttystate
c5aa993b
JM
46 {
47 struct termio termio;
48 };
c906108c
SS
49#endif /* termio */
50
51#ifdef HAVE_SGTTY
52/* Needed for the code which uses select(). We would include <sys/select.h>
53 too if it existed on all systems. */
54#include <sys/time.h>
55
56struct hardwire_ttystate
c5aa993b
JM
57 {
58 struct sgttyb sgttyb;
59 struct tchars tc;
60 struct ltchars ltc;
61 /* Line discipline flags. */
62 int lmode;
63 };
c906108c
SS
64#endif /* sgtty */
65
66static int hardwire_open PARAMS ((serial_t scb, const char *name));
67static void hardwire_raw PARAMS ((serial_t scb));
68static int wait_for PARAMS ((serial_t scb, int timeout));
69static int hardwire_readchar PARAMS ((serial_t scb, int timeout));
70static int rate_to_code PARAMS ((int rate));
71static int hardwire_setbaudrate PARAMS ((serial_t scb, int rate));
72static int hardwire_write PARAMS ((serial_t scb, const char *str, int len));
73static void hardwire_close PARAMS ((serial_t scb));
c5aa993b
JM
74static int get_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate * state));
75static int set_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate * state));
c906108c
SS
76static serial_ttystate hardwire_get_tty_state PARAMS ((serial_t scb));
77static int hardwire_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
78static int hardwire_noflush_set_tty_state PARAMS ((serial_t, serial_ttystate,
79 serial_ttystate));
80static void hardwire_print_tty_state PARAMS ((serial_t, serial_ttystate));
81static int hardwire_drain_output PARAMS ((serial_t));
82static int hardwire_flush_output PARAMS ((serial_t));
83static int hardwire_flush_input PARAMS ((serial_t));
84static int hardwire_send_break PARAMS ((serial_t));
85static int hardwire_setstopbits PARAMS ((serial_t, int));
86
87void _initialize_ser_hardwire PARAMS ((void));
88
7a292a7a 89extern int (*ui_loop_hook) PARAMS ((int));
c906108c
SS
90
91/* Open up a real live device for serial I/O */
92
93static int
c5aa993b 94hardwire_open (scb, name)
c906108c
SS
95 serial_t scb;
96 const char *name;
97{
98 scb->fd = open (name, O_RDWR);
99 if (scb->fd < 0)
100 return -1;
101
102 return 0;
103}
104
105static int
106get_tty_state (scb, state)
107 serial_t scb;
108 struct hardwire_ttystate *state;
109{
110#ifdef HAVE_TERMIOS
c5aa993b 111 if (tcgetattr (scb->fd, &state->termios) < 0)
c906108c
SS
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
137static int
c5aa993b 138set_tty_state (scb, state)
c906108c
SS
139 serial_t scb;
140 struct hardwire_ttystate *state;
141{
142#ifdef HAVE_TERMIOS
c5aa993b 143 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
c906108c
SS
144 return -1;
145
146 return 0;
147#endif
148
149#ifdef HAVE_TERMIO
150 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
151 return -1;
152 return 0;
153#endif
154
155#ifdef HAVE_SGTTY
156 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
157 return -1;
158 if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
159 return -1;
160 if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
161 return -1;
162 if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
163 return -1;
164
165 return 0;
166#endif
167}
168
169static serial_ttystate
c5aa993b 170hardwire_get_tty_state (scb)
c906108c
SS
171 serial_t scb;
172{
173 struct hardwire_ttystate *state;
174
c5aa993b 175 state = (struct hardwire_ttystate *) xmalloc (sizeof *state);
c906108c 176
c5aa993b 177 if (get_tty_state (scb, state))
c906108c
SS
178 return NULL;
179
c5aa993b 180 return (serial_ttystate) state;
c906108c
SS
181}
182
183static int
c5aa993b 184hardwire_set_tty_state (scb, ttystate)
c906108c
SS
185 serial_t scb;
186 serial_ttystate ttystate;
187{
188 struct hardwire_ttystate *state;
189
c5aa993b 190 state = (struct hardwire_ttystate *) ttystate;
c906108c 191
c5aa993b 192 return set_tty_state (scb, state);
c906108c
SS
193}
194
195static int
196hardwire_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
197 serial_t scb;
198 serial_ttystate new_ttystate;
199 serial_ttystate old_ttystate;
200{
201 struct hardwire_ttystate new_state;
202#ifdef HAVE_SGTTY
203 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
204#endif
205
c5aa993b 206 new_state = *(struct hardwire_ttystate *) new_ttystate;
c906108c
SS
207
208 /* Don't change in or out of raw mode; we don't want to flush input.
209 termio and termios have no such restriction; for them flushing input
210 is separate from setting the attributes. */
211
212#ifdef HAVE_SGTTY
213 if (state->sgttyb.sg_flags & RAW)
214 new_state.sgttyb.sg_flags |= RAW;
215 else
216 new_state.sgttyb.sg_flags &= ~RAW;
217
218 /* I'm not sure whether this is necessary; the manpage just mentions
219 RAW not CBREAK. */
220 if (state->sgttyb.sg_flags & CBREAK)
221 new_state.sgttyb.sg_flags |= CBREAK;
222 else
223 new_state.sgttyb.sg_flags &= ~CBREAK;
224#endif
225
226 return set_tty_state (scb, &new_state);
227}
228
229static void
230hardwire_print_tty_state (scb, ttystate)
231 serial_t scb;
232 serial_ttystate ttystate;
233{
234 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
235 int i;
236
237#ifdef HAVE_TERMIOS
238 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
239 state->termios.c_iflag, state->termios.c_oflag);
240 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x\n",
241 state->termios.c_cflag, state->termios.c_lflag);
242#if 0
243 /* This not in POSIX, and is not really documented by those systems
244 which have it (at least not Sun). */
245 printf_filtered ("c_line = 0x%x.\n", state->termios.c_line);
246#endif
247 printf_filtered ("c_cc: ");
248 for (i = 0; i < NCCS; i += 1)
249 printf_filtered ("0x%x ", state->termios.c_cc[i]);
250 printf_filtered ("\n");
251#endif
252
253#ifdef HAVE_TERMIO
254 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
255 state->termio.c_iflag, state->termio.c_oflag);
256 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
257 state->termio.c_cflag, state->termio.c_lflag,
258 state->termio.c_line);
259 printf_filtered ("c_cc: ");
260 for (i = 0; i < NCC; i += 1)
261 printf_filtered ("0x%x ", state->termio.c_cc[i]);
262 printf_filtered ("\n");
263#endif
264
265#ifdef HAVE_SGTTY
266 printf_filtered ("sgttyb.sg_flags = 0x%x.\n", state->sgttyb.sg_flags);
267
268 printf_filtered ("tchars: ");
c5aa993b
JM
269 for (i = 0; i < (int) sizeof (struct tchars); i++)
270 printf_filtered ("0x%x ", ((unsigned char *) &state->tc)[i]);
c906108c
SS
271 printf_filtered ("\n");
272
273 printf_filtered ("ltchars: ");
c5aa993b
JM
274 for (i = 0; i < (int) sizeof (struct ltchars); i++)
275 printf_filtered ("0x%x ", ((unsigned char *) &state->ltc)[i]);
c906108c
SS
276 printf_filtered ("\n");
277
278 printf_filtered ("lmode: 0x%x\n", state->lmode);
279#endif
280}
281
282/* Wait for the output to drain away, as opposed to flushing (discarding) it */
283
284static int
285hardwire_drain_output (scb)
286 serial_t scb;
287{
288#ifdef HAVE_TERMIOS
289 return tcdrain (scb->fd);
290#endif
291
292#ifdef HAVE_TERMIO
293 return ioctl (scb->fd, TCSBRK, 1);
294#endif
295
296#ifdef HAVE_SGTTY
297 /* Get the current state and then restore it using TIOCSETP,
298 which should cause the output to drain and pending input
299 to be discarded. */
300 {
301 struct hardwire_ttystate state;
302 if (get_tty_state (scb, &state))
303 {
304 return (-1);
305 }
306 else
307 {
308 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
309 }
310 }
c5aa993b 311#endif
c906108c
SS
312}
313
314static int
315hardwire_flush_output (scb)
316 serial_t scb;
317{
318#ifdef HAVE_TERMIOS
319 return tcflush (scb->fd, TCOFLUSH);
320#endif
321
322#ifdef HAVE_TERMIO
323 return ioctl (scb->fd, TCFLSH, 1);
324#endif
325
326#ifdef HAVE_SGTTY
327 /* This flushes both input and output, but we can't do better. */
328 return ioctl (scb->fd, TIOCFLUSH, 0);
c5aa993b 329#endif
c906108c
SS
330}
331
332static int
333hardwire_flush_input (scb)
334 serial_t scb;
335{
336 scb->bufcnt = 0;
337 scb->bufp = scb->buf;
338
339#ifdef HAVE_TERMIOS
340 return tcflush (scb->fd, TCIFLUSH);
341#endif
342
343#ifdef HAVE_TERMIO
344 return ioctl (scb->fd, TCFLSH, 0);
345#endif
346
347#ifdef HAVE_SGTTY
348 /* This flushes both input and output, but we can't do better. */
349 return ioctl (scb->fd, TIOCFLUSH, 0);
c5aa993b 350#endif
c906108c
SS
351}
352
353static int
354hardwire_send_break (scb)
355 serial_t scb;
356{
357#ifdef HAVE_TERMIOS
358 return tcsendbreak (scb->fd, 0);
359#endif
360
361#ifdef HAVE_TERMIO
362 return ioctl (scb->fd, TCSBRK, 0);
363#endif
364
365#ifdef HAVE_SGTTY
366 {
367 int status;
368 struct timeval timeout;
369
370 status = ioctl (scb->fd, TIOCSBRK, 0);
371
372 /* Can't use usleep; it doesn't exist in BSD 4.2. */
373 /* Note that if this select() is interrupted by a signal it will not wait
374 the full length of time. I think that is OK. */
375 timeout.tv_sec = 0;
376 timeout.tv_usec = 250000;
377 select (0, 0, 0, 0, &timeout);
378 status = ioctl (scb->fd, TIOCCBRK, 0);
379 return status;
380 }
c5aa993b 381#endif
c906108c
SS
382}
383
384static void
c5aa993b 385hardwire_raw (scb)
c906108c
SS
386 serial_t scb;
387{
388 struct hardwire_ttystate state;
389
c5aa993b
JM
390 if (get_tty_state (scb, &state))
391 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
c906108c
SS
392
393#ifdef HAVE_TERMIOS
394 state.termios.c_iflag = 0;
395 state.termios.c_oflag = 0;
396 state.termios.c_lflag = 0;
c5aa993b 397 state.termios.c_cflag &= ~(CSIZE | PARENB);
c906108c
SS
398 state.termios.c_cflag |= CLOCAL | CS8;
399 state.termios.c_cc[VMIN] = 0;
400 state.termios.c_cc[VTIME] = 0;
401#endif
402
403#ifdef HAVE_TERMIO
404 state.termio.c_iflag = 0;
405 state.termio.c_oflag = 0;
406 state.termio.c_lflag = 0;
c5aa993b 407 state.termio.c_cflag &= ~(CSIZE | PARENB);
c906108c
SS
408 state.termio.c_cflag |= CLOCAL | CS8;
409 state.termio.c_cc[VMIN] = 0;
410 state.termio.c_cc[VTIME] = 0;
411#endif
412
413#ifdef HAVE_SGTTY
414 state.sgttyb.sg_flags |= RAW | ANYP;
415 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
416#endif
417
418 scb->current_timeout = 0;
419
420 if (set_tty_state (scb, &state))
c5aa993b 421 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
c906108c
SS
422}
423
424/* Wait for input on scb, with timeout seconds. Returns 0 on success,
425 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
426
427 For termio{s}, we actually just setup VTIME if necessary, and let the
428 timeout occur in the read() in hardwire_read().
429 */
430
431static int
c5aa993b 432wait_for (scb, timeout)
c906108c
SS
433 serial_t scb;
434 int timeout;
435{
c906108c
SS
436#ifdef HAVE_SGTTY
437 {
438 struct timeval tv;
439 fd_set readfds;
440
441 FD_ZERO (&readfds);
442
443 tv.tv_sec = timeout;
444 tv.tv_usec = 0;
445
c5aa993b 446 FD_SET (scb->fd, &readfds);
c906108c
SS
447
448 while (1)
449 {
450 int numfds;
451
452 if (timeout >= 0)
c5aa993b 453 numfds = select (scb->fd + 1, &readfds, 0, 0, &tv);
c906108c 454 else
c5aa993b 455 numfds = select (scb->fd + 1, &readfds, 0, 0, 0);
c906108c
SS
456
457 if (numfds <= 0)
458 if (numfds == 0)
459 return SERIAL_TIMEOUT;
460 else if (errno == EINTR)
461 continue;
462 else
463 return SERIAL_ERROR; /* Got an error from select or poll */
464
465 return 0;
466 }
467 }
c5aa993b 468#endif /* HAVE_SGTTY */
c906108c
SS
469
470#if defined HAVE_TERMIO || defined HAVE_TERMIOS
471 if (timeout == scb->current_timeout)
472 return 0;
473
474 scb->current_timeout = timeout;
475
476 {
477 struct hardwire_ttystate state;
478
c5aa993b
JM
479 if (get_tty_state (scb, &state))
480 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
c906108c
SS
481
482#ifdef HAVE_TERMIOS
483 if (timeout < 0)
484 {
485 /* No timeout. */
486 state.termios.c_cc[VTIME] = 0;
487 state.termios.c_cc[VMIN] = 1;
488 }
489 else
490 {
491 state.termios.c_cc[VMIN] = 0;
492 state.termios.c_cc[VTIME] = timeout * 10;
493 if (state.termios.c_cc[VTIME] != timeout * 10)
494 {
495
496 /* If c_cc is an 8-bit signed character, we can't go
497 bigger than this. If it is always unsigned, we could use
498 25. */
499
500 scb->current_timeout = 12;
501 state.termios.c_cc[VTIME] = scb->current_timeout * 10;
502 scb->timeout_remaining = timeout - scb->current_timeout;
503 }
504 }
505#endif
506
507#ifdef HAVE_TERMIO
508 if (timeout < 0)
509 {
510 /* No timeout. */
511 state.termio.c_cc[VTIME] = 0;
512 state.termio.c_cc[VMIN] = 1;
513 }
514 else
515 {
516 state.termio.c_cc[VMIN] = 0;
517 state.termio.c_cc[VTIME] = timeout * 10;
518 if (state.termio.c_cc[VTIME] != timeout * 10)
519 {
520 /* If c_cc is an 8-bit signed character, we can't go
521 bigger than this. If it is always unsigned, we could use
522 25. */
523
524 scb->current_timeout = 12;
525 state.termio.c_cc[VTIME] = scb->current_timeout * 10;
526 scb->timeout_remaining = timeout - scb->current_timeout;
527 }
528 }
529#endif
530
531 if (set_tty_state (scb, &state))
c5aa993b 532 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
c906108c
SS
533
534 return 0;
535 }
c5aa993b 536#endif /* HAVE_TERMIO || HAVE_TERMIOS */
c906108c
SS
537}
538
539/* Read a character with user-specified timeout. TIMEOUT is number of seconds
540 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
541 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
542 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
543static int
544hardwire_readchar (scb, timeout)
545 serial_t scb;
546 int timeout;
547{
7a292a7a
SS
548 int status, delta;
549 int detach = 0;
c906108c
SS
550
551 if (scb->bufcnt-- > 0)
552 return *scb->bufp++;
553
c906108c
SS
554 if (timeout > 0)
555 timeout++;
c906108c 556
7a292a7a
SS
557 /* We have to be able to keep the GUI alive here, so we break the original
558 timeout into steps of 1 second, running the "keep the GUI alive" hook
559 each time through the loop.
560 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
561 will only go through the loop once. */
c5aa993b 562
7a292a7a 563 delta = (timeout == 0 ? 0 : 1);
c906108c
SS
564 while (1)
565 {
c906108c 566
7a292a7a
SS
567 /* N.B. The UI may destroy our world (for instance by calling
568 remote_stop,) in which case we want to get out of here as
569 quickly as possible. It is not safe to touch scb, since
570 someone else might have freed it. The ui_loop_hook signals that
571 we should exit by returning 1. */
572
c906108c 573 if (ui_loop_hook)
c5aa993b 574 detach = ui_loop_hook (0);
7a292a7a
SS
575
576 if (detach)
577 return SERIAL_TIMEOUT;
578
579 scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
580 status = wait_for (scb, delta);
581
c906108c
SS
582 if (status < 0)
583 return status;
584
585 scb->bufcnt = read (scb->fd, scb->buf, BUFSIZ);
586
587 if (scb->bufcnt <= 0)
588 {
589 if (scb->bufcnt == 0)
590 {
591 /* Zero characters means timeout (it could also be EOF, but
c5aa993b 592 we don't (yet at least) distinguish). */
c906108c
SS
593 if (scb->timeout_remaining > 0)
594 {
595 timeout = scb->timeout_remaining;
596 continue;
597 }
c5aa993b
JM
598 else if (scb->timeout_remaining < 0)
599 continue;
c906108c
SS
600 else
601 return SERIAL_TIMEOUT;
602 }
603 else if (errno == EINTR)
604 continue;
605 else
606 return SERIAL_ERROR; /* Got an error from read */
607 }
608
609 scb->bufcnt--;
610 scb->bufp = scb->buf;
611 return *scb->bufp++;
612 }
613}
614
615#ifndef B19200
616#define B19200 EXTA
617#endif
618
619#ifndef B38400
620#define B38400 EXTB
621#endif
622
623/* Translate baud rates from integers to damn B_codes. Unix should
624 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
625
626static struct
627{
628 int rate;
629 int code;
630}
631baudtab[] =
632{
c5aa993b
JM
633 {
634 50, B50
635 }
636 ,
637 {
638 75, B75
639 }
640 ,
641 {
642 110, B110
643 }
644 ,
645 {
646 134, B134
647 }
648 ,
649 {
650 150, B150
651 }
652 ,
653 {
654 200, B200
655 }
656 ,
657 {
658 300, B300
659 }
660 ,
661 {
662 600, B600
663 }
664 ,
665 {
666 1200, B1200
667 }
668 ,
669 {
670 1800, B1800
671 }
672 ,
673 {
674 2400, B2400
675 }
676 ,
677 {
678 4800, B4800
679 }
680 ,
681 {
682 9600, B9600
683 }
684 ,
685 {
686 19200, B19200
687 }
688 ,
689 {
690 38400, B38400
691 }
692 ,
c906108c 693#ifdef B57600
c5aa993b
JM
694 {
695 57600, B57600
696 }
697 ,
c906108c
SS
698#endif
699#ifdef B115200
c5aa993b
JM
700 {
701 115200, B115200
702 }
703 ,
c906108c
SS
704#endif
705#ifdef B230400
c5aa993b
JM
706 {
707 230400, B230400
708 }
709 ,
c906108c
SS
710#endif
711#ifdef B460800
c5aa993b
JM
712 {
713 460800, B460800
714 }
715 ,
c906108c 716#endif
c5aa993b
JM
717 {
718 -1, -1
719 }
720 ,
c906108c
SS
721};
722
c5aa993b
JM
723static int
724rate_to_code (rate)
c906108c
SS
725 int rate;
726{
727 int i;
728
729 for (i = 0; baudtab[i].rate != -1; i++)
c5aa993b 730 if (rate == baudtab[i].rate)
c906108c
SS
731 return baudtab[i].code;
732
733 return -1;
734}
735
736static int
c5aa993b 737hardwire_setbaudrate (scb, rate)
c906108c
SS
738 serial_t scb;
739 int rate;
740{
741 struct hardwire_ttystate state;
742
c5aa993b 743 if (get_tty_state (scb, &state))
c906108c
SS
744 return -1;
745
746#ifdef HAVE_TERMIOS
747 cfsetospeed (&state.termios, rate_to_code (rate));
748 cfsetispeed (&state.termios, rate_to_code (rate));
749#endif
750
751#ifdef HAVE_TERMIO
752#ifndef CIBAUD
753#define CIBAUD CBAUD
754#endif
755
756 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
757 state.termio.c_cflag |= rate_to_code (rate);
758#endif
759
760#ifdef HAVE_SGTTY
761 state.sgttyb.sg_ispeed = rate_to_code (rate);
762 state.sgttyb.sg_ospeed = rate_to_code (rate);
763#endif
764
765 return set_tty_state (scb, &state);
766}
767
768static int
c5aa993b 769hardwire_setstopbits (scb, num)
c906108c
SS
770 serial_t scb;
771 int num;
772{
773 struct hardwire_ttystate state;
774 int newbit;
775
c5aa993b 776 if (get_tty_state (scb, &state))
c906108c
SS
777 return -1;
778
779 switch (num)
780 {
781 case SERIAL_1_STOPBITS:
782 newbit = 0;
783 break;
784 case SERIAL_1_AND_A_HALF_STOPBITS:
785 case SERIAL_2_STOPBITS:
786 newbit = 1;
787 break;
788 default:
789 return 1;
790 }
791
792#ifdef HAVE_TERMIOS
793 if (!newbit)
794 state.termios.c_cflag &= ~CSTOPB;
795 else
c5aa993b 796 state.termios.c_cflag |= CSTOPB; /* two bits */
c906108c
SS
797#endif
798
799#ifdef HAVE_TERMIO
800 if (!newbit)
801 state.termio.c_cflag &= ~CSTOPB;
802 else
c5aa993b 803 state.termio.c_cflag |= CSTOPB; /* two bits */
c906108c
SS
804#endif
805
806#ifdef HAVE_SGTTY
807 return 0; /* sgtty doesn't support this */
808#endif
809
810 return set_tty_state (scb, &state);
811}
812
813static int
c5aa993b 814hardwire_write (scb, str, len)
c906108c
SS
815 serial_t scb;
816 const char *str;
817 int len;
818{
819 int cc;
820
821 while (len > 0)
822 {
c5aa993b 823 cc = write (scb->fd, str, len);
c906108c
SS
824
825 if (cc < 0)
826 return 1;
827 len -= cc;
828 str += cc;
829 }
830 return 0;
831}
832
833static void
c5aa993b 834hardwire_close (scb)
c906108c
SS
835 serial_t scb;
836{
837 if (scb->fd < 0)
838 return;
839
c5aa993b 840 close (scb->fd);
c906108c
SS
841 scb->fd = -1;
842}
843
844static struct serial_ops hardwire_ops =
845{
846 "hardwire",
847 0,
848 hardwire_open,
849 hardwire_close,
850 hardwire_readchar,
851 hardwire_write,
852 hardwire_flush_output,
853 hardwire_flush_input,
854 hardwire_send_break,
855 hardwire_raw,
856 hardwire_get_tty_state,
857 hardwire_set_tty_state,
858 hardwire_print_tty_state,
859 hardwire_noflush_set_tty_state,
860 hardwire_setbaudrate,
861 hardwire_setstopbits,
862 hardwire_drain_output, /* wait for output to drain */
863};
864
865void
866_initialize_ser_hardwire ()
867{
868 serial_add_interface (&hardwire_ops);
869}