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