* ser-unix.c (hardwire_print_tty_state) [HAVE_TERMIOS]: Don't
[deliverable/binutils-gdb.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
39
40 #ifdef HAVE_TERMIO
41 #include <termio.h>
42
43 struct hardwire_ttystate
44 {
45 struct termio termio;
46 int process_group;
47 };
48 #endif
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 #include <sgtty.h>
56
57 struct hardwire_ttystate
58 {
59 struct sgttyb sgttyb;
60 struct tchars tc;
61 struct ltchars ltc;
62 /* Line discipline flags. */
63 int lmode;
64
65 #ifdef SHORT_PGRP
66 /* This is only used for the ultra. Does it have pid_t? */
67 short process_group;
68 #else
69 int process_group;
70 #endif
71 };
72 #endif
73
74 static int hardwire_open PARAMS ((serial_t scb, const char *name));
75 static void hardwire_raw PARAMS ((serial_t scb));
76 static int wait_for PARAMS ((serial_t scb, int timeout));
77 static int hardwire_readchar PARAMS ((serial_t scb, int timeout));
78 static int rate_to_code PARAMS ((int rate));
79 static int hardwire_setbaudrate PARAMS ((serial_t scb, int rate));
80 static int hardwire_write PARAMS ((serial_t scb, const char *str, int len));
81 static void hardwire_restore PARAMS ((serial_t scb));
82 static void hardwire_close PARAMS ((serial_t scb));
83 static int get_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
84 static int set_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
85 static serial_ttystate hardwire_get_tty_state PARAMS ((serial_t scb));
86 static int hardwire_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
87
88 /* Open up a real live device for serial I/O */
89
90 static int
91 hardwire_open(scb, name)
92 serial_t scb;
93 const char *name;
94 {
95 scb->fd = open (name, O_RDWR);
96 if (scb->fd < 0)
97 return -1;
98
99 return 0;
100 }
101
102 static int
103 get_tty_state(scb, state)
104 serial_t scb;
105 struct hardwire_ttystate *state;
106 {
107 #ifdef HAVE_TERMIOS
108 pid_t new_process_group;
109
110 if (tcgetattr(scb->fd, &state->termios) < 0)
111 return -1;
112
113 if (!job_control)
114 return 0;
115
116 new_process_group = tcgetpgrp (scb->fd);
117 if (new_process_group == (pid_t)-1)
118 return -1;
119 state->process_group = new_process_group;
120 return 0;
121 #endif
122
123 #ifdef HAVE_TERMIO
124 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
125 return -1;
126
127 if (!job_control)
128 return 0;
129
130 return ioctl (scb->fd, TIOCGPGRP, &state->process_group);
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 /* Need to ignore errors, at least if attach_flag is set. */
163 tcsetpgrp (scb->fd, state->process_group);
164 return 0;
165 #endif
166
167 #ifdef HAVE_TERMIO
168 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
169 return -1;
170
171 if (!job_control)
172 return 0;
173
174 /* Need to ignore errors, at least if attach_flag is set. */
175 ioctl (scb->fd, TIOCSPGRP, &state->process_group);
176 return 0;
177 #endif
178
179 #ifdef HAVE_SGTTY
180 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
181 return -1;
182
183 if (!job_control)
184 return 0;
185
186 /* Need to ignore errors, at least if attach_flag is set. */
187 ioctl (scb->fd, TIOCSPGRP, &state->process_group);
188 return 0;
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 *)new_ttystate;
226 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
227
228 #ifdef HAVE_TERMIOS
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->termios.c_lflag & ICANON)
232 new_state.termios.c_lflag |= ICANON;
233 else
234 new_state.termios.c_lflag &= ~ICANON;
235 #endif
236
237 #ifdef HAVE_TERMIO
238 /* I'm not sure whether this is necessary; the manpage makes no mention
239 of discarding input when switching to/from ICANON. */
240 if (state->termio.c_lflag & ICANON)
241 new_state.termio.c_lflag |= ICANON;
242 else
243 new_state.termio.c_lflag &= ~ICANON;
244 #endif
245
246 #ifdef HAVE_SGTTY
247 if (state->sgttyb.sg_flags & RAW)
248 new_state.sgttyb.sg_flags |= RAW;
249 else
250 new_state.sgttyb.sg_flags &= ~RAW;
251
252 /* I'm not sure whether this is necessary; the manpage just mentions
253 RAW not CBREAK. */
254 if (state->sgttyb.sg_flags & CBREAK)
255 new_state.sgttyb.sg_flags |= CBREAK;
256 else
257 new_state.sgttyb.sg_flags &= ~CBREAK;
258 #endif
259
260 return set_tty_state (scb, &new_state);
261 }
262
263 static void
264 hardwire_print_tty_state (scb, ttystate)
265 serial_t scb;
266 serial_ttystate ttystate;
267 {
268 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
269 int i;
270
271 printf_filtered ("Process group = %d\n", state->process_group);
272
273 #ifdef HAVE_TERMIOS
274 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
275 state->termios.c_iflag, state->termios.c_oflag);
276 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x\n",
277 state->termios.c_cflag, state->termios.c_lflag);
278 #if 0
279 /* This not in POSIX, and is not really documented by those systems
280 which have it (at least not Sun). */
281 printf_filtered ("c_line = 0x%x.\n", state->termios.c_line);
282 #endif
283 printf_filtered ("c_cc: ");
284 for (i = 0; i < NCCS; i += 1)
285 printf_filtered ("0x%x ", state->termios.c_cc[i]);
286 printf_filtered ("\n");
287 #endif
288
289 #ifdef HAVE_TERMIO
290 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
291 state->termio.c_iflag, state->termio.c_oflag);
292 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
293 state->termio.c_cflag, state->termio.c_lflag,
294 state->termio.c_line);
295 printf_filtered ("c_cc: ");
296 for (i = 0; i < NCC; i += 1)
297 printf_filtered ("0x%x ", state->termio.c_cc[i]);
298 printf_filtered ("\n");
299 #endif
300
301 #ifdef HAVE_SGTTY
302 printf_filtered ("sgttyb.sg_flags = 0x%x.\n", state->sgttyb.sg_flags);
303
304 printf_filtered ("tchars: ");
305 for (i = 0; i < (int)sizeof (struct tchars); i++)
306 printf_filtered ("0x%x ", ((unsigned char *)&state->tc)[i]);
307 printf_filtered ("\n");
308
309 printf_filtered ("ltchars: ");
310 for (i = 0; i < (int)sizeof (struct ltchars); i++)
311 printf_filtered ("0x%x ", ((unsigned char *)&state->ltc)[i]);
312 printf_filtered ("\n");
313
314 printf_filtered ("lmode: 0x%x\n", state->lmode);
315 #endif
316 }
317
318 static int
319 hardwire_flush_output (scb)
320 serial_t scb;
321 {
322 #ifdef HAVE_TERMIOS
323 return tcflush (scb->fd, TCOFLUSH);
324 #endif
325
326 #ifdef HAVE_TERMIO
327 return ioctl (scb->fd, TCFLSH, 1);
328 #endif
329
330 #ifdef HAVE_SGTTY
331 /* This flushes both input and output, but we can't do better. */
332 return ioctl (scb->fd, TIOCFLUSH, 0);
333 #endif
334 }
335
336 static void
337 hardwire_raw(scb)
338 serial_t scb;
339 {
340 struct hardwire_ttystate state;
341
342 if (get_tty_state(scb, &state))
343 fprintf(stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
344
345 #ifdef HAVE_TERMIOS
346 state.termios.c_iflag = 0;
347 state.termios.c_oflag = 0;
348 state.termios.c_lflag = 0;
349 state.termios.c_cflag &= ~(CSIZE|PARENB);
350 state.termios.c_cflag |= CS8;
351 state.termios.c_cc[VMIN] = 0;
352 state.termios.c_cc[VTIME] = 0;
353 #endif
354
355 #ifdef HAVE_TERMIO
356 state.termio.c_iflag = 0;
357 state.termio.c_oflag = 0;
358 state.termio.c_lflag = 0;
359 state.termio.c_cflag &= ~(CSIZE|PARENB);
360 state.termio.c_cflag |= CS8;
361 state.termio.c_cc[VMIN] = 0;
362 state.termio.c_cc[VTIME] = 0;
363 #endif
364
365 #ifdef HAVE_SGTTY
366 state.sgttyb.sg_flags |= RAW | ANYP;
367 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
368 #endif
369
370 scb->current_timeout = 0;
371
372 if (set_tty_state (scb, &state))
373 fprintf(stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
374 }
375
376 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
377 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
378
379 For termio{s}, we actually just setup VTIME if necessary, and let the
380 timeout occur in the read() in hardwire_read().
381 */
382
383 static int
384 wait_for(scb, timeout)
385 serial_t scb;
386 int timeout;
387 {
388 int numfds;
389
390 #ifdef HAVE_SGTTY
391 struct timeval tv;
392 fd_set readfds;
393
394 FD_ZERO (&readfds);
395
396 tv.tv_sec = timeout;
397 tv.tv_usec = 0;
398
399 FD_SET(scb->fd, &readfds);
400
401 while (1)
402 {
403 if (timeout >= 0)
404 numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
405 else
406 numfds = select(scb->fd+1, &readfds, 0, 0, 0);
407
408 if (numfds <= 0)
409 if (numfds == 0)
410 return SERIAL_TIMEOUT;
411 else if (errno == EINTR)
412 continue;
413 else
414 return SERIAL_ERROR; /* Got an error from select or poll */
415
416 return 0;
417 }
418
419 #endif /* HAVE_SGTTY */
420
421 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
422 if (timeout == scb->current_timeout)
423 return 0;
424
425 {
426 struct hardwire_ttystate state;
427
428 if (get_tty_state(scb, &state))
429 fprintf(stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
430
431 #ifdef HAVE_TERMIOS
432 state.termios.c_cc[VTIME] = timeout * 10;
433 #endif
434
435 #ifdef HAVE_TERMIO
436 state.termio.c_cc[VTIME] = timeout * 10;
437 #endif
438
439 scb->current_timeout = timeout;
440
441 if (set_tty_state (scb, &state))
442 fprintf(stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
443
444 return 0;
445 }
446 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
447 }
448
449 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
450 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
451 char if successful. Returns -2 if timeout expired, EOF if line dropped
452 dead, or -3 for any other error (see errno in that case). */
453
454 static int
455 hardwire_readchar(scb, timeout)
456 serial_t scb;
457 int timeout;
458 {
459 int status;
460
461 if (scb->bufcnt-- > 0)
462 return *scb->bufp++;
463
464 status = wait_for(scb, timeout);
465
466 if (status < 0)
467 return status;
468
469 scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
470
471 if (scb->bufcnt <= 0)
472 if (scb->bufcnt == 0)
473 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
474 distinguish between EOF & timeouts
475 someday] */
476 else
477 return SERIAL_ERROR; /* Got an error from read */
478
479 scb->bufcnt--;
480 scb->bufp = scb->buf;
481 return *scb->bufp++;
482 }
483
484 #ifndef B19200
485 #define B19200 EXTA
486 #endif
487
488 #ifndef B38400
489 #define B38400 EXTB
490 #endif
491
492 /* Translate baud rates from integers to damn B_codes. Unix should
493 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
494
495 static struct
496 {
497 int rate;
498 int code;
499 }
500 baudtab[] =
501 {
502 {50, B50},
503 {75, B75},
504 {110, B110},
505 {134, B134},
506 {150, B150},
507 {200, B200},
508 {300, B300},
509 {600, B600},
510 {1200, B1200},
511 {1800, B1800},
512 {2400, B2400},
513 {4800, B4800},
514 {9600, B9600},
515 {19200, B19200},
516 {38400, B38400},
517 {-1, -1},
518 };
519
520 static int
521 rate_to_code(rate)
522 int rate;
523 {
524 int i;
525
526 for (i = 0; baudtab[i].rate != -1; i++)
527 if (rate == baudtab[i].rate)
528 return baudtab[i].code;
529
530 return -1;
531 }
532
533 static int
534 hardwire_setbaudrate(scb, rate)
535 serial_t scb;
536 int rate;
537 {
538 struct hardwire_ttystate state;
539
540 if (get_tty_state(scb, &state))
541 return -1;
542
543 #ifdef HAVE_TERMIOS
544 cfsetospeed (&state.termios, rate_to_code (rate));
545 cfsetispeed (&state.termios, rate_to_code (rate));
546 #endif
547
548 #ifdef HAVE_TERMIO
549 #ifndef CIBAUD
550 #define CIBAUD CBAUD
551 #endif
552
553 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
554 state.termio.c_cflag |= rate_to_code (rate);
555 #endif
556
557 #ifdef HAVE_SGTTY
558 state.sgttyb.sg_ispeed = rate_to_code (rate);
559 state.sgttyb.sg_ospeed = rate_to_code (rate);
560 #endif
561
562 return set_tty_state (scb, &state);
563 }
564
565 static int
566 hardwire_set_process_group (scb, ttystate, group)
567 serial_t scb;
568 serial_ttystate ttystate;
569 int group;
570 {
571 ((struct hardwire_ttystate *)ttystate)->process_group = group;
572 return 0;
573 }
574
575 static int
576 hardwire_write(scb, str, len)
577 serial_t scb;
578 const char *str;
579 int len;
580 {
581 int cc;
582
583 while (len > 0)
584 {
585 cc = write(scb->fd, str, len);
586
587 if (cc < 0)
588 return 1;
589 len -= cc;
590 str += cc;
591 }
592 return 0;
593 }
594
595 static void
596 hardwire_close(scb)
597 serial_t scb;
598 {
599 if (scb->fd < 0)
600 return;
601
602 close(scb->fd);
603 scb->fd = -1;
604 }
605
606 static struct serial_ops hardwire_ops =
607 {
608 "hardwire",
609 0,
610 hardwire_open,
611 hardwire_close,
612 hardwire_readchar,
613 hardwire_write,
614 hardwire_flush_output,
615 hardwire_raw,
616 hardwire_get_tty_state,
617 hardwire_set_tty_state,
618 hardwire_print_tty_state,
619 hardwire_noflush_set_tty_state,
620 hardwire_setbaudrate,
621 hardwire_set_process_group
622 };
623
624 int job_control;
625 #if defined (HAVE_TERMIOS)
626 #include <unistd.h>
627 #endif
628
629 /* This is here because this is where we figure out whether we (probably)
630 have job control. Just using job_control only does part of it because
631 setpgid or setpgrp might not exist on a system without job control.
632 It might be considered misplaced (on the other hand, process groups and
633 job control are closely related to ttys).
634
635 For a more clean implementation, in libiberty, put a setpgid which merely
636 calls setpgrp and a setpgrp which does nothing (any system with job control
637 will have one or the other). */
638 int
639 gdb_setpgid ()
640 {
641 int retval = 0;
642 if (job_control)
643 {
644 #if defined (NEED_POSIX_SETPGID) || defined (HAVE_TERMIOS)
645 /* Do all systems with termios have setpgid? I hope so. */
646 retval = setpgid (0, 0);
647 #else
648 #if defined (TIOCGPGRP)
649 #if defined(USG) && !defined(SETPGRP_ARGS)
650 retval = setpgrp ();
651 #else
652 retval = setpgrp (getpid (), getpid ());
653 #endif /* USG */
654 #endif /* TIOCGPGRP. */
655 #endif /* NEED_POSIX_SETPGID */
656 }
657 return retval;
658 }
659
660 void
661 _initialize_ser_hardwire ()
662 {
663 serial_add_interface (&hardwire_ops);
664
665 /* OK, figure out whether we have job control. */
666
667 #if defined (HAVE_TERMIOS)
668 /* Do all systems with termios have the POSIX way of identifying job
669 control? I hope so. */
670 #ifdef _POSIX_JOB_CONTROL
671 /* AIX defines _POSIX_JOB_CONTROL to an empty string, so I guess
672 defining _POSIX_JOB_CONTROL to 0 to mean no job control doesn't work
673 (I don't have the standard handy). */
674 job_control = 1;
675 #else
676 job_control = sysconf (_SC_JOB_CONTROL);
677 #endif
678
679 #else /* not termios. */
680
681 #ifdef TIOCGPGRP
682 job_control = 1;
683 #else
684 job_control = 0;
685 #endif /* TIOCGPGRP */
686
687 #endif /* not termios. */
688 }
This page took 0.054904 seconds and 5 git commands to generate.