* ser-unix.c (get_tty_state): if a descriptor is not a tty, then
[external/binutils.git] / gdb / ser-unix.c
1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
2    Copyright 1992, 1993 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include "defs.h"
21 #include "serial.h"
22 #include <fcntl.h>
23 #include <sys/types.h>
24
25 #if !defined (HAVE_TERMIOS) && !defined (HAVE_TERMIO) && !defined (HAVE_SGTTY)
26 #define HAVE_SGTTY
27 #endif
28
29 #ifdef HAVE_TERMIOS
30 #include <termios.h>
31 #include <unistd.h>
32
33 struct hardwire_ttystate
34 {
35   struct termios termios;
36   pid_t process_group;
37 };
38 #endif /* termios */
39
40 #ifdef HAVE_TERMIO
41 #include <termio.h>
42
43 /* It is believed that all systems which have added job control to SVR3
44    (e.g. sco) have also added termios.  Even if not, trying to figure out
45    all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
46    bewildering.  So we don't attempt it.  */
47
48 struct hardwire_ttystate
49 {
50   struct termio termio;
51 };
52 #endif /* termio */
53
54 #ifdef HAVE_SGTTY
55 /* Needed for the code which uses select().  We would include <sys/select.h>
56    too if it existed on all systems.  */
57 #include <sys/time.h>
58
59 #include <sgtty.h>
60
61 struct hardwire_ttystate
62 {
63   struct sgttyb sgttyb;
64   struct tchars tc;
65   struct ltchars ltc;
66   /* Line discipline flags.  */
67   int lmode;
68
69 #ifdef SHORT_PGRP
70   /* This is only used for the ultra.  Does it have pid_t?  */
71   short process_group;
72 #else
73   int process_group;
74 #endif
75 };
76 #endif /* sgtty */
77
78 static int hardwire_open PARAMS ((serial_t scb, const char *name));
79 static void hardwire_raw PARAMS ((serial_t scb));
80 static int wait_for PARAMS ((serial_t scb, int timeout));
81 static int hardwire_readchar PARAMS ((serial_t scb, int timeout));
82 static int rate_to_code PARAMS ((int rate));
83 static int hardwire_setbaudrate PARAMS ((serial_t scb, int rate));
84 static int hardwire_write PARAMS ((serial_t scb, const char *str, int len));
85 static void hardwire_restore PARAMS ((serial_t scb));
86 static void hardwire_close PARAMS ((serial_t scb));
87 static int get_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
88 static int set_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
89 static serial_ttystate hardwire_get_tty_state PARAMS ((serial_t scb));
90 static int hardwire_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
91
92 /* Open up a real live device for serial I/O */
93
94 static int
95 hardwire_open(scb, name)
96      serial_t scb;
97      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(scb, state)
108      serial_t scb;
109      struct hardwire_ttystate *state;
110 {
111 #ifdef HAVE_TERMIOS
112   extern int errno;
113   pid_t new_process_group;
114
115   if (tcgetattr(scb->fd, &state->termios) < 0)
116     return -1;
117
118   if (!job_control)
119     return 0;
120
121   /* Apparently, if a tty has no process group, then tcgetpgrp returns -1 with
122      errno == 0.   In this case, set the process group to -1 so that we know to
123      omit resetting it later.  */
124   new_process_group = tcgetpgrp (scb->fd);
125   if ((new_process_group == (pid_t)-1)
126       && (errno != ENOTTY))
127     return -1;
128   errno = 0;
129   state->process_group = new_process_group;
130   return 0;
131 #endif
132
133 #ifdef HAVE_TERMIO
134   if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
135     return -1;
136   return 0;
137 #endif
138
139 #ifdef HAVE_SGTTY
140   if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
141     return -1;
142   if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
143     return -1;
144   if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
145     return -1;
146   if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
147     return -1;
148
149   if (!job_control)
150     return 0;
151
152   return ioctl (scb->fd, TIOCGPGRP, &state->process_group);
153 #endif
154 }
155
156 static int
157 set_tty_state(scb, state)
158      serial_t scb;
159      struct hardwire_ttystate *state;
160 {
161 #ifdef HAVE_TERMIOS
162   if (tcsetattr(scb->fd, TCSANOW, &state->termios) < 0)
163     return -1;
164
165   if (!job_control)
166     return 0;
167
168   /* If the tty had no process group before, then do not reset it. */
169   if (state->process_group == -1)
170     return 0;
171   else
172     return tcsetpgrp (scb->fd, state->process_group);
173 #endif
174
175 #ifdef HAVE_TERMIO
176   if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
177     return -1;
178   return 0;
179 #endif
180
181 #ifdef HAVE_SGTTY
182   if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
183     return -1;
184
185   if (!job_control)
186     return 0;
187
188   return ioctl (scb->fd, TIOCSPGRP, &state->process_group);
189 #endif
190 }
191
192 static serial_ttystate
193 hardwire_get_tty_state(scb)
194      serial_t scb;
195 {
196   struct hardwire_ttystate *state;
197
198   state = (struct hardwire_ttystate *)xmalloc(sizeof *state);
199
200   if (get_tty_state(scb, state))
201     return NULL;
202
203   return (serial_ttystate)state;
204 }
205
206 static int
207 hardwire_set_tty_state(scb, ttystate)
208      serial_t scb;
209      serial_ttystate ttystate;
210 {
211   struct hardwire_ttystate *state;
212
213   state = (struct hardwire_ttystate *)ttystate;
214
215   return set_tty_state(scb, state);
216 }
217
218 static int
219 hardwire_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
220      serial_t scb;
221      serial_ttystate new_ttystate;
222      serial_ttystate old_ttystate;
223 {
224   struct hardwire_ttystate new_state;
225   struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
226
227   new_state = *(struct hardwire_ttystate *)new_ttystate;
228
229 #ifdef HAVE_TERMIOS
230   /* I'm not sure whether this is necessary; the manpage makes no mention
231      of discarding input when switching to/from ICANON.  */
232   if (state->termios.c_lflag & ICANON)
233     new_state.termios.c_lflag |= ICANON;
234   else
235     new_state.termios.c_lflag &= ~ICANON;
236 #endif
237
238 #ifdef HAVE_TERMIO
239   /* I'm not sure whether this is necessary; the manpage makes no mention
240      of discarding input when switching to/from ICANON.  */
241   if (state->termio.c_lflag & ICANON)
242     new_state.termio.c_lflag |= ICANON;
243   else
244     new_state.termio.c_lflag &= ~ICANON;
245 #endif
246
247 #ifdef HAVE_SGTTY
248   if (state->sgttyb.sg_flags & RAW)
249     new_state.sgttyb.sg_flags |= RAW;
250   else
251     new_state.sgttyb.sg_flags &= ~RAW;
252
253   /* I'm not sure whether this is necessary; the manpage just mentions
254      RAW not CBREAK.  */
255   if (state->sgttyb.sg_flags & CBREAK)
256     new_state.sgttyb.sg_flags |= CBREAK;
257   else
258     new_state.sgttyb.sg_flags &= ~CBREAK;
259 #endif
260
261   return set_tty_state (scb, &new_state);
262 }
263
264 static void
265 hardwire_print_tty_state (scb, ttystate)
266      serial_t scb;
267      serial_ttystate ttystate;
268 {
269   struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
270   int i;
271
272 #ifdef HAVE_TERMIOS
273   printf_filtered ("Process group = %d\n", state->process_group);
274
275   printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
276                    state->termios.c_iflag, state->termios.c_oflag);
277   printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x\n",
278                    state->termios.c_cflag, state->termios.c_lflag);
279 #if 0
280   /* This not in POSIX, and is not really documented by those systems
281      which have it (at least not Sun).  */
282   printf_filtered ("c_line = 0x%x.\n", state->termios.c_line);
283 #endif
284   printf_filtered ("c_cc: ");
285   for (i = 0; i < NCCS; i += 1)
286     printf_filtered ("0x%x ", state->termios.c_cc[i]);
287   printf_filtered ("\n");
288 #endif
289
290 #ifdef HAVE_TERMIO
291   printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
292                    state->termio.c_iflag, state->termio.c_oflag);
293   printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
294                    state->termio.c_cflag, state->termio.c_lflag,
295                    state->termio.c_line);
296   printf_filtered ("c_cc: ");
297   for (i = 0; i < NCC; i += 1)
298     printf_filtered ("0x%x ", state->termio.c_cc[i]);
299   printf_filtered ("\n");
300 #endif
301
302 #ifdef HAVE_SGTTY
303   printf_filtered ("Process group = %d\n", state->process_group);
304
305   printf_filtered ("sgttyb.sg_flags = 0x%x.\n", state->sgttyb.sg_flags);
306
307   printf_filtered ("tchars: ");
308   for (i = 0; i < (int)sizeof (struct tchars); i++)
309     printf_filtered ("0x%x ", ((unsigned char *)&state->tc)[i]);
310   printf_filtered ("\n");
311
312   printf_filtered ("ltchars: ");
313   for (i = 0; i < (int)sizeof (struct ltchars); i++)
314     printf_filtered ("0x%x ", ((unsigned char *)&state->ltc)[i]);
315   printf_filtered ("\n");
316
317   printf_filtered ("lmode:  0x%x\n", state->lmode);
318 #endif
319 }
320
321 static int
322 hardwire_flush_output (scb)
323      serial_t scb;
324 {
325 #ifdef HAVE_TERMIOS
326   return tcflush (scb->fd, TCOFLUSH);
327 #endif
328
329 #ifdef HAVE_TERMIO
330   return ioctl (scb->fd, TCFLSH, 1);
331 #endif
332
333 #ifdef HAVE_SGTTY
334   /* This flushes both input and output, but we can't do better.  */
335   return ioctl (scb->fd, TIOCFLUSH, 0);
336 #endif  
337 }
338
339 static int
340 hardwire_flush_input (scb)
341      serial_t scb;
342 {
343 #ifdef HAVE_TERMIOS
344   return tcflush (scb->fd, TCIFLUSH);
345 #endif
346
347 #ifdef HAVE_TERMIO
348   return ioctl (scb->fd, TCFLSH, 0);
349 #endif
350
351 #ifdef HAVE_SGTTY
352   /* This flushes both input and output, but we can't do better.  */
353   return ioctl (scb->fd, TIOCFLUSH, 0);
354 #endif  
355 }
356
357 static int
358 hardwire_send_break (scb)
359      serial_t scb;
360 {
361   int status;
362
363 #ifdef HAVE_TERMIOS
364   return tcsendbreak (scb->fd, 0);
365 #endif
366
367 #ifdef HAVE_TERMIO
368   return ioctl (scb->fd, TCSBRK, 0);
369 #endif
370
371 #ifdef HAVE_SGTTY
372   {
373     struct timeval timeout;
374
375     status = ioctl (scb->fd, TIOCSBRK, 0);
376
377     /* Can't use usleep; it doesn't exist in BSD 4.2.  */
378     /* Note that if this select() is interrupted by a signal it will not wait
379        the full length of time.  I think that is OK.  */
380     timeout.tv_sec = 0;
381     timeout.tv_usec = 250000;
382     select (0, 0, 0, 0, &timeout);
383     status = ioctl (scb->fd, TIOCCBRK, 0);
384     return status;
385   }
386 #endif  
387 }
388
389 static void
390 hardwire_raw(scb)
391      serial_t scb;
392 {
393   struct hardwire_ttystate state;
394
395   if (get_tty_state(scb, &state))
396     fprintf(stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
397
398 #ifdef HAVE_TERMIOS
399   state.termios.c_iflag = 0;
400   state.termios.c_oflag = 0;
401   state.termios.c_lflag = 0;
402   state.termios.c_cflag &= ~(CSIZE|PARENB);
403   state.termios.c_cflag |= CS8;
404   state.termios.c_cc[VMIN] = 0;
405   state.termios.c_cc[VTIME] = 0;
406 #endif
407
408 #ifdef HAVE_TERMIO
409   state.termio.c_iflag = 0;
410   state.termio.c_oflag = 0;
411   state.termio.c_lflag = 0;
412   state.termio.c_cflag &= ~(CSIZE|PARENB);
413   state.termio.c_cflag |= CS8;
414   state.termio.c_cc[VMIN] = 0;
415   state.termio.c_cc[VTIME] = 0;
416 #endif
417
418 #ifdef HAVE_SGTTY
419   state.sgttyb.sg_flags |= RAW | ANYP;
420   state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
421 #endif
422
423   scb->current_timeout = 0;
424
425   if (set_tty_state (scb, &state))
426     fprintf(stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
427 }
428
429 /* Wait for input on scb, with timeout seconds.  Returns 0 on success,
430    otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
431
432    For termio{s}, we actually just setup VTIME if necessary, and let the
433    timeout occur in the read() in hardwire_read().
434  */
435
436 static int
437 wait_for(scb, timeout)
438      serial_t scb;
439      int timeout;
440 {
441   int numfds;
442
443 #ifdef HAVE_SGTTY
444   struct timeval tv;
445   fd_set readfds;
446
447   FD_ZERO (&readfds);
448
449   tv.tv_sec = timeout;
450   tv.tv_usec = 0;
451
452   FD_SET(scb->fd, &readfds);
453
454   while (1)
455     {
456       if (timeout >= 0)
457         numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
458       else
459         numfds = select(scb->fd+1, &readfds, 0, 0, 0);
460
461       if (numfds <= 0)
462         if (numfds == 0)
463           return SERIAL_TIMEOUT;
464         else if (errno == EINTR)
465           continue;
466         else
467           return SERIAL_ERROR;  /* Got an error from select or poll */
468
469       return 0;
470     }
471
472 #endif  /* HAVE_SGTTY */
473
474 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
475   if (timeout == scb->current_timeout)
476     return 0;
477
478   {
479     struct hardwire_ttystate state;
480
481     if (get_tty_state(scb, &state))
482       fprintf(stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
483
484 #ifdef HAVE_TERMIOS
485     state.termios.c_cc[VTIME] = timeout * 10;
486 #endif
487
488 #ifdef HAVE_TERMIO
489     state.termio.c_cc[VTIME] = timeout * 10;
490 #endif
491
492     scb->current_timeout = timeout;
493
494     if (set_tty_state (scb, &state))
495       fprintf(stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
496
497     return 0;
498   }
499 #endif  /* HAVE_TERMIO || HAVE_TERMIOS */
500 }
501
502 /* Read a character with user-specified timeout.  TIMEOUT is number of seconds
503    to wait, or -1 to wait forever.  Use timeout of 0 to effect a poll.  Returns
504    char if successful.  Returns SERIAL_TIMEOUT if timeout expired, EOF if line
505    dropped dead, or SERIAL_ERROR for any other error (see errno in that case).  */
506
507 static int
508 hardwire_readchar(scb, timeout)
509      serial_t scb;
510      int timeout;
511 {
512   int status;
513
514   if (scb->bufcnt-- > 0)
515     return *scb->bufp++;
516
517   status = wait_for(scb, timeout);
518
519   if (status < 0)
520     return status;
521
522   scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
523
524   if (scb->bufcnt <= 0)
525     if (scb->bufcnt == 0)
526       return SERIAL_TIMEOUT;    /* 0 chars means timeout [may need to
527                                    distinguish between EOF & timeouts
528                                    someday] */
529     else
530       return SERIAL_ERROR;      /* Got an error from read */
531
532   scb->bufcnt--;
533   scb->bufp = scb->buf;
534   return *scb->bufp++;
535 }
536
537 #ifndef B19200
538 #define B19200 EXTA
539 #endif
540
541 #ifndef B38400
542 #define B38400 EXTB
543 #endif
544
545 /* Translate baud rates from integers to damn B_codes.  Unix should
546    have outgrown this crap years ago, but even POSIX wouldn't buck it.  */
547
548 static struct
549 {
550   int rate;
551   int code;
552 }
553 baudtab[] =
554 {
555   {50, B50},
556   {75, B75},
557   {110, B110},
558   {134, B134},
559   {150, B150},
560   {200, B200},
561   {300, B300},
562   {600, B600},
563   {1200, B1200},
564   {1800, B1800},
565   {2400, B2400},
566   {4800, B4800},
567   {9600, B9600},
568   {19200, B19200},
569   {38400, B38400},
570   {-1, -1},
571 };
572
573 static int 
574 rate_to_code(rate)
575      int rate;
576 {
577   int i;
578
579   for (i = 0; baudtab[i].rate != -1; i++)
580     if (rate == baudtab[i].rate)  
581       return baudtab[i].code;
582
583   return -1;
584 }
585
586 static int
587 hardwire_setbaudrate(scb, rate)
588      serial_t scb;
589      int rate;
590 {
591   struct hardwire_ttystate state;
592
593   if (get_tty_state(scb, &state))
594     return -1;
595
596 #ifdef HAVE_TERMIOS
597   cfsetospeed (&state.termios, rate_to_code (rate));
598   cfsetispeed (&state.termios, rate_to_code (rate));
599 #endif
600
601 #ifdef HAVE_TERMIO
602 #ifndef CIBAUD
603 #define CIBAUD CBAUD
604 #endif
605
606   state.termio.c_cflag &= ~(CBAUD | CIBAUD);
607   state.termio.c_cflag |= rate_to_code (rate);
608 #endif
609
610 #ifdef HAVE_SGTTY
611   state.sgttyb.sg_ispeed = rate_to_code (rate);
612   state.sgttyb.sg_ospeed = rate_to_code (rate);
613 #endif
614
615   return set_tty_state (scb, &state);
616 }
617
618 static int
619 hardwire_set_process_group (scb, ttystate, group)
620      serial_t scb;
621      serial_ttystate ttystate;
622      int group;
623 {
624 #if defined (HAVE_SGTTY) || defined (HAVE_TERMIOS)
625   ((struct hardwire_ttystate *)ttystate)->process_group = group;
626 #endif
627   return 0;
628 }
629
630 static int
631 hardwire_write(scb, str, len)
632      serial_t scb;
633      const char *str;
634      int len;
635 {
636   int cc;
637
638   while (len > 0)
639     {
640       cc = write(scb->fd, str, len);
641
642       if (cc < 0)
643         return 1;
644       len -= cc;
645       str += cc;
646     }
647   return 0;
648 }
649
650 static void
651 hardwire_close(scb)
652      serial_t scb;
653 {
654   if (scb->fd < 0)
655     return;
656
657   close(scb->fd);
658   scb->fd = -1;
659 }
660
661 static struct serial_ops hardwire_ops =
662 {
663   "hardwire",
664   0,
665   hardwire_open,
666   hardwire_close,
667   hardwire_readchar,
668   hardwire_write,
669   hardwire_flush_output,
670   hardwire_flush_input,
671   hardwire_send_break,
672   hardwire_raw,
673   hardwire_get_tty_state,
674   hardwire_set_tty_state,
675   hardwire_print_tty_state,
676   hardwire_noflush_set_tty_state,
677   hardwire_setbaudrate,
678   hardwire_set_process_group
679 };
680
681 int job_control;
682 #if defined (HAVE_TERMIOS)
683 #include <unistd.h>
684 #endif
685
686 /* This is here because this is where we figure out whether we (probably)
687    have job control.  Just using job_control only does part of it because
688    setpgid or setpgrp might not exist on a system without job control.
689    It might be considered misplaced (on the other hand, process groups and
690    job control are closely related to ttys).
691
692    For a more clean implementation, in libiberty, put a setpgid which merely
693    calls setpgrp and a setpgrp which does nothing (any system with job control
694    will have one or the other).  */
695 int
696 gdb_setpgid ()
697 {
698   int retval = 0;
699   if (job_control)
700     {
701 #if defined (NEED_POSIX_SETPGID) || defined (HAVE_TERMIOS)
702       /* Do all systems with termios have setpgid?  I hope so.  */
703       /* setpgid (0, 0) is supposed to work and mean the same thing as
704          this, but on Ultrix 4.2A it fails with EPERM (and
705          setpgid (getpid (), getpid ()) succeeds).  */
706       retval = setpgid (getpid (), getpid ());
707 #else
708 #if defined (TIOCGPGRP)
709 #if defined(USG) && !defined(SETPGRP_ARGS)
710       retval = setpgrp ();
711 #else
712       retval = setpgrp (getpid (), getpid ());
713 #endif /* USG */
714 #endif /* TIOCGPGRP.  */
715 #endif /* NEED_POSIX_SETPGID */
716     }
717   return retval;
718 }
719
720 void
721 _initialize_ser_hardwire ()
722 {
723   serial_add_interface (&hardwire_ops);
724
725   /* OK, figure out whether we have job control.  */
726
727 #if defined (HAVE_TERMIOS)
728   /* Do all systems with termios have the POSIX way of identifying job
729      control?  I hope so.  */
730 #ifdef _POSIX_JOB_CONTROL
731   job_control = 1;
732 #else
733   job_control = sysconf (_SC_JOB_CONTROL);
734 #endif
735 #endif /* termios */
736
737 #ifdef HAVE_TERMIO
738   /* See comment at top of file about trying to support process groups
739      with termio.  */
740   job_control = 0;
741 #endif /* termio */
742
743 #ifdef HAVE_SGTTY
744 #ifdef TIOCGPGRP
745   job_control = 1;
746 #else
747   job_control = 0;
748 #endif /* TIOCGPGRP */
749 #endif /* sgtty */
750
751 }