* config/sparc/nbsd64.mh (NAT_FILE): Remove.
[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 generic_readchar (struct serial *scb, int timeout,
74 int (*do_readchar) (struct serial *scb,
75 int timeout));
76 static int rate_to_code (int rate);
77 static int hardwire_setbaudrate (struct serial *scb, int rate);
78 static void hardwire_close (struct serial *scb);
79 static int get_tty_state (struct serial *scb,
80 struct hardwire_ttystate * state);
81 static int set_tty_state (struct serial *scb,
82 struct hardwire_ttystate * state);
83 static serial_ttystate hardwire_get_tty_state (struct serial *scb);
84 static int hardwire_set_tty_state (struct serial *scb, serial_ttystate state);
85 static int hardwire_noflush_set_tty_state (struct serial *, serial_ttystate,
86 serial_ttystate);
87 static void hardwire_print_tty_state (struct serial *, serial_ttystate,
88 struct ui_file *);
89 static int hardwire_drain_output (struct serial *);
90 static int hardwire_flush_output (struct serial *);
91 static int hardwire_flush_input (struct serial *);
92 static int hardwire_send_break (struct serial *);
93 static int hardwire_setstopbits (struct serial *, int);
94
95 void _initialize_ser_hardwire (void);
96
97 /* Open up a real live device for serial I/O */
98
99 static int
100 hardwire_open (struct serial *scb, const char *name)
101 {
102 scb->fd = open (name, O_RDWR);
103 if (scb->fd < 0)
104 return -1;
105
106 return 0;
107 }
108
109 static int
110 get_tty_state (struct serial *scb, struct hardwire_ttystate *state)
111 {
112 #ifdef HAVE_TERMIOS
113 if (tcgetattr (scb->fd, &state->termios) < 0)
114 return -1;
115
116 return 0;
117 #endif
118
119 #ifdef HAVE_TERMIO
120 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
121 return -1;
122 return 0;
123 #endif
124
125 #ifdef HAVE_SGTTY
126 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
127 return -1;
128 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
129 return -1;
130 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
131 return -1;
132 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
133 return -1;
134
135 return 0;
136 #endif
137 }
138
139 static int
140 set_tty_state (struct serial *scb, struct hardwire_ttystate *state)
141 {
142 #ifdef HAVE_TERMIOS
143 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
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
169 static serial_ttystate
170 hardwire_get_tty_state (struct serial *scb)
171 {
172 struct hardwire_ttystate *state;
173
174 state = (struct hardwire_ttystate *) xmalloc (sizeof *state);
175
176 if (get_tty_state (scb, state))
177 return NULL;
178
179 return (serial_ttystate) state;
180 }
181
182 static int
183 hardwire_set_tty_state (struct serial *scb, serial_ttystate ttystate)
184 {
185 struct hardwire_ttystate *state;
186
187 state = (struct hardwire_ttystate *) ttystate;
188
189 return set_tty_state (scb, state);
190 }
191
192 static int
193 hardwire_noflush_set_tty_state (struct serial *scb,
194 serial_ttystate new_ttystate,
195 serial_ttystate old_ttystate)
196 {
197 struct hardwire_ttystate new_state;
198 #ifdef HAVE_SGTTY
199 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
200 #endif
201
202 new_state = *(struct hardwire_ttystate *) new_ttystate;
203
204 /* Don't change in or out of raw mode; we don't want to flush input.
205 termio and termios have no such restriction; for them flushing input
206 is separate from setting the attributes. */
207
208 #ifdef HAVE_SGTTY
209 if (state->sgttyb.sg_flags & RAW)
210 new_state.sgttyb.sg_flags |= RAW;
211 else
212 new_state.sgttyb.sg_flags &= ~RAW;
213
214 /* I'm not sure whether this is necessary; the manpage just mentions
215 RAW not CBREAK. */
216 if (state->sgttyb.sg_flags & CBREAK)
217 new_state.sgttyb.sg_flags |= CBREAK;
218 else
219 new_state.sgttyb.sg_flags &= ~CBREAK;
220 #endif
221
222 return set_tty_state (scb, &new_state);
223 }
224
225 static void
226 hardwire_print_tty_state (struct serial *scb,
227 serial_ttystate ttystate,
228 struct ui_file *stream)
229 {
230 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
231 int i;
232
233 #ifdef HAVE_TERMIOS
234 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
235 (int) state->termios.c_iflag,
236 (int) state->termios.c_oflag);
237 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n",
238 (int) state->termios.c_cflag,
239 (int) state->termios.c_lflag);
240 #if 0
241 /* This not in POSIX, and is not really documented by those systems
242 which have it (at least not Sun). */
243 fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line);
244 #endif
245 fprintf_filtered (stream, "c_cc: ");
246 for (i = 0; i < NCCS; i += 1)
247 fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]);
248 fprintf_filtered (stream, "\n");
249 #endif
250
251 #ifdef HAVE_TERMIO
252 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
253 state->termio.c_iflag, state->termio.c_oflag);
254 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
255 state->termio.c_cflag, state->termio.c_lflag,
256 state->termio.c_line);
257 fprintf_filtered (stream, "c_cc: ");
258 for (i = 0; i < NCC; i += 1)
259 fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]);
260 fprintf_filtered (stream, "\n");
261 #endif
262
263 #ifdef HAVE_SGTTY
264 fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n",
265 state->sgttyb.sg_flags);
266
267 fprintf_filtered (stream, "tchars: ");
268 for (i = 0; i < (int) sizeof (struct tchars); i++)
269 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]);
270 fprintf_filtered (stream, "\n");
271
272 fprintf_filtered (stream, "ltchars: ");
273 for (i = 0; i < (int) sizeof (struct ltchars); i++)
274 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]);
275 fprintf_filtered (stream, "\n");
276
277 fprintf_filtered (stream, "lmode: 0x%x\n", state->lmode);
278 #endif
279 }
280
281 /* Wait for the output to drain away, as opposed to flushing (discarding) it */
282
283 static int
284 hardwire_drain_output (struct serial *scb)
285 {
286 #ifdef HAVE_TERMIOS
287 return tcdrain (scb->fd);
288 #endif
289
290 #ifdef HAVE_TERMIO
291 return ioctl (scb->fd, TCSBRK, 1);
292 #endif
293
294 #ifdef HAVE_SGTTY
295 /* Get the current state and then restore it using TIOCSETP,
296 which should cause the output to drain and pending input
297 to be discarded. */
298 {
299 struct hardwire_ttystate state;
300 if (get_tty_state (scb, &state))
301 {
302 return (-1);
303 }
304 else
305 {
306 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
307 }
308 }
309 #endif
310 }
311
312 static int
313 hardwire_flush_output (struct serial *scb)
314 {
315 #ifdef HAVE_TERMIOS
316 return tcflush (scb->fd, TCOFLUSH);
317 #endif
318
319 #ifdef HAVE_TERMIO
320 return ioctl (scb->fd, TCFLSH, 1);
321 #endif
322
323 #ifdef HAVE_SGTTY
324 /* This flushes both input and output, but we can't do better. */
325 return ioctl (scb->fd, TIOCFLUSH, 0);
326 #endif
327 }
328
329 static int
330 hardwire_flush_input (struct serial *scb)
331 {
332 ser_base_flush_input (scb);
333
334 #ifdef HAVE_TERMIOS
335 return tcflush (scb->fd, TCIFLUSH);
336 #endif
337
338 #ifdef HAVE_TERMIO
339 return ioctl (scb->fd, TCFLSH, 0);
340 #endif
341
342 #ifdef HAVE_SGTTY
343 /* This flushes both input and output, but we can't do better. */
344 return ioctl (scb->fd, TIOCFLUSH, 0);
345 #endif
346 }
347
348 static int
349 hardwire_send_break (struct serial *scb)
350 {
351 #ifdef HAVE_TERMIOS
352 return tcsendbreak (scb->fd, 0);
353 #endif
354
355 #ifdef HAVE_TERMIO
356 return ioctl (scb->fd, TCSBRK, 0);
357 #endif
358
359 #ifdef HAVE_SGTTY
360 {
361 int status;
362 struct timeval timeout;
363
364 status = ioctl (scb->fd, TIOCSBRK, 0);
365
366 /* Can't use usleep; it doesn't exist in BSD 4.2. */
367 /* Note that if this select() is interrupted by a signal it will not wait
368 the full length of time. I think that is OK. */
369 timeout.tv_sec = 0;
370 timeout.tv_usec = 250000;
371 select (0, 0, 0, 0, &timeout);
372 status = ioctl (scb->fd, TIOCCBRK, 0);
373 return status;
374 }
375 #endif
376 }
377
378 static void
379 hardwire_raw (struct serial *scb)
380 {
381 struct hardwire_ttystate state;
382
383 if (get_tty_state (scb, &state))
384 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
385
386 #ifdef HAVE_TERMIOS
387 state.termios.c_iflag = 0;
388 state.termios.c_oflag = 0;
389 state.termios.c_lflag = 0;
390 state.termios.c_cflag &= ~(CSIZE | PARENB);
391 state.termios.c_cflag |= CLOCAL | CS8;
392 state.termios.c_cc[VMIN] = 0;
393 state.termios.c_cc[VTIME] = 0;
394 #endif
395
396 #ifdef HAVE_TERMIO
397 state.termio.c_iflag = 0;
398 state.termio.c_oflag = 0;
399 state.termio.c_lflag = 0;
400 state.termio.c_cflag &= ~(CSIZE | PARENB);
401 state.termio.c_cflag |= CLOCAL | CS8;
402 state.termio.c_cc[VMIN] = 0;
403 state.termio.c_cc[VTIME] = 0;
404 #endif
405
406 #ifdef HAVE_SGTTY
407 state.sgttyb.sg_flags |= RAW | ANYP;
408 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
409 #endif
410
411 scb->current_timeout = 0;
412
413 if (set_tty_state (scb, &state))
414 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
415 }
416
417 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
418 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
419
420 For termio{s}, we actually just setup VTIME if necessary, and let the
421 timeout occur in the read() in hardwire_read().
422 */
423
424 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
425 ser_unix*() until the old TERMIOS/SGTTY/... timer code has been
426 flushed. . */
427
428 /* NOTE: cagney/1999-09-30: Much of the code below is dead. The only
429 possible values of the TIMEOUT parameter are ONE and ZERO.
430 Consequently all the code that tries to handle the possability of
431 an overflowed timer is unnecessary. */
432
433 static int
434 wait_for (struct serial *scb, int timeout)
435 {
436 #ifdef HAVE_SGTTY
437 while (1)
438 {
439 struct timeval tv;
440 fd_set readfds;
441 int numfds;
442
443 /* NOTE: Some OS's can scramble the READFDS when the select()
444 call fails (ex the kernel with Red Hat 5.2). Initialize all
445 arguments before each call. */
446
447 tv.tv_sec = timeout;
448 tv.tv_usec = 0;
449
450 FD_ZERO (&readfds);
451 FD_SET (scb->fd, &readfds);
452
453 if (timeout >= 0)
454 numfds = select (scb->fd + 1, &readfds, 0, 0, &tv);
455 else
456 numfds = select (scb->fd + 1, &readfds, 0, 0, 0);
457
458 if (numfds <= 0)
459 if (numfds == 0)
460 return SERIAL_TIMEOUT;
461 else if (errno == EINTR)
462 continue;
463 else
464 return SERIAL_ERROR; /* Got an error from select or poll */
465
466 return 0;
467 }
468 #endif /* HAVE_SGTTY */
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
479 if (get_tty_state (scb, &state))
480 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
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))
532 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
533
534 return 0;
535 }
536 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
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). */
543
544 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
545 ser_unix*() until the old TERMIOS/SGTTY/... timer code has been
546 flushed. */
547
548 /* NOTE: cagney/1999-09-16: This function is not identical to
549 ser_unix_readchar() as part of replacing it with ser_unix*()
550 merging will be required - this code handles the case where read()
551 times out due to no data while ser_unix_readchar() doesn't expect
552 that. */
553
554 static int
555 do_hardwire_readchar (struct serial *scb, int timeout)
556 {
557 int status, delta;
558 int detach = 0;
559
560 if (timeout > 0)
561 timeout++;
562
563 /* We have to be able to keep the GUI alive here, so we break the
564 original timeout into steps of 1 second, running the "keep the
565 GUI alive" hook each time through the loop.
566
567 Also, timeout = 0 means to poll, so we just set the delta to 0,
568 so we will only go through the loop once. */
569
570 delta = (timeout == 0 ? 0 : 1);
571 while (1)
572 {
573
574 /* N.B. The UI may destroy our world (for instance by calling
575 remote_stop,) in which case we want to get out of here as
576 quickly as possible. It is not safe to touch scb, since
577 someone else might have freed it. The
578 deprecated_ui_loop_hook signals that we should exit by
579 returning 1. */
580
581 if (deprecated_ui_loop_hook)
582 detach = deprecated_ui_loop_hook (0);
583
584 if (detach)
585 return SERIAL_TIMEOUT;
586
587 scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
588 status = wait_for (scb, delta);
589
590 if (status < 0)
591 return status;
592
593 status = read (scb->fd, scb->buf, BUFSIZ);
594
595 if (status <= 0)
596 {
597 if (status == 0)
598 {
599 /* Zero characters means timeout (it could also be EOF, but
600 we don't (yet at least) distinguish). */
601 if (scb->timeout_remaining > 0)
602 {
603 timeout = scb->timeout_remaining;
604 continue;
605 }
606 else if (scb->timeout_remaining < 0)
607 continue;
608 else
609 return SERIAL_TIMEOUT;
610 }
611 else if (errno == EINTR)
612 continue;
613 else
614 return SERIAL_ERROR; /* Got an error from read */
615 }
616
617 scb->bufcnt = status;
618 scb->bufcnt--;
619 scb->bufp = scb->buf;
620 return *scb->bufp++;
621 }
622 }
623
624 static int
625 hardwire_readchar (struct serial *scb, int timeout)
626 {
627 return generic_readchar (scb, timeout, do_hardwire_readchar);
628 }
629
630
631 #ifndef B19200
632 #define B19200 EXTA
633 #endif
634
635 #ifndef B38400
636 #define B38400 EXTB
637 #endif
638
639 /* Translate baud rates from integers to damn B_codes. Unix should
640 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
641
642 static struct
643 {
644 int rate;
645 int code;
646 }
647 baudtab[] =
648 {
649 {
650 50, B50
651 }
652 ,
653 {
654 75, B75
655 }
656 ,
657 {
658 110, B110
659 }
660 ,
661 {
662 134, B134
663 }
664 ,
665 {
666 150, B150
667 }
668 ,
669 {
670 200, B200
671 }
672 ,
673 {
674 300, B300
675 }
676 ,
677 {
678 600, B600
679 }
680 ,
681 {
682 1200, B1200
683 }
684 ,
685 {
686 1800, B1800
687 }
688 ,
689 {
690 2400, B2400
691 }
692 ,
693 {
694 4800, B4800
695 }
696 ,
697 {
698 9600, B9600
699 }
700 ,
701 {
702 19200, B19200
703 }
704 ,
705 {
706 38400, B38400
707 }
708 ,
709 #ifdef B57600
710 {
711 57600, B57600
712 }
713 ,
714 #endif
715 #ifdef B115200
716 {
717 115200, B115200
718 }
719 ,
720 #endif
721 #ifdef B230400
722 {
723 230400, B230400
724 }
725 ,
726 #endif
727 #ifdef B460800
728 {
729 460800, B460800
730 }
731 ,
732 #endif
733 {
734 -1, -1
735 }
736 ,
737 };
738
739 static int
740 rate_to_code (int rate)
741 {
742 int i;
743
744 for (i = 0; baudtab[i].rate != -1; i++)
745 {
746 /* test for perfect macth. */
747 if (rate == baudtab[i].rate)
748 return baudtab[i].code;
749 else
750 {
751 /* check if it is in between valid values. */
752 if (rate < baudtab[i].rate)
753 {
754 if (i)
755 {
756 warning (_("Invalid baud rate %d. Closest values are %d and %d."),
757 rate, baudtab[i - 1].rate, baudtab[i].rate);
758 }
759 else
760 {
761 warning (_("Invalid baud rate %d. Minimum value is %d."),
762 rate, baudtab[0].rate);
763 }
764 return -1;
765 }
766 }
767 }
768
769 /* The requested speed was too large. */
770 warning (_("Invalid baud rate %d. Maximum value is %d."),
771 rate, baudtab[i - 1].rate);
772 return -1;
773 }
774
775 static int
776 hardwire_setbaudrate (struct serial *scb, int rate)
777 {
778 struct hardwire_ttystate state;
779 int baud_code = rate_to_code (rate);
780
781 if (baud_code < 0)
782 {
783 /* The baud rate was not valid.
784 A warning has already been issued. */
785 errno = EINVAL;
786 return -1;
787 }
788
789 if (get_tty_state (scb, &state))
790 return -1;
791
792 #ifdef HAVE_TERMIOS
793 cfsetospeed (&state.termios, baud_code);
794 cfsetispeed (&state.termios, baud_code);
795 #endif
796
797 #ifdef HAVE_TERMIO
798 #ifndef CIBAUD
799 #define CIBAUD CBAUD
800 #endif
801
802 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
803 state.termio.c_cflag |= baud_code;
804 #endif
805
806 #ifdef HAVE_SGTTY
807 state.sgttyb.sg_ispeed = baud_code;
808 state.sgttyb.sg_ospeed = baud_code;
809 #endif
810
811 return set_tty_state (scb, &state);
812 }
813
814 static int
815 hardwire_setstopbits (struct serial *scb, int num)
816 {
817 struct hardwire_ttystate state;
818 int newbit;
819
820 if (get_tty_state (scb, &state))
821 return -1;
822
823 switch (num)
824 {
825 case SERIAL_1_STOPBITS:
826 newbit = 0;
827 break;
828 case SERIAL_1_AND_A_HALF_STOPBITS:
829 case SERIAL_2_STOPBITS:
830 newbit = 1;
831 break;
832 default:
833 return 1;
834 }
835
836 #ifdef HAVE_TERMIOS
837 if (!newbit)
838 state.termios.c_cflag &= ~CSTOPB;
839 else
840 state.termios.c_cflag |= CSTOPB; /* two bits */
841 #endif
842
843 #ifdef HAVE_TERMIO
844 if (!newbit)
845 state.termio.c_cflag &= ~CSTOPB;
846 else
847 state.termio.c_cflag |= CSTOPB; /* two bits */
848 #endif
849
850 #ifdef HAVE_SGTTY
851 return 0; /* sgtty doesn't support this */
852 #endif
853
854 return set_tty_state (scb, &state);
855 }
856
857 static void
858 hardwire_close (struct serial *scb)
859 {
860 if (scb->fd < 0)
861 return;
862
863 close (scb->fd);
864 scb->fd = -1;
865 }
866
867 \f
868 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
869 otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
870
871 static int
872 ser_unix_wait_for (struct serial *scb, int timeout)
873 {
874 while (1)
875 {
876 int numfds;
877 struct timeval tv;
878 fd_set readfds, exceptfds;
879
880 /* NOTE: Some OS's can scramble the READFDS when the select()
881 call fails (ex the kernel with Red Hat 5.2). Initialize all
882 arguments before each call. */
883
884 tv.tv_sec = timeout;
885 tv.tv_usec = 0;
886
887 FD_ZERO (&readfds);
888 FD_ZERO (&exceptfds);
889 FD_SET (scb->fd, &readfds);
890 FD_SET (scb->fd, &exceptfds);
891
892 if (timeout >= 0)
893 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
894 else
895 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, 0);
896
897 if (numfds <= 0)
898 {
899 if (numfds == 0)
900 return SERIAL_TIMEOUT;
901 else if (errno == EINTR)
902 continue;
903 else
904 return SERIAL_ERROR; /* Got an error from select or poll */
905 }
906
907 return 0;
908 }
909 }
910
911 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
912 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
913 char if successful. Returns -2 if timeout expired, EOF if line dropped
914 dead, or -3 for any other error (see errno in that case). */
915
916 static int
917 do_unix_readchar (struct serial *scb, int timeout)
918 {
919 int status;
920 int delta;
921
922 /* We have to be able to keep the GUI alive here, so we break the
923 original timeout into steps of 1 second, running the "keep the
924 GUI alive" hook each time through the loop.
925
926 Also, timeout = 0 means to poll, so we just set the delta to 0,
927 so we will only go through the loop once. */
928
929 delta = (timeout == 0 ? 0 : 1);
930 while (1)
931 {
932
933 /* N.B. The UI may destroy our world (for instance by calling
934 remote_stop,) in which case we want to get out of here as
935 quickly as possible. It is not safe to touch scb, since
936 someone else might have freed it. The
937 deprecated_ui_loop_hook signals that we should exit by
938 returning 1. */
939
940 if (deprecated_ui_loop_hook)
941 {
942 if (deprecated_ui_loop_hook (0))
943 return SERIAL_TIMEOUT;
944 }
945
946 status = ser_unix_wait_for (scb, delta);
947 if (timeout > 0)
948 timeout -= delta;
949
950 /* If we got a character or an error back from wait_for, then we can
951 break from the loop before the timeout is completed. */
952
953 if (status != SERIAL_TIMEOUT)
954 {
955 break;
956 }
957
958 /* If we have exhausted the original timeout, then generate
959 a SERIAL_TIMEOUT, and pass it out of the loop. */
960
961 else if (timeout == 0)
962 {
963 status = SERIAL_TIMEOUT;
964 break;
965 }
966 }
967
968 if (status < 0)
969 return status;
970
971 while (1)
972 {
973 status = read (scb->fd, scb->buf, BUFSIZ);
974 if (status != -1 || errno != EINTR)
975 break;
976 }
977
978 if (status <= 0)
979 {
980 if (status == 0)
981 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
982 distinguish between EOF & timeouts
983 someday] */
984 else
985 return SERIAL_ERROR; /* Got an error from read */
986 }
987
988 scb->bufcnt = status;
989 scb->bufcnt--;
990 scb->bufp = scb->buf;
991 return *scb->bufp++;
992 }
993
994 /* Perform operations common to both old and new readchar. */
995
996 /* Return the next character from the input FIFO. If the FIFO is
997 empty, call the SERIAL specific routine to try and read in more
998 characters.
999
1000 Initially data from the input FIFO is returned (fd_event()
1001 pre-reads the input into that FIFO. Once that has been emptied,
1002 further data is obtained by polling the input FD using the device
1003 specific readchar() function. Note: reschedule() is called after
1004 every read. This is because there is no guarentee that the lower
1005 level fd_event() poll_event() code (which also calls reschedule())
1006 will be called. */
1007
1008 static int
1009 generic_readchar (struct serial *scb, int timeout,
1010 int (do_readchar) (struct serial *scb, int timeout))
1011 {
1012 int ch;
1013 if (scb->bufcnt > 0)
1014 {
1015 ch = *scb->bufp;
1016 scb->bufcnt--;
1017 scb->bufp++;
1018 }
1019 else if (scb->bufcnt < 0)
1020 {
1021 /* Some errors/eof are are sticky. */
1022 ch = scb->bufcnt;
1023 }
1024 else
1025 {
1026 ch = do_readchar (scb, timeout);
1027 if (ch < 0)
1028 {
1029 switch ((enum serial_rc) ch)
1030 {
1031 case SERIAL_EOF:
1032 case SERIAL_ERROR:
1033 /* Make the error/eof stick. */
1034 scb->bufcnt = ch;
1035 break;
1036 case SERIAL_TIMEOUT:
1037 scb->bufcnt = 0;
1038 break;
1039 }
1040 }
1041 }
1042 reschedule (scb);
1043 return ch;
1044 }
1045
1046 int
1047 ser_unix_readchar (struct serial *scb, int timeout)
1048 {
1049 return generic_readchar (scb, timeout, do_unix_readchar);
1050 }
1051 \f
1052 void
1053 _initialize_ser_hardwire (void)
1054 {
1055 struct serial_ops *ops = XMALLOC (struct serial_ops);
1056 memset (ops, 0, sizeof (struct serial_ops));
1057 ops->name = "hardwire";
1058 ops->next = 0;
1059 ops->open = hardwire_open;
1060 ops->close = hardwire_close;
1061 /* FIXME: Don't replace this with the equivalent ser_unix*() until
1062 the old TERMIOS/SGTTY/... timer code has been flushed. cagney
1063 1999-09-16. */
1064 ops->readchar = hardwire_readchar;
1065 ops->write = ser_base_write;
1066 ops->flush_output = hardwire_flush_output;
1067 ops->flush_input = hardwire_flush_input;
1068 ops->send_break = hardwire_send_break;
1069 ops->go_raw = hardwire_raw;
1070 ops->get_tty_state = hardwire_get_tty_state;
1071 ops->set_tty_state = hardwire_set_tty_state;
1072 ops->print_tty_state = hardwire_print_tty_state;
1073 ops->noflush_set_tty_state = hardwire_noflush_set_tty_state;
1074 ops->setbaudrate = hardwire_setbaudrate;
1075 ops->setstopbits = hardwire_setstopbits;
1076 ops->drain_output = hardwire_drain_output;
1077 ops->async = ser_base_async;
1078 serial_add_interface (ops);
1079 }
This page took 0.051948 seconds and 4 git commands to generate.