* serial.h, ser-{unix,go32,tcp}.c: Add flush_input and send_break.
[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 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   pid_t new_process_group;
113
114   if (tcgetattr(scb->fd, &state->termios) < 0)
115     return -1;
116
117   if (!job_control)
118     return 0;
119
120   new_process_group = tcgetpgrp (scb->fd);
121   if (new_process_group == (pid_t)-1)
122     return -1;
123   state->process_group = new_process_group;
124   return 0;
125 #endif
126
127 #ifdef HAVE_TERMIO
128   if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
129     return -1;
130   return 0;
131 #endif
132
133 #ifdef HAVE_SGTTY
134   if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
135     return -1;
136   if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
137     return -1;
138   if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
139     return -1;
140   if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
141     return -1;
142
143   if (!job_control)
144     return 0;
145
146   return ioctl (scb->fd, TIOCGPGRP, &state->process_group);
147 #endif
148 }
149
150 static int
151 set_tty_state(scb, state)
152      serial_t scb;
153      struct hardwire_ttystate *state;
154 {
155 #ifdef HAVE_TERMIOS
156   if (tcsetattr(scb->fd, TCSANOW, &state->termios) < 0)
157     return -1;
158
159   if (!job_control)
160     return 0;
161
162   return tcsetpgrp (scb->fd, state->process_group);
163 #endif
164
165 #ifdef HAVE_TERMIO
166   if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
167     return -1;
168   return 0;
169 #endif
170
171 #ifdef HAVE_SGTTY
172   if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
173     return -1;
174
175   if (!job_control)
176     return 0;
177
178   return ioctl (scb->fd, TIOCSPGRP, &state->process_group);
179 #endif
180 }
181
182 static serial_ttystate
183 hardwire_get_tty_state(scb)
184      serial_t scb;
185 {
186   struct hardwire_ttystate *state;
187
188   state = (struct hardwire_ttystate *)xmalloc(sizeof *state);
189
190   if (get_tty_state(scb, state))
191     return NULL;
192
193   return (serial_ttystate)state;
194 }
195
196 static int
197 hardwire_set_tty_state(scb, ttystate)
198      serial_t scb;
199      serial_ttystate ttystate;
200 {
201   struct hardwire_ttystate *state;
202
203   state = (struct hardwire_ttystate *)ttystate;
204
205   return set_tty_state(scb, state);
206 }
207
208 static int
209 hardwire_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
210      serial_t scb;
211      serial_ttystate new_ttystate;
212      serial_ttystate old_ttystate;
213 {
214   struct hardwire_ttystate new_state;
215   struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
216
217   new_state = *(struct hardwire_ttystate *)new_ttystate;
218
219 #ifdef HAVE_TERMIOS
220   /* I'm not sure whether this is necessary; the manpage makes no mention
221      of discarding input when switching to/from ICANON.  */
222   if (state->termios.c_lflag & ICANON)
223     new_state.termios.c_lflag |= ICANON;
224   else
225     new_state.termios.c_lflag &= ~ICANON;
226 #endif
227
228 #ifdef HAVE_TERMIO
229   /* I'm not sure whether this is necessary; the manpage makes no mention
230      of discarding input when switching to/from ICANON.  */
231   if (state->termio.c_lflag & ICANON)
232     new_state.termio.c_lflag |= ICANON;
233   else
234     new_state.termio.c_lflag &= ~ICANON;
235 #endif
236
237 #ifdef HAVE_SGTTY
238   if (state->sgttyb.sg_flags & RAW)
239     new_state.sgttyb.sg_flags |= RAW;
240   else
241     new_state.sgttyb.sg_flags &= ~RAW;
242
243   /* I'm not sure whether this is necessary; the manpage just mentions
244      RAW not CBREAK.  */
245   if (state->sgttyb.sg_flags & CBREAK)
246     new_state.sgttyb.sg_flags |= CBREAK;
247   else
248     new_state.sgttyb.sg_flags &= ~CBREAK;
249 #endif
250
251   return set_tty_state (scb, &new_state);
252 }
253
254 static void
255 hardwire_print_tty_state (scb, ttystate)
256      serial_t scb;
257      serial_ttystate ttystate;
258 {
259   struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
260   int i;
261
262 #ifdef HAVE_TERMIOS
263   printf_filtered ("Process group = %d\n", state->process_group);
264
265   printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
266                    state->termios.c_iflag, state->termios.c_oflag);
267   printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x\n",
268                    state->termios.c_cflag, state->termios.c_lflag);
269 #if 0
270   /* This not in POSIX, and is not really documented by those systems
271      which have it (at least not Sun).  */
272   printf_filtered ("c_line = 0x%x.\n", state->termios.c_line);
273 #endif
274   printf_filtered ("c_cc: ");
275   for (i = 0; i < NCCS; i += 1)
276     printf_filtered ("0x%x ", state->termios.c_cc[i]);
277   printf_filtered ("\n");
278 #endif
279
280 #ifdef HAVE_TERMIO
281   printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
282                    state->termio.c_iflag, state->termio.c_oflag);
283   printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
284                    state->termio.c_cflag, state->termio.c_lflag,
285                    state->termio.c_line);
286   printf_filtered ("c_cc: ");
287   for (i = 0; i < NCC; i += 1)
288     printf_filtered ("0x%x ", state->termio.c_cc[i]);
289   printf_filtered ("\n");
290 #endif
291
292 #ifdef HAVE_SGTTY
293   printf_filtered ("Process group = %d\n", state->process_group);
294
295   printf_filtered ("sgttyb.sg_flags = 0x%x.\n", state->sgttyb.sg_flags);
296
297   printf_filtered ("tchars: ");
298   for (i = 0; i < (int)sizeof (struct tchars); i++)
299     printf_filtered ("0x%x ", ((unsigned char *)&state->tc)[i]);
300   printf_filtered ("\n");
301
302   printf_filtered ("ltchars: ");
303   for (i = 0; i < (int)sizeof (struct ltchars); i++)
304     printf_filtered ("0x%x ", ((unsigned char *)&state->ltc)[i]);
305   printf_filtered ("\n");
306
307   printf_filtered ("lmode:  0x%x\n", state->lmode);
308 #endif
309 }
310
311 static int
312 hardwire_flush_output (scb)
313      serial_t 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 (scb)
331      serial_t scb;
332 {
333 #ifdef HAVE_TERMIOS
334   return tcflush (scb->fd, TCIFLUSH);
335 #endif
336
337 #ifdef HAVE_TERMIO
338   return ioctl (scb->fd, TCFLSH, 0);
339 #endif
340
341 #ifdef HAVE_SGTTY
342   /* This flushes both input and output, but we can't do better.  */
343   return ioctl (scb->fd, TIOCFLUSH, 0);
344 #endif  
345 }
346
347 static int
348 hardwire_send_break (scb)
349      serial_t scb;
350 {
351   int status;
352
353 #ifdef HAVE_TERMIOS
354   return tcsendbreak (scb->fd, 0);
355 #endif
356
357 #ifdef HAVE_TERMIO
358   return ioctl (scb->fd, TCSBRK, 0);
359 #endif
360
361 #ifdef HAVE_SGTTY
362   status = ioctl (scb->fd, TIOCSBRK, 0);
363   usleep (250000);
364   status = ioctl (scb->fd, TIOCCBRK, 0);
365   return status;
366 #endif  
367 }
368
369 static void
370 hardwire_raw(scb)
371      serial_t scb;
372 {
373   struct hardwire_ttystate state;
374
375   if (get_tty_state(scb, &state))
376     fprintf(stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
377
378 #ifdef HAVE_TERMIOS
379   state.termios.c_iflag = 0;
380   state.termios.c_oflag = 0;
381   state.termios.c_lflag = 0;
382   state.termios.c_cflag &= ~(CSIZE|PARENB);
383   state.termios.c_cflag |= CS8;
384   state.termios.c_cc[VMIN] = 0;
385   state.termios.c_cc[VTIME] = 0;
386 #endif
387
388 #ifdef HAVE_TERMIO
389   state.termio.c_iflag = 0;
390   state.termio.c_oflag = 0;
391   state.termio.c_lflag = 0;
392   state.termio.c_cflag &= ~(CSIZE|PARENB);
393   state.termio.c_cflag |= CS8;
394   state.termio.c_cc[VMIN] = 0;
395   state.termio.c_cc[VTIME] = 0;
396 #endif
397
398 #ifdef HAVE_SGTTY
399   state.sgttyb.sg_flags |= RAW | ANYP;
400   state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
401 #endif
402
403   scb->current_timeout = 0;
404
405   if (set_tty_state (scb, &state))
406     fprintf(stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
407 }
408
409 /* Wait for input on scb, with timeout seconds.  Returns 0 on success,
410    otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
411
412    For termio{s}, we actually just setup VTIME if necessary, and let the
413    timeout occur in the read() in hardwire_read().
414  */
415
416 static int
417 wait_for(scb, timeout)
418      serial_t scb;
419      int timeout;
420 {
421   int numfds;
422
423 #ifdef HAVE_SGTTY
424   struct timeval tv;
425   fd_set readfds;
426
427   FD_ZERO (&readfds);
428
429   tv.tv_sec = timeout;
430   tv.tv_usec = 0;
431
432   FD_SET(scb->fd, &readfds);
433
434   while (1)
435     {
436       if (timeout >= 0)
437         numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
438       else
439         numfds = select(scb->fd+1, &readfds, 0, 0, 0);
440
441       if (numfds <= 0)
442         if (numfds == 0)
443           return SERIAL_TIMEOUT;
444         else if (errno == EINTR)
445           continue;
446         else
447           return SERIAL_ERROR;  /* Got an error from select or poll */
448
449       return 0;
450     }
451
452 #endif  /* HAVE_SGTTY */
453
454 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
455   if (timeout == scb->current_timeout)
456     return 0;
457
458   {
459     struct hardwire_ttystate state;
460
461     if (get_tty_state(scb, &state))
462       fprintf(stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
463
464 #ifdef HAVE_TERMIOS
465     state.termios.c_cc[VTIME] = timeout * 10;
466 #endif
467
468 #ifdef HAVE_TERMIO
469     state.termio.c_cc[VTIME] = timeout * 10;
470 #endif
471
472     scb->current_timeout = timeout;
473
474     if (set_tty_state (scb, &state))
475       fprintf(stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
476
477     return 0;
478   }
479 #endif  /* HAVE_TERMIO || HAVE_TERMIOS */
480 }
481
482 /* Read a character with user-specified timeout.  TIMEOUT is number of seconds
483    to wait, or -1 to wait forever.  Use timeout of 0 to effect a poll.  Returns
484    char if successful.  Returns -2 if timeout expired, EOF if line dropped
485    dead, or -3 for any other error (see errno in that case). */
486
487 static int
488 hardwire_readchar(scb, timeout)
489      serial_t scb;
490      int timeout;
491 {
492   int status;
493
494   if (scb->bufcnt-- > 0)
495     return *scb->bufp++;
496
497   status = wait_for(scb, timeout);
498
499   if (status < 0)
500     return status;
501
502   scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
503
504   if (scb->bufcnt <= 0)
505     if (scb->bufcnt == 0)
506       return SERIAL_TIMEOUT;    /* 0 chars means timeout [may need to
507                                    distinguish between EOF & timeouts
508                                    someday] */
509     else
510       return SERIAL_ERROR;      /* Got an error from read */
511
512   scb->bufcnt--;
513   scb->bufp = scb->buf;
514   return *scb->bufp++;
515 }
516
517 #ifndef B19200
518 #define B19200 EXTA
519 #endif
520
521 #ifndef B38400
522 #define B38400 EXTB
523 #endif
524
525 /* Translate baud rates from integers to damn B_codes.  Unix should
526    have outgrown this crap years ago, but even POSIX wouldn't buck it.  */
527
528 static struct
529 {
530   int rate;
531   int code;
532 }
533 baudtab[] =
534 {
535   {50, B50},
536   {75, B75},
537   {110, B110},
538   {134, B134},
539   {150, B150},
540   {200, B200},
541   {300, B300},
542   {600, B600},
543   {1200, B1200},
544   {1800, B1800},
545   {2400, B2400},
546   {4800, B4800},
547   {9600, B9600},
548   {19200, B19200},
549   {38400, B38400},
550   {-1, -1},
551 };
552
553 static int 
554 rate_to_code(rate)
555      int rate;
556 {
557   int i;
558
559   for (i = 0; baudtab[i].rate != -1; i++)
560     if (rate == baudtab[i].rate)  
561       return baudtab[i].code;
562
563   return -1;
564 }
565
566 static int
567 hardwire_setbaudrate(scb, rate)
568      serial_t scb;
569      int rate;
570 {
571   struct hardwire_ttystate state;
572
573   if (get_tty_state(scb, &state))
574     return -1;
575
576 #ifdef HAVE_TERMIOS
577   cfsetospeed (&state.termios, rate_to_code (rate));
578   cfsetispeed (&state.termios, rate_to_code (rate));
579 #endif
580
581 #ifdef HAVE_TERMIO
582 #ifndef CIBAUD
583 #define CIBAUD CBAUD
584 #endif
585
586   state.termio.c_cflag &= ~(CBAUD | CIBAUD);
587   state.termio.c_cflag |= rate_to_code (rate);
588 #endif
589
590 #ifdef HAVE_SGTTY
591   state.sgttyb.sg_ispeed = rate_to_code (rate);
592   state.sgttyb.sg_ospeed = rate_to_code (rate);
593 #endif
594
595   return set_tty_state (scb, &state);
596 }
597
598 static int
599 hardwire_set_process_group (scb, ttystate, group)
600      serial_t scb;
601      serial_ttystate ttystate;
602      int group;
603 {
604 #if defined (HAVE_SGTTY) || defined (HAVE_TERMIOS)
605   ((struct hardwire_ttystate *)ttystate)->process_group = group;
606 #endif
607   return 0;
608 }
609
610 static int
611 hardwire_write(scb, str, len)
612      serial_t scb;
613      const char *str;
614      int len;
615 {
616   int cc;
617
618   while (len > 0)
619     {
620       cc = write(scb->fd, str, len);
621
622       if (cc < 0)
623         return 1;
624       len -= cc;
625       str += cc;
626     }
627   return 0;
628 }
629
630 static void
631 hardwire_close(scb)
632      serial_t scb;
633 {
634   if (scb->fd < 0)
635     return;
636
637   close(scb->fd);
638   scb->fd = -1;
639 }
640
641 static struct serial_ops hardwire_ops =
642 {
643   "hardwire",
644   0,
645   hardwire_open,
646   hardwire_close,
647   hardwire_readchar,
648   hardwire_write,
649   hardwire_flush_output,
650   hardwire_flush_input,
651   hardwire_send_break,
652   hardwire_raw,
653   hardwire_get_tty_state,
654   hardwire_set_tty_state,
655   hardwire_print_tty_state,
656   hardwire_noflush_set_tty_state,
657   hardwire_setbaudrate,
658   hardwire_set_process_group
659 };
660
661 int job_control;
662 #if defined (HAVE_TERMIOS)
663 #include <unistd.h>
664 #endif
665
666 /* This is here because this is where we figure out whether we (probably)
667    have job control.  Just using job_control only does part of it because
668    setpgid or setpgrp might not exist on a system without job control.
669    It might be considered misplaced (on the other hand, process groups and
670    job control are closely related to ttys).
671
672    For a more clean implementation, in libiberty, put a setpgid which merely
673    calls setpgrp and a setpgrp which does nothing (any system with job control
674    will have one or the other).  */
675 int
676 gdb_setpgid ()
677 {
678   int retval = 0;
679   if (job_control)
680     {
681 #if defined (NEED_POSIX_SETPGID) || defined (HAVE_TERMIOS)
682       /* Do all systems with termios have setpgid?  I hope so.  */
683       retval = setpgid (0, 0);
684 #else
685 #if defined (TIOCGPGRP)
686 #if defined(USG) && !defined(SETPGRP_ARGS)
687       retval = setpgrp ();
688 #else
689       retval = setpgrp (getpid (), getpid ());
690 #endif /* USG */
691 #endif /* TIOCGPGRP.  */
692 #endif /* NEED_POSIX_SETPGID */
693     }
694   return retval;
695 }
696
697 void
698 _initialize_ser_hardwire ()
699 {
700   serial_add_interface (&hardwire_ops);
701
702   /* OK, figure out whether we have job control.  */
703
704 #if defined (HAVE_TERMIOS)
705   /* Do all systems with termios have the POSIX way of identifying job
706      control?  I hope so.  */
707 #ifdef _POSIX_JOB_CONTROL
708   job_control = 1;
709 #else
710   job_control = sysconf (_SC_JOB_CONTROL);
711 #endif
712 #endif /* termios */
713
714 #ifdef HAVE_TERMIO
715   /* See comment at top of file about trying to support process groups
716      with termio.  */
717   job_control = 0;
718 #endif /* termio */
719
720 #ifdef HAVE_SGTTY
721 #ifdef TIOCGPGRP
722   job_control = 1;
723 #else
724   job_control = 0;
725 #endif /* TIOCGPGRP */
726 #endif /* sgtty */
727
728 }